X Tutup
Skip to content

Latest commit

 

History

History
58 lines (38 loc) · 1.16 KB

File metadata and controls

58 lines (38 loc) · 1.16 KB

About

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 here

Inheritance vs Composition

These concepts are very similar and are often confused.

  • Inheritance means that the child has 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

X Tutup