4 Pillars of Object Oriented Programming

07 Jul 2022

Encapsulation

Inheritance

Hierarchical Inheritance

Multi-level Inheritance

Abstraction

Abstraction is a process of creating generalized class with some common features that can be inherited by specialized classes. When there is an obvious situation to implement a method in a

If method overriding is compulsory in a specialized class innherited from a generalized class then there is no point of writing a complete implementation of a method in the generalized class. In this case, we declare an abstract method in the generalized class which is not implemented.

Abstract Class

Polymorphism

Interface in Java

Interfaces in Java represents pure abstract class. Interfaces allow us to wrtie a common specification or outline that can be implemented by various implementation providers.

(Prior to Java 8) Java interfaces can have only abstract methods.

A specification of method signatures.

Q: Can Java interfaces have variables? A: Yes, but the variables have to be pulblic, static and final (constant)

Q: Can interface extend another interface? A: Yes, An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a comma-separated list of all the interfaces that it extends.

Method signature indicates a method indication which reveals access specifier, return type, name of the method and parameter list (if any).

Interface declaration syntax:

interface Calculator{

	public abstract double add(double x, double y);
	public abstract double subtract(double x, double y);
	public abstract double divide(double x, double y);
	public abstract double multiply(double x, double y);

}

Q: Can a class extend an interface? A: No, class can implement an interface.

s