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