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.
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
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