Four Pillars of OOP
What are the 4 Pillars of OOPs
- Inheritance - Encapsulation - Polymorphism - Abstraction
Abstract Class vs Interface
- Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior. - Variables declared in a Java interface is by default final. An abstract class may contain non-final variables. - Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc.. - Java interface should be implemented using keyword "implements"; A Java abstract class should be extended using keyword "extends". - An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces. - A Java class can implement multiple interfaces but it can extend only one abstract class. - Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
Abstraction
Abstraction is a process of hiding the implementation details from the user. Оnly the functionality will be provided to the user. In Java, abstraction is achieved using abstract classes and interfaces
Interface
An interface in the Java programming language is an abstract type that is used to specify a behavior that classes must implement. - Any variables will be implicitly public, static, and final. - Implicity abstract - Uses "implements" keyword - Classes can implement multiple interfaces
Encapsulation
Keeping details (like data and procedures) together in one part of a program so that programmers working on other parts of the program don't need to know about them.
Inheritance
Shared qualities (States / Behaviors) between classes with sub/superclass relationships. Reduces code redundancy.
Method Overloading
The ability to define two or more different methods with the same name but different method signatures.
Covariance
The ability to refer to a subclass as an instance of its superclass (or an implemented interface) - Example Class Cat extends Animal Animal a1 = new Cat(); (Can not be Cat a1 = new Animal(); )
Method Overriding
When a child class overwrites a method inherited from a parent class.
Abstract Class
a class that cannot be instantiated - used "abstract" keyword in class declaration - allows for the use of abstract keyword to modify any of its methods - can include instance variables - Class can only extend one class - Methods must be abstract
Object Oriented Programming (OOP)
a style of programming that involves representing items, things, and people as objects rather than basing the logic around actions.
Public Access Modifier
all class instances can see it
Default Access Modifier
anything in the same package can access it
Access Modifiers
control where a type or type member can be used - Private - Public - Protected - Default
Private Access Modifier
limited access to data or methods to only objects of that particular class.
Polymorphism
the ability for classes and methods to behave differently depending on how they are being used - Covariance - Method Overriding - Method Overloading
Protected Access Modifier
the class instances it is in and all derived class instances can see it