Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

An example of abstraction in java

 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1870
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A nice example is given here
 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
thank you all of you for giving the answers.
raja
 
rohith das
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think this thread would sit better on another forum (not diversions), so I shall move it
 
quick nikhil
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay..Campbell Ritchie..

I will do the same next time..Thanks a ton..
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic