Object-Oriented Programming Concepts in Java
OOP is a fundamental programming concept that includes the concepts of abstraction, encapsulation, inheritance, and polymorphism. When comparing earlier programming methods, Reusability, maintainability, and extensibility, of code are the main advantages of OOP concepts.
Ok…Now, Let’s move on with further clarifications.
Inheritance
Inheritance is a core concept of object-oriented programming and eliminates redundant code. Therefore, we can use inheritance for method overriding and code reusability. Sub class and super class are the two main important terms, we use in inheritance in Java.
Super class- base/parent class where a subclass inherits the features.
Sub class- an extended class that inherits the other classes.
Single Inheritance
Output
If we change the String brand;
variable in the super class as private String brand;
The sub class called NewPhone will not be allowed to access the variable called brand.
Hierarchical Inheritance
Output
Multilevel Inheritance
A class inherits properties from a class that again has inherited properties.
Output
Encapsulation
Encapsulation means the bundling of variables and methods inside a class. Furthermore, it can achieve data hiding, and getters and setters are provided. A completely encapsulated class can be created in Java by making all the class variables private. Data hiding, Increased flexibility, and Reusability are some advantages of encapsulation.
🔮🧸Animal Class
🐶Dog Class
🎯Demo Class
Output
Polymorphism
Having many forms is the simple definition of polymorphism. When considering the real world, one person can play different roles. For example, a person at the same time is a brother, father, husband, or friend. But, the behavior will be changed according to the situation. This is the meaning of polymorphism. There are two types of polymorphism
Compile time polymorphism(static polymorphism)
The method used to create this form of polymorphism is function overloading. Methods will be overloaded when there are multiple methods with the same name but different parameter lists(methods with different method signatures).
Runtime polymorphism(dynamic polymorphism)
It is a procedure by which a function call to an overridden method is handled at runtime. By using Method Overriding, this kind of polymorphism is accomplished. Method overriding is the process of declaring a method in a subclass that already exists in the parent class. Constructors, static methods, and final methods cannot be overridden. Overridden is allowed when the instance methods are inherited by the child class.
Output:
Polymorphism using method overriding
Output:
Polymorphism using method overloading
Output:
Abstraction
The process of hiding specific details and showing only essential information to the user is data abstraction. The abstract keyword (non-access modifier), is used for methods and classes. Abstract classes cannot be used to create objects; abstract methods can only be used inside an abstract class. But, non-abstract classes can be contained inside the abstract class. These abstract methods don’t have a body and the body is provided by the subclass in inherited form. Abstract classes should be extended and the abstract methods should be overridden.
Output
All 4 concepts of object-oriented programming have been discussed using examples and code snippets. I hope you gained some knowledge by going through this article.
Thank you…..!!😎