500 likes | 868 Views
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
E N D
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 4.4 Overview of Abstraction 4.5 Interfaces Definition & declaration Derived interface
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.
Example using System; public class ParentClass{ public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); }}
Example • public class ChildClass : ParentClass{ public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); }}
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)
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
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”); } }
Single Inheritance Class Single { Public Static void Main() { B obj= new B(); Obj.Welcome(); Obj.Hello(); } }
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”); } }
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 }}
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”); } }
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(); }}
Implementation Inheritance • Calling base version of function • Abstract class & function • Sealed class & method • Constructors of derived class
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:
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; }}
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 { }
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!"); } }
Abstract class static void Main() { SayHello hello = new SayHello(); hello.speak(); } }
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.
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);
Abstract method public abstract void speak(); } public class SayHello : Talk { public override void speak() { Console.WriteLine("Hello!"); }
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.
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.
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.
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.
Sealed class & method Sealed class Finalclass { //etc } Class Derivedclass:Finalclass // wrong error generate { //etc }
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.
Sealed method • Example. class A { Public virtual void fun() {… } } Class B:A { Public sealed override void Fun() {…..} }
Constructors of derived class ConsoleApplication1
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
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
Public • Exa_public
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
Private • Example: In the following example num2 is not accessible outside the class. • Exa_private
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
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
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
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.
Interface • C# does not support multiple inheritance. • Like class A:B:C { …. …. } • So that C# provides an alternate approach known as interface.
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.
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 { }
Interface • Example: • Ex_interface • ex_interface1