Inheritance In Java Example

Chapter: Inheritance Last Updated: 20-07-2016 03:15:42 UTC

Program:

            /* ............... START ............... */
                
class Parent {
	public void p1() {
		System.out.println("Parent method");
	}
}

public class JavaInheritanceExampleChild extends Parent {
	public void c1() {
		System.out.println("Child method");
	}

	public static void main(String[] args) {
		JavaInheritanceExampleChild cobj = new JavaInheritanceExampleChild();
		cobj.c1(); // method of Child class
		cobj.p1(); // method of Parent class
	}
}
                /* ............... END ............... */
        

Output

Child method
Parent method

Notes:

  • Inheritance is the process of reusing the code used in similar class, or a class that is derived from another class is called a subclass.
  • Inheritance is one of the feature of Object-Oriented Programming (OOPs).
  • The derived class is also called subclass and the base class is also known as super-class.
  • Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance.
  • Syntax : class Subclass-name extends Superclass-name.

Tags

Inheritance In Java, Java

Similar Programs Chapter Last Updated
Java Final Class Inheritance 02-08-2017
Java Final Method In Inheritance Inheritance 02-08-2017
Java Multilevel Hierarchical Inheritance Inheritance 01-08-2017
Java Super To Invoke Parent Class Method Inheritance 31-07-2017
Java Super To Invoke Parent Class Constructor Inheritance 30-07-2017
Static And Dynamic Binding In Java Inheritance 24-03-2017
Java Overloaded Method To Print Array Inheritance 03-09-2016
Dynamic Method Dispatch In Java Example Inheritance 19-06-2016
Abstract Class In Java Example Inheritance 20-07-2016
Method Overriding In Java Example Inheritance 20-07-2016
Method Overloading In Java Example Inheritance 20-07-2016
Inheritance And Constructors In Java Inheritance 20-07-2016
Multilevel Inheritance In Java Example Inheritance 22-09-2018
Super() In Java Example Inheritance 14-07-2016

1