inheritance

Views:
 
Category: Education
     
 

Presentation Description

Inheritance concept in java

Comments

By: saddamhussainkhan1 (21 month(s) ago)

its nice but i want to download this file so how i can access too it please

By: dinlinsar (39 month(s) ago)

Nice concepts!!!

By: amitgosavi007 (31 month(s) ago)

verryyyyyyyyy fancy

 

Presentation Transcript

Slide 2: 

INHERITANCE IN JAVA By Kalpana G

Presentation Content : 

Presentation Content Inheritance Types of Inheritance in c++ Inheritance in Java Super class & Sub class The super keyword Interface – A way to Multiple Inheritance Difference between C++ & JAVA regarding to Inheritance Advantages of Inheritance

Inheritance : 

Inheritance The process of acquiring the properties(i.e., variables & methods)from one class or object to another class or object is called INHERITANCE It is one of the important features of OOPS concept(i.e.,Object Oriented Programming) It is based on the following two components BASE CLASS-Which is inherited DERIVED CLASS-Which does inheriting

Syntax : 

Syntax The following syntax is given for Inheritance in C++ class derived_name : access base_name { //body of the derived } class-keyword derived_name,base_name-name of the base &derived classes access-It is either private public or protected

Types of Inheritance(C++) : 

Types of Inheritance(C++) Single Inheritance. Multiple Inheritance Multilevel Inheritance Hybrid Inheritance Hierarchical Inheritance.

Single Inheritance : 

Single Inheritance When a sub class(i.e.,derived class) is simply derived from a single super class(i.e., base class)then that mechanism is said to be SINGLE INHERITANCE It has a single base class and derived class

Multiple Inheritance : 

Multiple Inheritance The Mechanism of inheriting the features of more than one base class into a single class is known as MULTIPLE INHERITANCE.

Multilevel Inheritance : 

Multilevel Inheritance Deriving a new class from another derived class.It is known as MULTILEVEL INHERITANCE Here the first derived class is known as INTERMEDIATE CLASS between the base class and the derived class followed by that derived class

Hierarchical Inheritance : 

Hierarchical Inheritance Derivation of several classes from a single class is known as HIERARCHIAL INHERITANCE The traits of one class may be inherited by more than one class,is called Hierarchical inheritance.  Hirerical inheritance is that inheritance which have a tree like structure

Hybrid Inheritance : 

Hybrid Inheritance The combination of two are more types of inheritance is known as HYBRID INHERITANCE Very often the Multiple inheritance and Hierarchical inheritance together forms the Hybrid inheritance

Inheritance in JAVA : 

Inheritance in JAVA The definition of Inheritance is the same in both the C++ and JAVA. But,the types of inheriting is different It allows only the following four types of Inheritance Single Inheritance Multilevel Inheritance Hybrid Inheritance Hierarchial Inheritance It doesn’t supports for Multiple Inheritance. Instead it supports for a new feature called INTERFACE.

Syntax : 

Syntax The following syntax is given for inheritance in JAVA class derived_name extends base_name { //body of the derived } class - keyword derived_name,base_name – name of the base and derived classes extends – keyword that induces inheritance

Super class & Sub class : 

Super class & Sub class These super and sub class are the two major components in inheritance made in the java language Super class It is similar to Base class This class is inherited by the derived class called sub class Like the base class, this class also have objects and constructor functions

Sub class : 

Sub class Sub class It is similar to derived class This is a class which does inheriting (i.e., acquiring the properties) Like the derived class, the sub class also have object and constructor function. Since java has no destructor function, both the super and sub class don’t have any destructor function

Super keyword : 

Super keyword Super Keyword It is a JAVA keyword It is used to access the members of the super class It is used in JAVA for two purposes The first use of the super keyword is to access the hidden data variables of the super class hidden by the sub class The second use of the super keyword is to call the constructor of the super class in the sub class.

Purpose of the Super Keyword : 

Purpose of the Super Keyword The first purpose can be explained as follows: For e.g.., class A is the super class that has two variables int a ,float b, class B is the sub class that also have its own data members named as int a and float b, Then we can access the super class(i.e., class A) variables a and b inside the sub class(i.e.,class B) just by the following command.And the syntax is given as super.member; Here the member be either a variable or a method(i.e.,function) of a super class.

Purpose of the super keyword contd... : 

Purpose of the super keyword contd... The second purpose of the super keyword can be explained as follows: This use can be achieved by using the following command. super(param_list); Here param_list is the list of parameters required by the constructor of the super class. If we want to specify the default constructor then we can pass the empty parantheses.

Single Inheritance : 

Single Inheritance When a sub class(i.e.,derived class) is simply derived from a single super class(i.e., base class)then that mechanism is said to be SINGLE INHERITANCE Single inheritance can be shown pictorially as follows:

Pictorial representation of Single Inheritance

Slide 23: 

A sample program for single inheritance in java class A {   int x;   int y;   int get(int p, int q){     x=p; y=q; return(0);     }      void Show(){       System.out.println(x);       } } class B extends A{   public static void main(String args[]){     A a = new A();    a.get(5,6);     a.Show();     }     void display(){      System.out.println("B");       } }

An Example program for constructor in inheritance class A{  int a;  int b;  int c;  A(int p, int q, int r){    a=p;    b=q;    c=r;  }}    class B extends A{    int d;    B(int l, int m, int n, int o){      super(l,m,n);      d=o;      }

Slide 25: 

void Show(){      System.out.println("a = " + a);      System.out.println("b = " + b);      System.out.println("c = " + c);      System.out.println("d = " + d);    }    public static void main(String args[]){      B b = new B(4,3,8,7);      b.Show();    }  } A sample program for constructor contd…,

Multilevel Inheritance : 

Multilevel Inheritance It is the enhancement of the concept of inheritance When a subclass is derived from a derived class then this mechanism is known as the MULTILEVEL INHERITANCE. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above ( parent ) class.  Multilevel inheritance can go up to any number of level.

Slide 27: 

Pictorial representation of Multilevel Inheritance

Slide 28: 

A sample program for multilevel inheritance in java class A {  int x;  int y;  int get(int p, int q){    x=p; y=q; return(0);    }    void Show(){      System.out.println(x);      }}class B extends A{  void Showb(){    System.out.println("B");    }}

Slide 29: 

Sample program for Multilevel Inheritance Contd…, class C extends B{  void display(){    System.out.println("C");  }  public static void main(String args[]){    A a = new A();    a.get(5,6);    a.Show();    }}

Multiple Inheritance : 

Multiple Inheritance The mechanism of inheriting the features of more than one base class into a single class is known as MULTIPLE INHERITANCE. Java does not support multiple inheritance The multiple inheritance can be achieved by using the interface. In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.

Interface – A way to Multiple Inheritance : 

Interface – A way to Multiple Inheritance An INTERFACE is a type, just as a class. Like a class, an interface defines methods(functions). A class can implement multiple interfaces.

Slide 32: 

A sample program for Interface in Java interface Area{float compute(float x, float y);}class Rectangle implements Area{public float compute(float x, float y){return(x * y);}}class Triangle implements Area{public float compute(float x,float y)

Slide 33: 

Sample program for Interface in Java contd…, {return(x * y/2);}}class InterfaceArea{public static void main(String args[]){Rectangle rect = new Rectangle();Triangle tri = new Triangle();Area area;area = rect;System.out.println("Area Of Rectangle = "+ area.compute(1,2));

Slide 34: 

Sample program for Interface in Java contd…, area = tri;System.out.println("Area Of Triangle = "+ area.compute(10,2));}}/** OUTPUT **Area Of Rectangle = 2.0Area Of Triangle = 10.0*/

Difference between c++ & java regarding to inheritance : 

Difference between c++ & java regarding to inheritance C++ JAVA It uses : (colon) in the syntax It have Multiple Inheritance It have only base class and derived class It have Destructor Function It don’t have base class and derived class as keywords It uses the extends keyword Instead it uses Interface Instead it have superclass and subclass It doesn’t have Destructor function It have super and sub as its keywords

Advantages of Inheritance : 

Advantages of Inheritance Reusability of code The code written by a user can be reused by other users with or without any modifications. Code sharing The code that is written by a programmer which are common to more than one classes then we can share those codes