Tracks
/
Java
Java
/
Syllabus
/
Inheritance
In

Inheritance in Java

1 exercise

About Inheritance

Inheritance

Java supports inheritance as its core functionality and then to achieve a lot of OOPs principles like abstraction using inheritance.

A class can extend another class using extends keyword and can inherit from an interface using implements keywords.

Access Modifiers

The access modifiers define rules for the member variables and methods of a class about their access from other classes (or anywhere in the code).

There are four access modifiers:

  • private
  • public
  • protected
  • default (No keyword required)

You can read more about them in this article

Inheritance vs Composition

These concepts are very similar and are often confused.

  • Inheritance means that the child has an IS-A relationship with the parent class.
interface Animal() {
    public void bark();
}

class Dog implements Animal {
    public void bark() {
        System.out.println("Bark");
    }
}

Here, Dog IS-A Animal

  • Composition represents a HAS-A relationship.
interface Engine {

}

class Car {
    private Engine engine;
}

Here, Car HAS-A Engine

Edit via GitHub The link opens in a new window or tab

Learn Inheritance