Home Java Language Inheritance
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Inheritance

Advertisement
In this section, you will learn about the inheritance with an example and its description.

Inheritance

     

To know the concept of inheritance clearly you must have the idea of class and its features like methods, data members, access controls, constructors, keywords this, super etc.

As the name suggests, inheritance means to take something that is already made. It is one of the most important feature of Object Oriented Programming. It is the concept that is used for reusability purpose. Inheritance is the mechanism through which we can derived classes from other classes. The derived class is called as child class or the subclass or  we can say the extended class and the class from which we are deriving the subclass is called the base class or the parent class. To derive a class in java the keyword extends is used. To clearly understand the concept of inheritance you must go through the following example.

The concept of inheritance is used to make the things from general to more specific e.g. When we hear the word vehicle then we got an image in our mind that it moves from one place to another place it is used for traveling or carrying goods but the word vehicle does not specify whether it is two or three or four wheeler because it is a general word. But the word car makes a more specific image in mind than vehicle, that the car has four wheels . It concludes from the example that car is a specific word and vehicle is the general word. If we think technically  to this example then vehicle is the super class (or base class or parent class) and car is the subclass or child class because every car has the features of it's parent (in this case vehicle) class.

The following kinds of inheritance are there in java.

  •   Simple Inheritance
  •   Multilevel Inheritance

Pictorial Representation of Simple and Multilevel Inheritance

Simple Inheritance Multilevel Inheritance

Simple 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.     

eg.

class {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}

class 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.
e.g. 

class {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}
class extends A{
  void Showb(){
  System.out.println("B");
  }
}

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

Java does not support 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 but 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.

super keyword

The super is java keyword. As the name suggest super is used to access the members of the super class.It is used for two purposes in java.

 The first use of keyword super is to access the hidden data variables of the super class hidden by the sub class.

e.g. 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;

Here member can either be an instance variable or a method. This form of super most useful to handle situations where the local members of a subclass hides the members of a super class having the same name. The following example clarify all the confusions. 

class A{
  int a;
  float b;
  void Show(){
  System.out.println("b in super class:  " + b);
  }

}

class 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(15);
  subobj.Show();
  }
}

Output:

C:\>java B
b in super class: 5.0
b in super class: 5.0
a in sub class: 1

Use of super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass. This functionality can be achieved just by using the following command.

super(param-list);

Here parameter list is the list of the parameter requires by the constructor in the super class. super must be the first statement executed inside a super class constructor. If we want to call the default constructor then we pass the empty parameter list. The following program illustrates the use of the super keyword to call a super class constructor. 

class A{
  int a;
  int b;
  int c;
  A(int p, int q, int r){
  a=p;
  b=q;
  c=r;
  }
}
  
  class extends A{
  int d;
  B(int l, int m, int n, int o){
  super(l,m,n);
  d=o;
  }
  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();
  }
  }

Output:

C:\>java B
a = 4
b = 3
c = 8
d = 7
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 9, 2007

Ask Questions?    Discuss: Inheritance   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
imran
April 9, 2011
java

it is very helpful site for students.so improve our knowledge.
sajidaqumer
April 11, 2011
inheretance

so interisting
Buyisile
April 19, 2011
Basics in Inharitance

I need help with the basics of inheritace.
sunil kumar
April 21, 2011
java

how can we achieveb the runtime polmorphism by using the virtual functtions in java
Neha gupta
June 2, 2011
hi like roseindia.net site

I am very happy to see this site is very useful in our study it provide us very good study material with great example
Neha gupta
June 2, 2011
i like this site

It is very good site for help us in our studies & provide us very good matter with great example
Rohit Prakashan
July 3, 2011
java

If we want to call the default constructor then we pass the empty parameter list. in inheritance pls explain by using program...........
Kumar Raja
August 20, 2011
Inheritance

A big thankyou to roseindia who provides the such a best tutorial with a suitable example... Regards Kumar Raja, Patna(Parsa)
Rama Deokate
September 9, 2011
core java

Core java is a super language...
Raju K. Darji
September 27, 2011
View Size Of Java Variable

How can we find the size of particular variable or class? Like we use sizeof operator in c++ to find the size of variable.
deepak
October 10, 2011
Thanx

M greatly thanking to this site is quite helpful for the students like me studying last nite b4 the exams thanx a lot......
sudha
November 1, 2011
sir

sir pls send the information about inheritance concept,benifts of inheritance,super&sub classes........
Priya and Sakthi
November 21, 2011
Feedback

Dont use extends keyword in Main class
sindhu
December 15, 2011
inheritance

i want more details.plz give me sir.
bhurani
December 20, 2011
java

it is very helpful to our referrence
Devendra
December 27, 2011
Question

I have 2 classes Summary and Main. in main class i have one button and i want to call the summary class on the click event of main class button becauuse my summary class contain the gui
praveen jindal
January 18, 2012
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(); } }
afif lami
January 28, 2012
encapsulation

good
murugan.D
January 31, 2012
java programming

simple program exercise
sandeep
January 31, 2012
java

good...............
A.NARESH
February 3, 2012
java

in constructors concept what is use of this keyword and with more example programs
MSelvam
February 20, 2012
Like

I Liked It Nice To learn.........
suvabrata saha
February 27, 2012
java

plz send me an example of multiple inheritance
charu thakral
March 6, 2012
java

good
Pushkar
March 8, 2012
java

Good Examplation
Ashish
March 10, 2012
complete java language

i want to learn complete java...............
Amit kumar yadav
March 20, 2012
java

what can we say that java is fully secured? prove it briefly.
anuj poonia
April 3, 2012
java

what is the defferenc between this and super keyward
situ
April 8, 2012
java

well describe
SHETE PRATHMESH PRAKASH
April 19, 2012
java

nice explanation
Manohar kumar Bishwas
May 17, 2012
java

about inheratance
Srimoyi Kundu
May 27, 2012
general

Rosevalley is the best tutorial website for JAVA. I have read all the topics from this website for my BCA Final year exam.... Thanks a ton
ajit
June 3, 2012
some errors during compile

C:\Users\patra>cd desktop C:\Users\patra\Desktop>javac A.java A.java:1: class, interface, or enum expected lass A{ ^ A.java:3: class, interface, or enum expected float b; ^ A.java:4: class, interface, or enum expected void Show(){ ^ A.java:6: class, interface, or enum expected } ^ 4 errors
geethesh babu
June 10, 2012
java, inheritance and interface

without method. how to find result? like.... interface A{ int i=20; void show(); void test(); void display(); } class B implements A { void show() { s.o.p("show"); s.o.p("how are you"); } void test() { s.o.p("test"); } void display() { s.o.p("display"); } } please send the result of this. it has been urgent
mohan
June 21, 2012
sample codings

i want some java sample coding in all topics
SAGAR BANARASE
June 26, 2012
multiple inheritance

multiple inheritances not used in directly in java
Erick Ouko
July 10, 2012
Java arithmetics

I like the way inheritance has been explained, can I get to know more in Method overloading and Method Overriding?
shakhtivel
July 17, 2012
doubt

put output for all progms
karthigairani s
July 22, 2012
inheritance in java and types

Its very useful of the Engg Students.
gem
July 23, 2012
blood

nice one
murugavel
August 2, 2012
nice

very useful.thanks
khilendra
August 25, 2012
inheritance

helpful the all the details.
anuragsinha
September 14, 2012
c++

hi you help me c++ facebook id =>anuragsinha1989@gmail.com
rushik sutariya
October 3, 2012
rushik sutariya

which is the easy and better JAVA or PHP?
niro
October 3, 2012
no

no
yousuf khan
October 7, 2012
Error inheritance

Error: Could not find or load main class B
yousuf khan
October 7, 2012
Error inheritance

Error: Could not find or load main class B
motilal giri $ sagarika
November 7, 2012
java

easy to understand for student
pk
November 27, 2012
inherirtance

good
Ajin Joy
December 3, 2012
Inheritance

This is very simple and good concept of inheritance with suitable example. But Why don't java supports Other types of inheritance like c++ ?
Ajin Joy
December 5, 2012
Simple Inheritance

class A { int x; int y; void get(int p, int q) { x=p; y=q; } void Show() { System.out.println(x); System.out.println(y); } } //B derived from class A class B extends A { void display() { System.out.println("B"); } public static void main(String args[]) { B a = new B(); //Created object a from child class B a.get(5,6); a.Show(); a.display(); } }
pastina
July 24, 2013
thank you

nice.......it's very easy understanding in the program...thank you
Azam khan
August 30, 2013
Java

Its so intresting.site. .
mary
July 28, 2013
progaramming paradgim

I am very happy to see the site.it is very useful me.
Anoj
July 29, 2013
Java

Simply Described the concept of inheritance.... Thanx
DEEPU
July 30, 2013
inheritance

It is very very useful for absolute beginners.Try to make use of it.
THEERTHAN D S
August 2, 2013
inheritance

i did not get more information about inheritance just had two types of inheritance ,there are five types i am not at all happy with this sit.
M. Fermin Timoth
August 2, 2013
Inheritance

Very Interesting.. Got good ideas on inheritance.. Thank you...
Niraz
September 13, 2013
JAVA

Thank you so much for this,really helped alot
niketh
July 9, 2011
easy example of inheritence

class Sample extends Demo { public void test() { System.out.println("Hello"); } public static void main(String[] arg) { Sample s=new Sample(); s.test(); s.add(); } } class Demo { public void add() { System.out.println("Add Method"); } }
DMCA.com