Inheritance - Video
Click here for video on Method Hiding
What are the 4 pillars of any object oriented programming language?
1. Abstraction
2. Inheritance
3. Encapsulation
4. Polymorphism
Do structs support inheritance?
No, structs do not support inheritance, but they can implement interfaces.
What is the main advantage of using inheritance?
Code reuse
Is the following code legal?
class ChildClass : ParentClassA, ParentClassB
{
}
No, a child class can have only one base class. You cannot specify 2 base classes at the same time. C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance.
What will be the output of the following code?
using System;
public class BaseClass
{
public BaseClass()
{
Console.WriteLine("I am a base class");
}
}
public class ChildClass : BaseClass
{
public ChildClass()
{
Console.WriteLine("I am a child class");
}
static void Main()
{
ChildClass CC = new ChildClass();
}
}
Output:
I am a base class
I am a child class
This is because base classes are automatically instantiated before derived classes. Notice the output, The BaseClass constructor executed before the ChildClass constructor.
Does C# support multiple class inheritance?
No, C# supports single class inheritance only. However classes can implement multiple interfaces at the same time.
Why some argue that the use of inheritance should be minimized?This was one of the question asked to me on one of my interview?Can you please post this on your blog>
ReplyDeleteFavor composition over inheritance. This is a design principle.
DeleteSIMPLE , IF U IMPLEMENT INTERFCAE THEN U HAVE TO IMPLEMENT ALL OF THEM ...THAT WHY IT SHOULD BE MINIMIZED.
ReplyDeleteInheritance: Creating new classes from existing classes such that the new classes will acquire all the features of existing classes is called inheritane.
ReplyDeleteexample:class A
{
protected int a;
protected int b;
public void method1()
{
}
}
Note:here A is Base class or Super class, class A access only its members.
class B:A
{
private int c;
public void method2()
{
}
}
Note:B is a subclass and aceess the both class members.
class A are acceceble from class B class A is base class
ReplyDeleteclass A is base class
ReplyDeleteI have 3 class with same method which are inherited for example
ReplyDeleteClass A
{
Protected void getvalues()
{
Consol.WriteLine(“values of a”);
}
}
Class B : A
{
Protected void getvalues()
{
Consol.WriteLine(“values of a”);
}
}
Class C : B
{
Getvalues();
Protected void getvalues()
{
Consol.WriteLine(“values of a”);
}
}
can i call getvalues method of a class in c class
Yes, of course. You can call the getvalues() method of class C as following :
DeleteClass C:B
{
protected void Getvalues()
{
Console.WriteLine("Values of c.");
}
static void Main(string[] args)
{
C c = new C();
c.Getvalues();
}
}
if you want to invoke class B's getvalues() then Use
static void Main(string[] args)
{
B b = new C();
b.Getvalues();
}
but first make getvalues() of Class B as public.
if you want to invoke class A's getvalues() then Use
static void Main(string[] args)
{
A a = new C();
a.Getvalues();
}
but first make getvalues() of Class A as public.
Can you please explain the outcome of his program? It was an interview question.
ReplyDeleteclass Hello
{
static void Main()
{
D d = new D();
A a = new A();
B b = new B();
C c = new C();
a.Amethod();
b.Amethod();
c.Cmethod();
d.Cmethod();
Console.ReadKey();
}
}
public class A
{
public virtual void Amethod()
{
Console.WriteLine("A.F,");
}
}
public class B : A
{
public override void Amethod()
{
Console.WriteLine("B.f,");
}
}
public class C : B
{
public virtual void Cmethod()
{
Console.WriteLine("C.F,");
}
}
public class D : C
{
public override void Cmethod()
{
Console.WriteLine("D.F,");
}
}
}
I believe it will be following:
ReplyDeleteA.F, B.f, B,f, D.F
output-
ReplyDeleteA.F, B.f, C,f, D.F