Presentation is loading. Please wait.

Presentation is loading. Please wait.

2.7 Inheritance Types of inheritance

Similar presentations


Presentation on theme: "2.7 Inheritance Types of inheritance"— Presentation transcript:

1 2.7 Inheritance 2.7.1 Types of inheritance
-> Creating a new class from existing class is called as inheritance Single Inheritance Hierarchical Inheritance Multi Level Inheritance Hybrid Inheritance Multiple Inheritance

2 1.Single Inheritance when a single derived class is created from a single base class then the inheritance is called as single inheritance.

3 2.Hierarchical Inheritance when more than one derived class are created from a single base class, then that inheritance is called as hierarchical inheritance.

4 3.Multi Level Inheritance when a derived class is created from another derived class, then that inheritance is called as multi level inheritance

5 4.Hybrid Inheritance Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance

6 Multiple Inheritance when a derived class is created from more than one base class then that inheritance is called as multiple inheritance. But multiple inheritance is not supported by .net using classes and can be done using interfaces

7 Calling base function Definition: A function is a block of code that performs a calculation and returns a value

8 Abstract class & function
Classes can be declared as abstract by putting the keyword abstract before the class. EX. public abstract class A { public abstract void DoWork(int i); }

9 Example of abstract class
using System; class Hello { public abstract class abs { public abstract void speak(); } public class abc : abs { public override void speak() { Console.WriteLine("Hello!"); Console.Read(); static void Main() { abc hello = new abc(); hello.speak();

10 Sealed Classes When you want to restrict your classes from being inherited by others you can create the class as sealed class.  ->To create a class as sealed class, create the class using the keyword sealed. -> sealed class cannot be used as a base class. [Access Modifier] sealed class classname { }

11 Sealed class example using System; sealed class SealedClass{ public int x; public int y; } class MainClass { static void Main() { SealedClass sc = new SealedClass(); sc.x = 110; sc.y = 150; Console.WriteLine("x = {0} \ny = {1}", sc.x, sc.y); Console.Read();

12 Constructor A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor. Constructors are mainly used to initialize private fields of the class while creating an instance for the class. When you are not creating a constructor in the class, then compiler will automatically create a default constructor in the class that initializes all numeric fields in the class to zero and all string and object fields to null.  To create a constructor, create a method in the class with same name as class name and has the following syntax. [Access Modifier] ClassName([Parameters]) { }

13 Example of constructore
using System; class ProgramCall { int i, j; //default contructor public ProgramCall() { i = 45; j = 76; } public static void Main() { //When an object is created , contructor is called ProgramCall obj = new ProgramCall(); Console.WriteLine(obj.i); Console.WriteLine(obj.j); Console.Read();

14 Modifiers The access modifier is the key of Object Oriented Programming, to promote encapsulation, a type in C# should limit its accessibility and this accessibility limit are specified using Access 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

15 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 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.

16 Accessibility:  Cannot be accessed by object By derived classes

17

18 Overview Abstraction Definition : Abstraction is "Collection of data" and Encapsulation is grouping of data in appropriate access specified". Encapsulation is data wrapping for the propose of simplifying system design and implementation.

19 Interface Interface like classes define set of properties, methods and events. But unlike classes interface don't provide any implementation. They implemented by classes from different separate entities.

20 we create a Test instance and store it in an object reference.
Program that uses interface [C#] using System; interface iface{ void Read(); } class Test : iface { public void Read() { Console.WriteLine("Read interface "); Console.Read(); } } class Program { static void Main() { iface iobj = new Test(); // Create instance. iobj.Read(); // Call method on interface.

21 KEY: When a class implements an interface, it can be used through a reference to that interface.

22 Derived interface Interfaces are some sort of Class, similar to the Abstract Classes (with difference Abstract Classes can implement its methods-functions, Interfaces can not.). Idea behind an Interface is to create a Class that can tell to inherited Class to implement certain methods or functions. It is some sort of contract between the Interface and inherited Class. 


Download ppt "2.7 Inheritance Types of inheritance"

Similar presentations


Ads by Google