1 / 49

Inheritance

Inheritance. Chapter 4. Inheritance. Inheritance 4.1 Types of inheritance 4.2 Implementation Inheritance Calling base version of function Abstract class & function Sealed class & method Constructors of derived class 4.3 Modifiers Visibility modifiers Other modifiers

Download Presentation

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. Inheritance Chapter 4

  2. Inheritance Inheritance 4.1 Types of inheritance 4.2 Implementation Inheritance Calling base version of function Abstract class & function Sealed class & method Constructors of derived class 4.3 Modifiers Visibility modifiers Other modifiers 4.4 Overview of Abstraction 4.5 Interfaces Definition & declaration Derived interface

  3. Inheritance • Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. • Inheritance means taking an existing class and adding functionality by deriving a new class from it. • The class you start with is called the base class, and the new class you create is called he derived class. • Inheritance is a mechanism of sharing the members among the classes.

  4. Example using System; public class ParentClass{    public ParentClass()    {        Console.WriteLine("Parent Constructor.");    }    public void print()    {        Console.WriteLine("I'm a Parent Class.");    }}

  5. Example • public class ChildClass : ParentClass{    public ChildClass()    {        Console.WriteLine("Child Constructor.");    }    public static void Main()    {        ChildClass child = new ChildClass();        child.print();    }}

  6. Types Of Inheritance. • In C# inheritance may be implemented in different combination as shown in below. • Single Inheritance (only one base class) • Multilevel Inheritance (several base class) • Multiple Inheritance (one base, many sub class) • Hierarchical Inheritance (derived from derives class)

  7. Types Of Inheritance. Class A Class A Class B Class B Class C Class D Single inheritance Hierarchical inheritance Class A Class A Class B Class B Class C Class C Multiple inheritance Multilevel inheritance

  8. Single Inheritance Using System; Class A { Public void welcome() { Console.writeLine(“welcome to C#”); } } Class B : A //class B is derived by Class A { Public void Hello() { Console.WriteLine(“hello sir”); } }

  9. Single Inheritance Class Single { Public Static void Main() { B obj= new B(); Obj.Welcome(); Obj.Hello(); } }

  10. Multilevel Inheritance Using System; Class A { Public void welcome() { Console.writeLine(“welcome to C#”); } } Class B : A //class B is derived by Class A { Public void Hello() { Console.WriteLine(“hello sir”); } }

  11. Multilevel Inheritance Class C: B //class C is derived by Class B { Public void HowRu() { Console.WriteLine(“how are You?”); } } Class Single { Public Static void Main() { B obj= new B(); Obj.Welcome(); //Super Class function Obj.Hello(); // Base Class function Obj.HowRu(); //Own Class function }}

  12. Hierarchical Inheritance Using System; Class A { Public void welcome() { Console.writeLine(“welcome to C#”); } } Class B : A //class B is derived by Class A { Public void Hello() { Console.WriteLine(“hello sir”); } }

  13. Hierarchical Inheritance Class C: A //class C is derived by Class A { Public void HowRu() { Console.WriteLine(“how are You?”); } } Class Single{ Public Static void Main() { B obj1= new B(); C obj2 = new C(); Obj1.Welcome(); Obj1.Hello(); Obj2.welcome(); Obj2.HowRu(); }}

  14. Implementation Inheritance • Calling base version of function • Abstract class & function • Sealed class & method • Constructors of derived class

  15. Calling base version of function • C# has a special syntax for calling base version of a method from a derived class: base.<method name>() • For example if u want a method in a derived class to return 90 percent of the value returned by the base class method, you can use the following syntax:

  16. Calling base version of function Class CustomerAccount { Public virtual decimal CalculatePrice() { Return 0; }} Class GoldAccount : CustomerAccount { public override decimal CalculatePrice() { Return base. CalculatePrice() * 0.9; }}

  17. Abstract class & function • A C# abstract class contains abstract members which define what a subclass should contain. • These abstract members only declare that a member of a particular type is required, it does not implement the member. Implementation of abstract members takes place within the derived class. • A subclass which derives from an abstract class and fails to implement abstract methods will fail to compile. Abstract classes are declared using the abstract modifier in the class declaration: public abstract class Talk { }

  18. Abstract class using System; class Hello { public abstract class Talk { public abstract void speak(); } public class SayHello : Talk { public override void speak() { Console.WriteLine("Hello!"); } }

  19. Abstract class static void Main() { SayHello hello = new SayHello(); hello.speak(); } }

  20. Features of abstract class • The abstract class may contain the one or more abstract methods. • The abstract class cannot be instantiate in the other classes. • If you are inheriting the abstract class in other class, you should implement all the abstract methods in the derived class. • We cannot apply a sealed modifier to it.

  21. Abstract method • Similar to abstract classes, we can also create abstract methods. when an instance method declaration include the modifier abstract, the method is said to be an abstract method. • An abstract method is implicitly a virtual method and does not provide any implementation. therefore an abstract method does not have method body. Example. public abstract void Draw( int x , int y);

  22. Abstract method public abstract void speak(); } public class SayHello : Talk { public override void speak() { Console.WriteLine("Hello!"); }

  23. Characteristics of an abstract method are: • It can not have implementation. • Its implementation must be provided in non-abstract derived classes by overriding the method . • It can be declared only in abstract classes. • It can not take either static or virtual modifiers. • An abstract declaration is permitted to override a virtual method.

  24. Sealed class & method • Sealed classes are used to restrict the inheritance feature of object oriented programming. • Once a class is defined as sealed class, this class cannot be inherited. In C#, the sealed modifier is used to define a class as sealed. • structs are sealed. You cannot derive a class from a struct. 

  25. Sealed class & method • The following class definition defines a sealed class in C#: // Sealed class sealed class SealedClass {      }In the following code, I create a sealed class SealedClass and use it from Class1. If you run this code, it will work fine. But if you try to derive a class from sealed class, you will get an error.

  26. Sealed class & method • The main purpose of a sealed class to take away the inheritance feature from the user so they cannot derive a class from a sealed class. • One of the best usage of sealed classes is when you have a class with static members. • A sealed class can not be an abstract class.

  27. Sealed class & method Sealed class Finalclass { //etc } Class Derivedclass:Finalclass // wrong error generate { //etc }

  28. Sealed method • When an instance method declaration includes the sealed modifier, the method is said to be a sealed method. it derived class cannot override this method. • A sealed method is used to override an inherited virtual method with the same signature. That means the sealed modifier is always used in combination with the override modifier.

  29. Sealed method • Example. class A { Public virtual void fun() {… } } Class B:A { Public sealed override void Fun() {…..} }

  30. Constructors of derived class ConsoleApplication1

  31. Modifiers • Access modifiers are an integral part of object-oriented programming. • They support the concept of encapsulation, which promotes the idea of hiding functionality. • Access modifiers allow you to define who does or doesn't have access to certain features. • There are two types of modifiers. Visibility modifier Other modifiers

  32. Visibility Modifiers.

  33. Public The public keyword is an access modifier for types and type members. Public access is the most permissive access level. There are no restrictions on accessing public members. • Accessibility: Can be accessed by objects of the class Can be accessed by derived classes

  34. Public • Exa_public

  35. Private Private access is the least permissive access level. • Private members are accessible only within the body of the class or the struct in which they are declared. • Accessibility: • Cannot be accessed by object • Cannot be accessed by derived classes

  36. Private • Example: In the following example num2 is not accessible outside the class. • Exa_private

  37. Protected A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member. • A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type. • Accessibility: Cannot be accessed by object By derived classes

  38. Protected • In the below program we try to access protected member in Main, it is not available as shown in the picture below that num1 is not listed in intellisense. • ex_protected

  39. Internal • The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). • In other words, access is limited exclusively to classes defined within the current project assembly. • Accessibility: In same assembly (public)  Can be accessed by objects of the class Can be accessed by derived classes • In other assembly (internal)  Cannot be accessed by object Cannot be accessed by derived classes

  40. Protected internal • The protected internal accessibility means protected OR internal, not protected AND internal. • In other words, a protected internal member is accessible from any class in the same assembly, including derived classes. • The protected internal access modifier seems to be a confusing but is a union of protected and internal in terms of providing access but not restricting. It allows:   Inherited types, even though they belong to a different assembly, have access to the protected internal members. Types that reside in the same assembly, even if they are not derived from the type, also have access to the protected internal members.

  41. Other modifiers

  42. Other modifiers

  43. Interface • C# does not support multiple inheritance. • Like class A:B:C { …. …. } • So that C# provides an alternate approach known as interface.

  44. Interface • An interface looks like a class, but has no implementation. The only thing it contains are definitions of events, indexers, methods and/or properties. • The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined.

  45. Interface • Interface is a special class whose purpose is to serve as a template and used to implement the concept of multiple inheritance. • An interface is primarily created like a class: it has a name ,a body and can have members. Interface ICourDimension { }

  46. Interface • Example: • Ex_interface • ex_interface1

  47. Interface vs. Abstract Class

  48. Interface vs. Abstract Class

  49. Interface vs. Abstract Class

More Related