Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Inheritance

• Inheritance is the capability of a class to use the properties


and methods of another class while adding its own
functionality.

• Enables you to add new features and functionality to an existing


class without modifying the existing class.

2
Inheritance
• Base class and Subclass

 A Baseclass or parent class is the one from which another


class inherits attributes and behavior.

 A subclass or child class is a class that inherits attributes


and behavior from a Baseclass.

3
Types of Inheritance
• Single inheritance
Subclass is derived from only one superclass.

Super class
/Base Class
(Parent)

Sub class
/Derived Class
(Child)

5
public class Accountcreditinfo //base class    
{  
    public string Credit()  
    {  
        return "balance is credited";  
    }  
}  
public class debitinfo : Accountcreditinfo //derived class    
{  
    public string debit()  
    {  
        Credit();                       ////derived class method    
        return "balance is debited";  
    }  
}  
Types of Inheritance
Hierarchical inheritance

This is the type of inheritance in which there are multiple classes derived from one
base class. This type of inheritance is used when there is a requirement of one class
feature that is needed in multiple classes.

Employe
e

Manage Secretary Programmer


r

4
class A  //base class    
{  
    public string msg()  
    {  
        return "this is A class Method";  
    }  
}  
class B : A  
{  
    public string info()  
    {  
        msg();  
        return "this is B class Method";  
    }  
    class C : A  
    {  
        public string getinfo()  
        {  
            msg();  
            return "this is B class Method";  
        }  
    }  
}    
Types of Inheritance
Multilevel inheritance
 
When one class is derived from another derived class then this type of inheritance is
called multilevel inheritance.

6
public class Person  
{  
    public string persondet()  
    {  
        return "this is the person class";  
    }  
}  
public class Bird : Person  
{  
    public string birddet()  
    {  
        persondet();  
        return "this is the birddet Class";  
    }  
}  
public class Animal : Bird  
{  
    public string animaldet()  
    {  
        persondet();  
        birddet();  
        return "this is the Animal Class";  
    }  
}   
Types of Inheritance
• Multiple Inheritance
C# does not support multiple inheritances of classes. To overcome this
problem we can use interfaces.

Super Class Super Class


(Parent 1) (Parent 2)

Sub Class
(Child)

6
public interface IA //ineterface  1    
{  
     string setImgs(string a);  
}  
public interface IB  //Interface 2    
{  
     int getAmount(int Amt);  
}  

public class ICar : IA, IB //implementatin    
{  
    public int getAmount(int Amt)  
    {  
        return 100;  
    }  
    public string setImgs(string a)  
    {  
        return "this is the car";  
    }  
}    
What really happens?

When an object is created using new, the system must allocate enough memory to
hold all its instance variables.
This includes any inherited instance variables

In this example, we can say that an Employee "is a kind of" Person.
An Employee object inherits all of the attributes, methods
and associations of Person

Person
- name: String Person
- dob: Date name = "John Smith"
dob = Jan 13, 1954
Employee
name = "Sally Halls"
is a kind of
dob = Mar 15, 1968
Employee employeeID = 37518
- employeeID: int salary = 65000
- salary: int startDate = Dec 15,
- startDate: Date 2000

You might also like