• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

An example of abstraction in java

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



 
Ranch Hand
Posts: 237
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80619
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80619
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay..Campbell Ritchie..

I will do the same next time..Thanks a ton..
 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic