Hi All,
I am back again with some
Java basic stuff.
First of all my sincere apologies, if there are any mistakes in my post.
Please give your valuable inputs on these
thread, by going thru each and every line of it with proper explanation.
Please dont give any references to another thread, as I feel no other thread is clear and complete about these concepts. So I want this thread to end the discussion on the basic concepts of OOPS(in Java).
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).
** By seeing the above two definitions, we can say that ‘Abstraction’ and ‘Encapsulation’ are two separate topics. Abstraction means ‘hiding implementation details’ and Encapsulation means ‘hiding the data’.**
**This concept of abstraction is no where related to abstract class or interface. When I am talking about abstraction means it’s different from abstract class. (Please clarify on this clearly)**
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 coderanch.
Java example ::
Thanks & Regards,
Ravi.