Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Inheritance SWE 344 Internet Protocols & Client Server Programming.

Similar presentations


Presentation on theme: "Class Inheritance SWE 344 Internet Protocols & Client Server Programming."— Presentation transcript:

1 Class Inheritance SWE 344 Internet Protocols & Client Server Programming

2  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 Class Inheritance 2

3 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. Class Inheritance 3

4 A derived 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. Class Inheritance 4

5 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"); } }; Class Inheritance 5

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

7 using System; public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); Console.ReadLine(); } Class Inheritance Parent Constructor. Child Constructor. I'm a Parent Class. 7

8 8 using System; namespace InheritanceApplication { class Shape { protected int width; protected int height; public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } Example of Inheritance

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

10 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 11


Download ppt "Class Inheritance SWE 344 Internet Protocols & Client Server Programming."

Similar presentations


Ads by Google