Computersight > Programming > Java

Inheritance in Java

Inheritance is the fundamental concept of object oriented programming.It makes programming less complicated and more powerful. It is the first step to an efficient object oriented programming.

Introduction

Inheritance is a fundamental concept of Object Oriented programming.

The concept of inheritance in programming is very much similar to real world concept. Inheritance as the word suggest is to inherit, derive some or all the properties of the parents.

It gives meaning to the concept of Object oriented programming.

We will try to understand the concept of inheritance in Java.

For experienced C++ programmers or those coming from another object oriented language like small talk, this concept will seem largely familiar, but there are many differences between how inheritance is implemented in Java and how it is done in C++ or in other object oriented languages.

Concept

We take an eg. of a class named Employee.


Class Employee
{
int i,j;
void showij()
{
System.out.println(“ I and j :”+I+””+j);
}
}

This is a simple class named Employee.

It contains two variables I and j;

There is a method named showij. This method is printing the values of I & j.

Now look at the following program


Class EmpB extends Employee
{
int k;
void showk()
{
System.out.print(“k:”+k);
}

void sum()
{
System.out.println(“I+j+k:”+(I+j+k));
}
}

There are few points to note in this part of program:

  • Keyword ‘extends’
    This keyword is responsible for inheritance

    Syntax
    class <sub class name> extends <super class name>

  • System.out.println(“I+j+k:”+(I+j+k));
    Here we can see that variables I,j of class Employee are accessed directly.
    This is possible because we inherited class Employee with the help of keyword extends thus all the methods and variables are inherited and can be used in the base class.

Now we will write the main class


Class InheritanceDemo
{
public static void main(String args[])
{
Employee ob1=new Employee();
EmpB ob2=new EmpB();

// The superclass can be used by itself

ob1.i=10;
ob2.j=20;
System.out.println(“contents of super class:”);
Ob1.showij();


/* The subclass has access to all public members of its superclass. */

ob2.i=7;
ob2.j=8;
ob2.k=9;
System.out.println(“ Contents of subclass:”);
ob2.showij();
ob2.showk();

System.out.println();

System.out.println(“Sum of I, j and k in sub class:”);
ob2.sum();
}
}

The all three classes are compiled and the output is


Contents of super class:
I and j: 10 20

Contents of sub class:
I and j: 7 8
K: 9

Sum of I,j and k in sub class
I+j+k: 24

The points to note are:

ob2.i=7;

ob2.j=8;

Here we are using the object of the base class to access variables of super class.

This is possible due to inheritance.

Thus we can see, the subclass EmpB includes all the members of it’s superclass Employee. This is why ob2 can access I and j and call showij().

Also , inside sum(), I and j can be referred to directly, as if they were part of EmpB.

Here Employee is a superclass but is also a completely independent stand-alone class.

Note: to C++ programmers.

The basic difference between inheritance in C++ and Java is that Java does not support multiple inheritance. I.e. only one class can be inherited at a time. To overcome this limitation Java provides a feature known as packages.

1
Liked It
I Like It!
Related Articles
Object Oriented Programming Strategy (OOPS)  |  3d Engine Construction and Design Workshop: Lesson 3 - Object Oriented Design
Comments (2)
#1 by manoveg, Sep 5, 2006
excellent
#2 by HS, Sep 12, 2006
brilliant
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.