• Like
Inheritance
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Inheritance

  • 222 views
Published

 

Published in Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
222
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 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
  • 2. Types of Inheritance • • • • • Single Inheritance Hierarchical Inheritance Multi Level Inheritance Hybrid Inheritance Multiple Inheritance
  • 3. Single Inheritance Base Drived
  • 4. Hierarchical Inheritance Base Drived1 Drived2
  • 5. Multi Level Inheritance Base Drived1 Drived2
  • 6. Hybrid Inheritance Base Drived1 Drived2 Drived2
  • 7. Multiple Inheritance (interface) Base Base Drived
  • 8. Example class a { public void disp() { Console.WriteLine("Class A "); } } class b:a { public void disp1() { Console.WriteLine("Class B "); } }
  • 9. class c:a { public void disp2() { Console.WriteLine("class C "); } } class D : b { public void f() { Console.WriteLine("class D "); } }
  • 10. 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(); } }
  • 11. 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.
  • 12. • 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.
  • 13. 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(); } }
  • 14. • 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); }
  • 15. • 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.
  • 16. interface abc { void xyz(); } class Demo : abc { public static void Main() { Console.WriteLine("Hello Interfaces"); void xyz() { System.Console.WriteLine("In xyz"); }} }
  • 17. 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.
  • 18. • 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.
  • 19. • 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.
  • 20. • for an abstract class to contain concrete methods that a derived class is free to use asis. • Only those methods declared as abstract must be overridden by derived classes.
  • 21. 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); }
  • 22. //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); }
  • 23. //using override keyword, implementing the abstract method //MultiplyTwoNumbers public override int MultiplyTwoNos(int Num1, int Num2) { return Num1 * Num2; } }
  • 24. Differences Feature Interface Abstract class Multiple 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.
  • 25. 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. Homogeneity If various implementations only share method signatures then it is better to use Interfaces. Speed Requires more time to find the actual method in the corresponding classes. An abstract class defines the core identity of a class and there it is used for objects of the same type. If various implementations are of the same kind and use common behaviour or status then abstractclass is better to use. Requires less time to find the actual method in the corresponding classes.
  • 26. Adding If we add a new functionality method to an (Versioning) Interface then we have to track down all the implementations of the interface and define implementation for the new method. Fields and No fields can be defined Constants in interfaces If we add a new method to an abstract class th en we have the option of providing default implementation and therefore all the existing code might work properly. An abstract class ca n have fields and constrants defined
  • 27. 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 .
  • 28. 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.
  • 29. 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.
  • 30. 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.
  • 31. • 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.