inheritance and interface concepts of java

     Inheritance


    Definition – what is inheritance

    Types – Single inheritance

                 Multilevel inheritance

                Multiple inheritance ( thru interface )

                Hybrid inheritance


What is inheritance

    • Inheritance is acquiring properties of a class by another class

    • Here, properties means that data and methods which are belongs to that base class

    • Properties owning class is called as base class or parent class or super class

    • Properties acquiring class is called as Derived class or child class or sub class


  Single inheritance

           

• One child class to a one base class is called single level inheritance

Example

class Parent

{

int house, farm, car;

void numOfAssets{

house=10; farm=1; car=3; }

}

class Child extends Parent

{

public static void main(String args[])throws IOException

{

System.out.println(“ house =“, house,”farm=“,farm,”cars",car);

}

}


Multilevel inheritance


        When a class(grandchild) extends another class( child), and that another class(child) extends another class(parent) then, this is called multilevel inheritance.

Example

class GrandParent

{

int house, car, farm;

void estimation()

{ house=2;car=2;farm=1;}

}

class Parent inherits GrandParent

{

int van;

void estimation1()

{house=house+2;car=car+1;farm=farm+1;van=2;}

}

class Child inherits Paren{

psvm(){

S.o.p(“print all asserts”);

}

}


Multiple inheritance 

• When a class inheriting from more than one parent class is called as

multiple inheritance.

• In real time it is not possible to have than one Parent to a child so, java doesn’t permit to inherit more than one parent class , A class cannot extend more than one class at a time. just like two father or two mother cannot give birth a child.

• But it is possible to achieve multiple inheritance by extending one

class and implementing one “interface” , which means a father and a mother can give birth a child.


Hybrid inheritance

• When combining more than one type of inheritance together is called as hybrid inheritance.


Interface


Interface can also called as abstract class

• It contains declaration part of both data and methods

• It does not contain definition part for data and methods

• Definition part will be included wherever it has been implemented

 Example :

interface InterfaDemo{

int car,house; // only declaration 

void estimation(); // only declaration

}

class implements InterfaceDemo{

estimation() //definition part

car=4;  // definition part

house=2;  // definition part

}

Comments

Popular posts from this blog

Unit 3 : Regression

Operating System - Unit - 4 Memory Management and File Managment

Unit 1 - Introduction to Operating Sytem & Unit 2 - Process Characterization