• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstraction and Object Oriented Programming

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to know what is and how do we do abstraction? How do we divide a big project into different classes in different packages using abstraction?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO abstraction means you get only the details that are relevant to your problem from the real-life entity, and make them the basis of your class.
For example, suppose, you want to create an abstract data type called Room. A real room contains many details but only certain aspects of it are relevant, depending on how you approach it. If you were an architect, you would probably be concerned with floor size, color, the number of windows it has, lighting, etc etc. If you were a game designer you would maybe think about what Monsters would be deployed in this room, what treasure hidden, if there are any clues/traps etc. If you were a hotelier you would be thinking about occupancy rates, how many beds, room number, etc etc.
Having determined which entities are relevant to the problem domain, you have to decide on the granularity of your class's attributes.
Furthermore, abstraction also entails presenting a public interface for your class's behavior, but hiding the actual ipmlementation of this behavior to your client.
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anthony.
I want to know about how abstraction is used to present a public interface for my class's behavior, but hiding the actual ipmlementation of this behavior to my client. May be an example would help.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this example, the implementation of the calculateArea( Rectangle ) method is hidden from the user while it provides a public interface for the user to use.
Getting the idea?
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to consider the following java supports interfaces which are abstract and abstract classes. The idea behind an interface is that you define the behaviors you want to expose. You can then implement the interface in a class. The implementation is thus hidden from the client application that uses it. If you want to change the internals of the implementation class you can and the client application will not need to make any progamming changes. Obviously, you'd need to redistribute the code, but the client would not need to physcically alter his/her code that calls you class because they use the interface. Classes implementing the interface are forced to provide implementations for all the interfaces methods.
An abstract class is sort of like a interface in that some portion of it is abstract. The developer marks the class as being abstract this forces the developer using it to extend the class and provide implementations for all remaining abstract methods, while it inherits those that were concretely defined.
Regards,
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And since you brought up OO...
Consider how abstraction supports encapsulation and polymorphism.
We already talked about a form of encapsulation achieved by limiting the client's access to our class via an interface.
Polymorphism is also supported by providing an interface that can be implemneted by a number of independent classes. These different classes can however be treated the same with regard to the methods provided by their common interface. As a result, you can have a vector of object references of the interface type. Each reference refers to an object which is of the type interface, but each class can implement the methods differently.
Regards,
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. I am clear on OO concepts.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic