What are Java parent and child classes in Java?

Java 8Object Oriented ProgrammingProgramming

Java supports inheritance, an OOPs concept where one class acquires the members (methods and fields) of another.

You can inherit the members of one class from another, use the extends keyword as:

class A extends B{}

The class which inherits the properties of other is known as child class (derived class, sub class) and the class whose properties are inherited is known as parent class (base class, superclass class).
Following is an example which demonstrates inheritance. Here, we have two classes namely Sample and MyClass. Where Sample is the parent class and the class named MyClass is the child class.

Example

Live Demo

class Sample{
   public void display(){
      // Java Arrays with Answers
      System.out.println("Hello this is the method of the super class");
   }
}
public class MyClass extends Sample {
   public void greet(){
      System.out.println("Hello this is the method of the sub class");
   }
   public static void main(String args[]){
      MyClass obj = new MyClass();
      obj.display();
      obj.greet();
    }
}

Output

Hello this is the method of the super class
Hello this is the method of the sub class


raja
Published on 05-Jan-2018 12:45:09
Advertisements