Home Java Language Inheritance in Java with example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Inheritance in Java with example

Advertisement
Here we are going to discuss Inheritance in Java with a simple example. Inheritance is a Object Oriented Programming concepts, which allows to extend a superclass/parentclass to a subclass. Interface in java is used for class inheritance and interface inheritance. extend and implement keywords are used for inheritance in Java.

Here we are going to discuss Inheritance in Java with a simple example. Inheritance is a Object Oriented Programming concepts, which allows to extend a superclass/parentclass to a subclass. Interface in java is used for class inheritance and interface inheritance. extend and implement keywords are used for inheritance in Java.

Inheritance is used in java for the reusability of code.

The extends keyword indicates that the features of a superclasses class is inherited by its subclass. Inheritance allows one class to inherit from another class by using extend keyword and every class is a subclass of java.lang.Object.java.

Java supports three types of inheritance, they are:

  • Simple inheritance
  • Multilevel inheritance.
  • Hierarchical inheritance

Note:

1. subclass cannot inherit members that are declared private in superclass. However, these can be accessed indirectly.
2. In other packages Subclass cannot inherit the default members of the superclass but in the same package it can access them by their simple names.
3. subclass do not inherit constructors and initializer blocks as they are not members of a class.
4. subclass can extend only single superclass

Simple inheritance: The simple inheritance is superclass inherited by subclass. In simple inheritance one class extend one class only.

Example of simple inheritance:

class SimpleInherit {
	int ab;
	int cd;

	int get(int pq, int qr) {
		ab = pq;
		cd = qr;
		return(0);
	}

	void Show() {
		System.out.println(ab);
	}
}

public class SimpleInheritText extends SimpleInherit {
	public static void main(String args[]) {
		SimpleInherit text = new SimpleInherit();
		text.get(25,26);
		text.Show();
	}
     }

Output:

Multilevel inheritance: The Multilevel inheritance is inheriting features of more than one class into a single class. Multiple inheritance in Java can be achieved by interface. In this example we have used multilevel inheritance to create one-to-one ladder. "super" keyword is used to inherit features of a superclass into subclass in Multilevel inheritance.

Example of Multilevel inheritance:

class College {
    public void branch(String str) {
        System.out.println("Branch is MCA ");
        

    }
}

class Bipul extends College {
    public void branch(String str) {
        System.out.println("Bipul Branch is BAC:");

    }
}

class Gopal extends College {
    public void branch(String str) {
        System.out.println("Gopal Branch is MBA: ");

    }
}

class Ankit extends College {
    public void branch(String str) {
        System.out.println("Ankit Branch is B.Tech: ");

    }
}

public class Student {
    public static void main(String[] args) {
        Bipul b = new Bipul();
        Gopal c = new Gopal();
        Ankit d = new Ankit();
         b.branch("MCA");
         c.branch("BCA");
         d.branch("MBA");
    }
}

Output:

Hierarchical inheritance: Hierarchical inheritance, a superclass is extended by more than one class in a tree structure.

Syntax of hierarchical inheritance:

class A//A is superclass class
{
body declaration 
}
class B extends A
{
//body declaration 
}
class c extends B
{
'//body declaration 
}
class D
{
public static void main (string args[])
{
// body declaration 
}
}

Download Source Code

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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: May 15, 2013

Related Tutorials

Ask Questions?    Discuss: Inheritance in Java with example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com