Inheritance –
- It is a concept where one class acquires the property of another class.
- Above all, it supports the concept of re-usability.
- Inheritance represents the IS-A relationship. So it is also known as a parent-child relationship.
- To implement inheritance, we use the “extends” keyword.
- It helps to increase the functionality by making a new class that derives from an existing class.
- In other words, the subclass is one that inherits from another class, and the superclass is the one from which it inherits.
- Syntax – class Sub-className extends Super-className { //methods and fields }
- Example –
class Calculate {
protected int num1 = 5;
public void printSum() {
System.out.println(num1);
}
}
class Add extends Calculate {
private int num2 = 10;
public static void main(String args[]) {
Add add = new Add();
add.printSum();
System.out.println("Total = " + add.num1 + add.num2);
}
}
- Inheritance is of 4 types –
- Single Inheritance: In Single Inheritance, a class inherits the properties of another class.
- Multilevel Inheritance: In Multilevel Inheritance, a class is derived from another class, which in return is also derived from another class, i.e., a class having more than one parent class but at different levels.
- Hierarchical Inheritance: In Hierarchical Inheritance, a class has more than one child class.
- Hybrid Inheritance: In Hybrid Inheritance, a combination of multiple inheritance and multilevel inheritance is seen. It means there will be multiple parent classes and multiple child classes.
Encapsulation –
- It is a concept of binding of data and methods within a single unit.
- It acts as a protective shield. As a result, the outside world cannot access the data.
- Above all, it is a way to achieve data hiding + Abstraction together.
- Therefore, the other classes are not able to access the data through their private data members.
- Since it is declared as private, implementation details are also hidden from the end-user.
- For instance, a java class is an example of encapsulation as it wraps up the properties and methods together.
- Example –
public Class Person {
private String name;
public String getName() {
return name;
}
public void setName(String newName) {
this.name = newName;
}
}
Abstraction –
- It is a concept where it displays only the essential information and hides the details.
- Therefore, it helps to reduce programming complexity and effort.
- We can achieve Abstraction using abstract classes and interfaces.
- An abstract class only contains one or more abstract methods(method with nobody).
- A class that extends an abstract class can override its abstract methods.
- An interface is a reference type that determines the model of the class that implements it. All methods declared in it are abstract by default.
- To implement an interface, we use the “implements” keyword.
- Example –
// Abstraction by abstract class
abstract class Animal {
public abstract void display() {
System.out.println("Animal");
}
}
class Cat extends Animal {
public void display() {
System.out.println("Cat");
}
}
class MainClass {
public static void main(String args[]) {
Cat cat = new Cat();
cat.display();
}
}
// Abstraction by interface
interface Vehicle{
void display();
}
class Car implements Vehicle {
public void display() {
System.out.println("Car");
}
}
class Bike extends Vehicle {
public void display() {
System.out.println("Bike");
}
}
class MainClass {
public static void main(String args[]) {
Vehicle car = new Car();
car.display();
Vehicle bike = new Bike();
bike.display();
}
}
Polymorphism –
- To achieve Polymorphism, satisfy below three conditions –
- An object can exist in many forms.
- All forms should reside within a single unit.
- Which form will be executed depends on the input.
- It is mainly of two types –
- Compile Time/ Static binding – Method Overloading
- Method overloading is when multiple methods with the same name but different signature exist in a class. A different signature means a different number of parameters or different parameter datatype or a different sequence of parameters.
- Run Time/ Dynamic binding – Method Overriding
- Method Overriding is redefining a method of the superclass in a subclass.
- Compile Time/ Static binding – Method Overloading
- Example –
class Animal {
public void display() {
System.out.println("Animal");
}
}
class Cat extends Animal {
public void display() {
System.out.println("Cat");
}
}
class Dog extends Animal {
public void display() {
System.out.println("Dog");
}
}
class MainClass {
public static void main(String args[]) {
Animal animal = new Animal();
Animal cat = new Cat();
Animal dog = new Dog();
animal.display();
cat.display();
dog.display();
}
}
What is runtime polymorphism?
Runtime Polymorphism is the dynamic binding that happens at runtime. For instance, method overriding. Method Overriding is redefining a method of the superclass in a subclass.
What is method overloading and method overriding?
- Method overloading is when multiple methods with the same name but different signature exist in a class. A different signature means a different number of parameters or different parameter datatype or a different sequence of parameters.
- Method Overriding is redefining a method of the superclass in a subclass.
What is multiple inheritance? Is it supported in Java?
Multiple inheritance is where one can have multiple parent classes but just one child class.
This concept is not supported in Java. Because it leads to ambiguity, so this type of inheritance can only be achieved through the use of interfaces.