Object Oriented Programming Concepts with Java 🍵
Object Oriented Programming is a concept, based on Objects. These are the pillars of Object Oriented Programming which are Classes, Objects, Encapsulation, Inheritance, Polymorphism and Abstraction. Do you know how to explain these pillars to an interviewer with real world examples ?? Here it is ..
Class
In object-oriented programming, a class is a blueprint from which individual objects are created. In Java, everything is related to classes and objects. Each class has its methods and attributes that can be accessed and manipulated through the objects.
Object
In object-oriented programming, an object is an entity that has two characteristics (states and behavior). Some of the real-world objects are book, mobile, table, computer, etc. A class has the methods and attributes, these are accessed through an object. Thus, an object is an instance of a class.
Encapsulation
Encapsulation is a technique of limiting the user's direct interaction with system's internal data and grant the access with own set of rules. In short we can say wrapping the data in to a single unit is encapsulation.
Imagine the Laptop you work. What will happen if you could directly access your Internal hardware items like Ram, Mother-board or Display. You might break down the entire system unless your lap-top's internal systems are hidden from you. To prevent this, the laptop uses Encapsulation:
In Java we do the same by adding the private access modifier to attributes, then the access level is restricted only to that class. So to access this attributes in a java class we have to use special public methods like getters and setters. Inside these methods we can write our rules and limit the direct access for the data
Inheritance
In inheritance we can reuse the functionalities of one classes in another new class. In the concept of inheritance, there are two terms base (parent) class and derived (child) class. When a class is inherited from another class, it obtains all the properties and behaviors of the base class and we can add new features in the derived class as well.
Do you know the first laptop invented in 1975 was heavier than today. See the difference of Laptops from time to time in the below pictures. The Modern laptops have the features of old laptops and new features of its own. So this is the concept of inheritance.
In programming with Java we use keyword extends to inherit features of one class to another which lets the derived class to use the features of base class with adding new features. There are types of Inheritance in java.
Types of Java Inheritance
In Java, there are mainly three types of inheritances Single, Multilevel, and Hierarchical. Java does not support Multiple and Hybrid inheritance due to the Diamond problem. The Diamond Problem is a confusion about which parent's method to use when a child has two parents that both came from the same grandparent. Java avoids this by banning multiple inheritance of classes.
Recommended by LinkedIn
Polymorphism
Polymorphism means having one interface with many forms. and it comes from the Greek words poly (many) and morph (forms). This means one entity can take many forms.
How do we unlock a laptop. In some laptops we use a Password, Pin or a Finger Print to unlock. Here what we need to do is unlocking the laptop but we use many methods to achieve that. This is Polymorphism in real world.
There are two types of Polymophsim as mention in the above picture.
1. Compile-Time Polymorphism
Compile-Time Polymorphism in Java, also known as static Polymorphism is achieved mainly through method overloading, where multiple methods with the same name but different parameter lists.
Method Overloading
Method overloading means defining multiple methods with the same name but different parameter lists. The method call is resolved at compile time based on the arguments passed.
2. Runtime Polymorphism
Runtime Polymorphism in Java is also known as dynamic method dispatch. It occurs when a method call is resolved at runtime, and it is achieved using method overriding.
Method Overriding
Method overriding occurs when a subclass provides its own implementation of a method already defined in its super class. The method must have the same name, parameters, and return type.
Abstraction
Abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.
Imagine sending an email from a laptop, here user doesn't need to know how this is really happening instead they need only to send the email to the destination. This is Abstraction hiding the complex implementation details from the user and show only the essential ones.
Abstract classes and interfaces in Java are both used to achieve abstraction, but they serve different design purposes. While they may look similar at first glance, the way classes interact with them is fundamentally different.
Difference Between Abstract Class and Interface in Java