Inheritance in Java with an example

In Java, inheritance is a fundamental concept of object-oriented programming (OOP), allowing a new class to be based on an existing class, inheriting all of its properties and methods while adding new features or functionality. In Java, inheritance is accomplished through the use of the extends keyword.

The following is the general form for inheritance implementation in Java:

class ParentClass {
    // member variables and methods

class ChildClass extends ParentClass {
    // additional member variables and methods

The "class" keyword is used to define the parent class, and then the class's name is specified. Variables and methods can be defined as members of this class. Then, the child class is defined using the "class" keyword followed by the class name and the "extends" keyword followed by the parent class's name.

In addition to inheriting the member variables and methods from the parent class, the child class can define additional member variables and methods.

Once the child class is defined, objects of the child class can be created and its member variables and methods can be used, in addition to those inherited from the parent class.

Java inheritance example

Consider the following program as an example demonstrating inheritance in Java.

Java Code
 * MyClass is the parent class,
 * which has a name and age attribute,
 * and a speak() method to output the name and age.
public class MyClass {
    private String name;
    private int age;

    public MyClass(String name, int age) { = name;
        this.age = age;

    public void speak() {
        System.out.println("My name is " + name + " and I am " + age + " years old.");

 * MyStudent is a child class of MyClass, 
 * which inherits the name and age attributes and the speak() method, 
 * and also has a major attribute and a study() method.
public class MyStudent extends MyClass {
    private String major;

    public MyStudent(String name, int age, String major) {
        super(name, age);
        this.major = major;

    public void study() {
        System.out.println(name + " is studying " + major + ".\n");

 * MyTeacher is a child class of MyClass, 
 * which also inherits the name and age attributes and the speak() method, 
 * and has a subject attribute and a teach() method.
public class MyTeacher extends MyClass {
    private String subject;

    public MyTeacher(String name, int age, String subject) {
        super(name, age);
        this.subject = subject;

    public void teach() {
        System.out.println(name + " is teaching " + subject + ".");

 * InheritanceExample is the main class, 
 * which creates objects of the child classes MyStudent and MyTeacher and calls their methods.
public class InheritanceExample {
    public static void main(String[] args) {
        MyStudent myStudent = new MyStudent("Lucas", 22, "Java");

        MyTeacher myTeacher = new MyTeacher("William", 29, "Python");
My name is Lucas and I am 22 years old.
Lucas is studying Java.

My name is William and I am 29 years old.
William is teaching Python.

A parent class (MyClass) is extended by two child classes (MyStudent and MyTeacher) in this example. The name and age attributes, as well as the speak() method, are passed down from the parent class to both child classes.

MyStudent also has an additional attribute called major, and its study() method returns the student's name and major. The name of the teacher and the subject they are teaching are output by the methods teach() and an additional attribute called subject in the MyTeacher class.

Objects of both child classes are created and their respective methods are called in the main method of the InheritanceExample class. The inherited speak() and study() methods are called by the MyStudent object, while the inherited speak() and teach() methods are called by the MyTeacher object.

Advantages of inheritance in Java

Disadvantages of inheritance in Java

Java Online Test

« Previous Tutorial Next Tutorial »

Liked this post? Share it!