Originally posted by rajaraman navaneethan: hi friends, recently in an interview i was asked to explain basic 4 OOPs concepts with examples.i was able to explain inheritance,polymorphism,encapsulation with practical programming examples.
i was not able to give a programming example to illustrate abstraction.can any one of you give an example of a java program which illustrates abstraction?
rajaraman navaneethan wrote:i was able to explain inheritance,polymorphism,encapsulation with practical programming examples.
If you explained encapsulation you also explained abstraction. This is because data abstraction is one of the two major aspects of encapsulation (the other is information hiding).
When you encapsulate something you get an outside and an inside. Take any encapsulated unit, like a primitive or an object for example. The way you can use it and how it behaves is the data abstraction. Hidden inside is the implementation.
In Java you usually define your own data abstraction by putting together a set of non-private methods in the form of a class or an interface.
Abstraction is only giving essential details w/o any background information.This is achieved in java by
1) Abstract Class and Interface //We don't know the function implementation of code until unless at run time or we have a information about the class which implementing this Interface or Extends this Abstract Class If we click on this method it directly take us to Interface method not to Class which actually implementing this method.
2) Hiding information by access modifier only give access to required
3) Object - Nothing can be access by Object ref.
An example from the standard Java libraries is the Calendar class. Calendar is abstract; when you call its getInstance method, you are getting an instance of the concrete implementation (GregorianCalendar, BuddhistCalendar, ...) that is appropriate to your particular Locale.
quick nikhil wrote:One more simple example for abstraction for Beginners:
I hate to say, but I think it would be a lot better if your example had properly named classes, because I'm with Keith: abstraction is
"the analytical process of recognising that several real-world objects share enough characteristics to be considered as members of the same class".
(very nicely put; I'm going to have to remember that one ).
@rajaraman: I guess my question would be: Do you want an example of abstraction, or the use of an abstract class?
Because the two things aren't quite the same.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here