The OOPS concepts in Java are
1. Abstraction (Many books don’t discuss about this)
2. Encapsulation
3. Inheritance
4. Polymorphism
1. Abstraction :: Hiding the implementation details
In Java, we achieve this with the help of a class.
Real time example :: a car or a TV remote etc.
When we start a car, we are least bothered about the internal functionality what makes the car to start.
The same way, when we are using the power button or another buttons on a TV remote, we are not bothered how the actual functionality is happening with the help of circuits inside it.
Java example :: a class
Consider we create a Car class and it have the behavior speed(), color() etc. For any object which uses these methods are lease bothered the implementation of these methods and they just care about the functionality.
2. Encapsulation :: Hiding the data
In Java, we achieve this with the help of access modifiers.
Real time example :: Medical capsule
One drug is stored in bottom layer and another drug is stored in Upper layer these two layers are combined in single capsule.
Java example :: data (fields) in a class are marked as private and we use public methods to access these data(fields).
3.Inheritance :: using the methods of a (super)class in another (sub)class.
In Java, we achieve this with the help of sub classing (i.e using extends keyword).
Real time example :: Father and a son
Java example ::
Inheritance allows a class to be a subclass of a superclass, and thereby inherit public and protected variables and methods of the superclass.
4. Polymorphism :: many forms. A single method(name) is functioning differently.
In Java, we achieve this with the help of overriding and Interfaces. And not because of overloading, as overloading is just a different method that happens to have the same method name.
Overloading has nothing to do with inheritance and polymorphism.
Real time example :: A person, acts like a husband at home, acts like a employee at office, Good citizen in public.
No comments:
Post a Comment