aspose file tools*
The moose likes Beginning Java and the fly likes An example of abstraction in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "An example of abstraction in java" Watch "An example of abstraction in java" New topic
Author

An example of abstraction in java

rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
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?

regards,
Raja
Keith Pitty
Ranch Hand

Joined: Sep 22, 2005
Posts: 32
Any application that defines a Class that is used to instantiate objects is one that demonstrates abstraction.

The analytical process of recognising that several real-world objects share enough characteristics to be considered as members of the same class is abstraction.
Amit Agrawal
Ranch Hand

Joined: Aug 23, 2001
Posts: 282
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?

regards,
Raja


Threads, IO, Sockets, JDBC....

can you give some examples where you don't see abstraction in Java...
Sameer Jamal
Ranch Hand

Joined: Feb 16, 2001
Posts: 1870
A nice example is given here
rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
hi friends,
thank you all of you for giving the answers.
raja
rohith das
Greenhorn

Joined: Sep 22, 2009
Posts: 8
here is an example

abstract Abstclass{
int x, y;
...
void method1(int X, int Y) {
...
}
abstract void method2();
abstract void method3();
}


class Subclass1 extends Abstclass {
void method2() {
...
}
void method3() {
...
}
}
class Subclass2 extends Abstclass {
void method2() {
...
}
void method3() {
...
}
}


hope this will help you




_rohith
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
[
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.
jaibardhan Ruwari
Greenhorn

Joined: Jul 18, 2010
Posts: 5
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to the Ranch

I think this thread would sit better on another forum (not diversions), so I shall move it
quick nikhil
Greenhorn

Joined: Mar 14, 2012
Posts: 2
One more simple example for abstraction for Beginners:

Abstraction Example:

ClassA.java



ClassB.java


ClassC.java



MainClass.java



Out Put:

this is Method 2 of class B
this is Method 3 of class B
this is Method 2 of class c
this is Method 3 of class c

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to the Ranch
Always use the code button; I have edited your post and you can see how much better it looks.
That doesn’t show abstraction at all, I am afraid. It shows abstract classes.
quick nikhil
Greenhorn

Joined: Mar 14, 2012
Posts: 2
okay..Campbell Ritchie..

I will do the same next time..Thanks a ton..
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: An example of abstraction in java