Friday 3 February 2017

Inheritance in Java


                                                     
Inheritance in Java


Inheritance

Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification.

To inherit a class , you simply incorporate the definition of one class into another by using extends keyword.

Program:

//To illustrate inheritance
Class A
{
 int i=10,j=20;

void showij( )
{
System.out.println(" i and j" + i + " " + j);
 }
}
Class B extends A
{
  int k=30;
void showk ( )
{
 System.out.println( " k=" + k);
}
void sum ( )
{
 System.out.println ("i+j+k =" + (i+j+k));
 }
}
class InheritanceDemo
{
  public static void main ( String args [] )          Output:
{                                                                           i and j = 10  20
  B b1 = new B( );                                                    j = 30
  b1.showij ();                                                       i+j+k = 60
 b1.showk();
 b1.sum ( );
}
}

Super Keyword

Whenever a subclass needs to refer to its immediate superclass , it can do so by the keyword  Super.

It has 2 general forms:

First , it is used to call the base class constructor. This can be done by following form :

   super( parameters_list);

Second , It is used to access a member of the superclass that has been hidden by a member of a subclass. This can be done by :

  Super.member;

        Where member can be either a method or an instance variable.


                                                                           By: Knowledge Bits


No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *