Fundamentals of Object Oriented Programming
OOP is a programming method that is based on the object. Small think from OOP is an object collection that interacts with each other and exchange information. The main concept of OOP is,
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Abstraction
- Abstraction is a very important element of object-oriented software development and is used in the design phase to find a suitable hierarchy for a set of classes.
- Refers to the process of representing essential features without including background details or explanations.
- Abstraction refers to the ability to make a class abstract in OOP.
- Cannot be instantiated.
- All other functionality of the class still exists and its fields, methods, and constructors are all accessed in the same manner.
- Cannot create an instance of the abstract class.
Encapsulation
- The wrapping of data functions into a single unit (class).
- Enable data hiding and Information hiding. Data hiding is a method used in OOP to hide information within computer code.
- Encapsulation is one of the most important characteristics of an OOP.
Read also : Paradigm of Object Oriented Programming
Inheritance
- The “is-a” relationship, and the specialization that accompanies it, are modeled in object-oriented languages via inheritance.
- Classes can inherit from other classes. Base inheritance in a program on the real-world things being modeled.
Inheritance in Java is
- Java is a pure object-oriented language.
- All code is part of some class.
- All classes, except one, must inherit from exactly one other class.
- If a class header does not include the extends clause the class extends the Object class by default. The object is an ancestor to all classes and it is the only class that does not extend some other class.
- A class extends exactly one other class. Extending two or more classes is multiple inheritances. Java does not support this directly, rather it uses Interfaces.
Polymorphism
- Another feature of OOP.
- Literally “having many forms”.
- Object variables in Java are polymorphic.
- Object variables can refer to objects or their declared type AND any objects that are descendants of the declared type.
In Java polymorphism is mainly divided into two types:
- Compile time Polymorphism. It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading. But Java doesn’t support Operator Overloading.
- Runtime Polymorphism. It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
Relationship Between Two Classes
- Object-oriented programming leads to programs that are models. Sometimes models of things in the real world and sometimes models of contrived or imaginary things.
- There are many types of relationships between the things in the models, such as :
- chess piece has a position
- chess piece has a color
- chess piece moves (changes position)
- chess piece is taken
- chess piece is taken a rook is a type of chess piece
Read also : Exception on Object-Oriented Programming with Java
The “has-a” Relationship
- Objects are often made up of many parts or have sub data.
- chess piece: position, color
- die: result, number of sides
- This “has-a” relationship is modeled by composition. The instance variables or fields internal to objects.
- Encapsulation captures this concept.