Object-Oriented Programming Concepts
If you start programming in Java, you must familiar with Object Oriented Programming Concept. Without knowledge about OOP concepts, you will not be able (elegantly) to design systems in object oriented programming model.
Core Object Oriented Programming Concepts are:
And object relation concepts:
Let’s look into these concepts one by one:
Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes.
A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses.
Inheritance and Polymorphism
More in Inheritance in Java
Polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.
There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.
More in Polymorphism in Java
Encapsulation is the process of hiding information details and protecting data and behavior of an object from misuse by other objects. In Java, encapsulation is done using access modifiers (public, protected, private) with classes, interfaces, setters, getters.
More in Encapsulation in Java
Payable (Interface) and Staff (Abstract)
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.
More in Abstraction in Java.
In association, relation between two separate classes establishes through their objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
Let’s take an example of Customer and Order. One customer can associate with multiple orders. It's represent one-to-many relationship.
Composition and Aggregation are the two forms of association.
Aggregation is a specialized form of Association where all objects have their own life cycle, where the child can exist independently of the parent. Aggregation is also called a “Has-a” relationship.
Let’s take an example of Class (parent) and Student (child). Delete the Class, Student objects will not be destroyed and still exists.
Composition is a restricted form of Aggregation in which two classes are highly dependent on each other. There's a strong life-cycle dependency between the parent and child. The parent cannot exist without the child, and the child has no meaning without the parent.
Let’s take an example of Car (parent) and Engine (child). A car needs engine to run. Although engine may exist as separate parts, detached from a specific car, but it's no use without the parent (car).