Você está na página 1de 14

Introduction

As we all know reusability is a feature of OOPS.


Java also supports this concept. This is done by
creating new classes reusing the properties of
existing ones. The mechanism of deriving a new
class from an old class is called Inheritance.
The old class is known as Base Class or Super
Class or Parent class & the new class is known as
Subclass or Derived class or Child class
Introduction To
Inheritance
Inheritance is a compile-time mechanism in Java that
allows you to extend a class (called the base class or
superclass) with another class (called the derived
class or subclass) . Inheritance is used for 2 purposes:

1. Class Inheritance
2. Interface Inheritance
Class Inheritance
1. It is used to create a new class as an extension of
another class.
2. Its purpose is of Code Reuse
3. The derived class inherits the public methods and
public data of the base class.
Interface
Inheritance
1)Create a new class to implement the methods
defined as part of an interface for the purpose of
subtyping.
2)Java supports multiple inheritance.
3)In Java Multiple Inheritance can be achieved
through use of Interfaces by implementing more
than one interfaces in a class.
Examples
Class Inheritance
Public class derived-class-name extends base-class-name
{

Interface Inheritance
public class class-name implements interface-name
{

}
Forms of
Inheritance
1) Single Inheritance
2)Multiple Inheritance
3)Hierarchical Inheritance
4)Multilevel Inheritance
Single Inheritance
When a subclass is derived simply from it's parent
class then this mechanism is known as simple
inheritance. In case of simple inheritance there is
only a sub class and it's parent class. It is also
called single inheritance or one level inheritance
Example of Single
Inheritance
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");
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.
Example of Multiple
Inheritance
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");
    }
}

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();
    }
}
Super Keyword
As the name suggest super is used to access the
members of the super class. It is used for two
purposes in Java.

A)The first use of keyword super is to access the


hidden data variables of the super class hidden by
the sub class.
Example
Suppose class A is the super class that has two instance
variables as int a and float b. class B is the subclass
that also contains its own data members named a and b.
then we can access the super class (class A) variables a
and b inside the subclass class B just by calling the
following command.
super.member;
class A{
  int a;
  float b;
  void Show(){
System.out.println("b in super class: 
 " + b);
  }}
class B extends A{
  int a; 
  float b;
  B( int p, float q){
    a = p;
    super.b = q;
  }
  void Show(){
    super.Show();
System.out.println("b in super class:" + super.b);
System.out.println("a in sub class:" + a);
  }

  public static void main(String[] args){
    B subobj = new B(1, 5);
    subobj.Show();
  }
}

Você também pode gostar