inheritance
DESCRIPTION
TRANSCRIPT
Inheritance• Inheritance is the foundational principles of
object-oriented programming because it allows the creation of hierarchical classifications.
• a class that is inherited is called a base class. • The class that does the inheriting is called a
derived class.• a derived class is a specialized version of a base
class. • It inherits all of the variables, methods,
properties, and indexers defined by the base class and adds its own unique elements
Types of Inheritance
• Single Inheritance• Hierarchical Inheritance• Multi Level Inheritance• Hybrid Inheritance• Multiple Inheritance
Single Inheritance
Base
Drived
Hierarchical Inheritance
Base
Drived1 Drived2
Multi Level Inheritance
Base
Drived1
Drived2
Hybrid Inheritance
Drived1
Drived2 Drived2
Base
Multiple Inheritance (interface)
Base
Drived
Base
Exampleclass a { public void disp() { Console.WriteLine("Class A "); } } class b:a { public void disp1() { Console.WriteLine("Class B "); } }
class c:a { public void disp2() { Console.WriteLine("class C "); } } class D : b { public void f() { Console.WriteLine("class D "); } }
class Program { static void Main(string[] args) { b o1 = new b(); c o2 = new c(); D d1 = new D();
o1.disp(); o2.disp(); d1.f(); d1.disp(); } }
Interface • An interface defines a set of methods that will
be implemented by a class. • An interface does not, itself, implement any
method. • Thus, an interface is a purely logical construct
that describes functionality without specifying implementation.
• Interfaces are syntactically similar to abstract classes. However, in an interface, no method can include a body.
• It specifies what must be done, but not how. • Once an interface is defined, any number of
classes can implement it.• Also, one class can implement any number
of interfaces.• To implement an interface, a class must
provide bodies (implementations) for the methods described by the interface.
• Each class is free to determine the details of its own implementation.
public interface A1 { void f1(); void f2(); } class A:A1 { public void f1() {Console.WriteLine("A1");} public void f2() { }static void Main() { A obj = new A(); obj.f1(); //obj.f2(); } }
• Interfaces are declared by using the interface keyword. Here is a simplified form of an interface declaration:
interface name {ret-type method-name1(param-list);ret-type method-name2(param-list); // ...ret-type method-nameN (param-list);}
• Methods are declared using only their return type and signature. They are, essentially, abstract methods.
• Each class that includes an interface must implement all of the methods.
• In an interface, methods are implicitly public , and no explicit access specifier is allowed.
• Interfaces cannot have data members.• They cannot define constructors, destructors,
or operator methods.• no member can be declared as static.
interface abc { void xyz(); }
class Demo : abc { public static void Main() { Console.WriteLine("Hello Interfaces"); } void xyz() {
System.Console.WriteLine("In xyz"); }}
Abstract Classe• Sometimes base class defines only a
generalized form that will be shared by all of its derived classes, leaving it to each derived class to fill in the details.
• the derived classes must implement, but does not, itself, provide an implementation of one or more of these methods.
• An abstract method is created by specifying the abstract type modifier.
• An abstract method contains no body and is, therefore, not implemented by the base class. Thus, a derived class must override it—it cannot simply use the version defined in the base class.
• As you can probably guess, an abstract method is automatically virtual, and there is no need to use the virtual modifier.
• In fact, it is an error to use virtual and abstract together.
• general form:abstract type name(parameter-list );
• The abstract modifier can be used only on instance methods.• It cannot be applied to static methods.• Since an abstract class does not define a
complete implementation, there can be no objects of an abstract class.
• Thus, attempting to create an object of an abstract class by using new will result in a compile-time error.
• for an abstract class to contain concrete methods that a derived class is free to use as-is.
• Only those methods declared as abstract must be overridden by derived classes.
abstract class absClass
{
//A Non abstract method
public int AddTwoNumbers(int Num1, int Num2)
{ return Num1 + Num2; }
//An abstract method, to be //overridden in derived class
public abstract int MultiplyTwoNos(int Num1, int Num2);
}
//A Child Class of absClass
class absDerived:absClass { static void Main(string[] args) { //You can create an instance of the derived class absDerived calculate = new absDerived(); int added = calculate.AddTwoNumbers(10,20);int multiplied = calculate.MultiplyTwoNos(10,20); Console.WriteLine("Added : {0}, Multiplied : {1}",
added, multiplied); }
//using override keyword, implementing the abstract method //MultiplyTwoNumbers
public override int MultiplyTwoNos(int Num1, int Num2)
{ return Num1 * Num2; } }
DifferencesFeature Interface Abstract
classMultiple inheritance A class may inherit
several interfaces.A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers An interface cannot have access modifiers for the functions, properties etc everything is assumed as public.
An abstract class can contain access modifiers for the functions, properties.
Core VS Peripheral
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behaviour or status then abstractclass is better to use.
Speed Requires more time to find the actual method in the corresponding classes.
Requires less time to find the actual method in the corresponding classes.
Adding functionality (Versioning)
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants
No fields can be defined in interfaces
An abstract class can have fields and constrants defined
Name Hiding with Interface Inheritance
• When one interface inherits another,• it is possible to declare a member in the derived
interface that hides one defined by the base interface.
• This happens when a member in a derived interface has the same declaration as one in the base interface.
• In this case, the base interface name is hidden. This will cause a warning message unless you specify the derived interface member with new .
Interface mapping
• A class or struct must provide implementations of all members of the interfaces that are listed in the base class list of the class or struct.
• The process of locating implementations of interface members in an implementing class or struct is known as interface mapping.
Explicit Implementations/mapping
• When implementing a member of an interface, it is possible to fully qualify its name with its interface name.
• Doing this creates an explicit interface member implementation, or explicit implementation.
interface IFace1 { int MyMeth(int x);}//then it is legal to implement IMyIF as shown here:
class MyClass : IMyIF { int IFace1.MyMeth(int x) { return x / 3; }}• As you can see, when the MyMeth( ) member of
IFace1 is implemented, its complete name, including its interface name, is specified.
• it is possible for a class to implement two interfaces, both of which declare methods by the same name and type signature.
• Qualifying the names with their interfaces removes the ambiguity from this situation.