1 / 11

Class Inheritance

SWE 344 Internet Protocols & Client Server Programming. Class Inheritance. Class Inheritance. Inheritance is one of the primary concepts of object-oriented programming. It allows one class to pass on properties and methods to child classes It allows us to reuse existing code.

pomona
Télécharger la présentation

Class Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SWE 344 Internet Protocols & Client Server Programming Class Inheritance

  2. Class Inheritance • Inheritance is one of the primary concepts of object-oriented programming. • It allows one class to pass on properties and methods to child classes • It allows us to reuse existing code. • Through effective employment of reuse, we can save time in our programming. • C# supports single class inheritance only. Therefore, we can specify only one base class to inherit from. • However, it does allow multiple interface inheritance

  3. Class Inheritance Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (or pillars) of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If Class C is derived from Class B, and Class B is derived from Class A, Class C inherits the members declared in Class B and Class A.

  4. Class Inheritance Aderived class is a specialization of the base class. For example, if you have a base class Animal, you might have one derived class that is named DOG and another derived class that is named CAT. A DOG is an Animal, and a CAT is an Animal, but each derived class represents different specializations of the base class. When you define a class to derive from another class, the derived class implicitly gains all the members of the base class, except for its constructors and destructors. The derived class can thereby reuse the code in the base class without having to re-implement it. In the derived class, you can add more members. In this manner, the derived class extends the functionality of the base class.

  5. Class Inheritance class Animal { public Animal() { Console.WriteLine("Animal constructor"); } public void Greet() { Console.WriteLine("Animal says Hello"); } public void Talk() { Console.WriteLine("Animal talk"); } public virtual void Sing() { Console.WriteLine("Animal song"); } }; class Dog : Animal { public Dog() { Console.WriteLine("Dog constructor"); } public new void Talk() { Console.WriteLine("Dog talk"); } public override void Sing() { Console.WriteLine("Dog song"); } };

  6. Class Inheritance Output Animal constructor Animal talk Animal song Animal says Hello Animal a1 = new Animal(); a1.Talk(); a1.Sing(); a1.Greet(); Output Animal constructor Dog constructor Animal talk Dog song Animal says Hello Doga2 = new Dog(); a2.Talk(); a2.Sing(); a2.Greet();

  7. Class Inheritance using System; publicclassParentClass { publicParentClass() { Console.WriteLine("Parent Constructor."); } publicvoid print() { Console.WriteLine("I'm a Parent Class."); } } publicclassChildClass : ParentClass { publicChildClass() { Console.WriteLine("Child Constructor."); } publicstaticvoid Main() { ChildClass child = newChildClass(); child.print(); Console.ReadLine(); } } Parent Constructor. Child Constructor. I'm a Parent Class.

  8. Example of Inheritance using System; namespaceInheritanceApplication { classShape { protected intwidth; protected intheight; public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } }

  9. // Derived class class Rectangle: Shape { public intgetArea() { return(width * height); } } classRectangleTester { static void Main(string[] args) {

  10. Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. Console.WriteLine("Total area: {0}",Rect.getArea()); Console.ReadKey(); } } }

  11. END

More Related