Page 1 of 1

OOP- Concept of Abstraction

#1 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Posted 04 May 2010 - 08:28 PM


Uses of Abstraction
*Note: japanir also has some great specific tutorials pertaining to this subject specifically in Java. This topic is more about the concept of abstraction for OOP languages with a focus on Java.

The “extends” keyword
Since this tutorial is cross-language, I’ll tell you that it is the method in Java that makes subclasses (like the parenthesis after a class name in Python). That is the basis for understanding abstraction.
You can think of extends as a way to make a class inherit all the attributes and methods of the extended class (superclass). So, say I have a class called Animal:
public class Animal {
    private int weight;
    private int height;
    public void breath() {...}
    public void eat() {...}
    public void drink() {...}
    public void sleep() {...}

Now, assume that those methods actually do something. I just don't want to write them all out again. Now, any class that extends Animal will have all the attributes AND methods of Animal and for all intents and purposes, it IS an Animal. Say we make the class called Cat:
public class Cat extends Animal {
    public void jump() {...}

Now, Cat is an Animal and you can call breathe(), eat(), drink(), or sleep() on it and it will work! Since you implemented them in the Animal class, they still exist in Cat. But, for Cat, we defined an additional method that is unique to Cats - jump(). Not all Animals can jump, so we don't define it there. This is called abstraction, where we make superclasses contain as much related information as possible.

Why abstraction?
Abstraction has useful benefits such as being able to group all Animals together. Say we have Parrot, Dog, and Frog also as subclasses of Animal. We can group all of them together under Animal.

public static void main(String[] args) {
    ArrayList<Animal> arr = new ArrayList<Animal>();
    arr.add(new Dog());
    arr.add(new Cat());
    arr.add(new Parrot());
    arr.add(new Frog());

    // Now, we can call methods on these elements, but
    // ONLY those defined in Animal.
    arr.get(0).breathe(); // Valid
    arr.get(1).jump(); // ERROR: Invalid
    if (arr.get(1) instanceof Cat)
        (Cat)(arr.get(0)).jump(); // Valid

That shows the practical use of abstraction. See? The arr ArrayList can hold any Animal, even subclasses. But, it can only call those methods that is has in common with the others (those defined in Animal). In order to call subclass-specific stuff, you will have to cast.

Additionally, having this form of abstraction is wonderful when that class (or interface) has many, many subclasses and each one has literally hundreds of methods. Take the List<E> interface in Java. There are several dozen methods defined in List and several classes that subclass it (or implement it, if you want to be a stickler for Java). This can be a REAL pain implementing them over and over again in each subclass like LinkedList, ArrayList, AbstractList, and even Vector. They all use those methods and the use of abstraction allows them all to have those methods in common and be grouped together.

The “instanceof” keyword
The instanceof operator is useful when it comes to abstraction because it will tell you if a specific object is an instance of something else (usually subclass or superclass). Going back to the previous example, arr's 1st index MIGHT be a Cat, so I check first, and if it is, then I cast to a Cat to execute the Cat-specific code. If not, then no harm is done.
So basically, "Extends tells any class the class from which it inherits". Here is a note for you. All objects that do not extend anything actually extend the Object class. So this:
class MyObject {

and this:
class MyObject extends Object {

are the same. Therefore, EVERY object in Java has methods in common with the Object class. They can ALL be put under the same category here and are related directly.

Normally, I have something cool to say when I’m done, but for now, I’ll just leave you with this. In Object-Oriented Languages, abstraction is a tool that one can use to both define like objects and be able to manage them together. For more specific tutorials on how this works in Java, seriously, look at japanir’s tutorials. They really are quite good. Well, that’s abstraction for you! :D

edit: fixed broken tag

This post has been edited by Dogstopper: 04 May 2010 - 08:45 PM

Is This A Good Question/Topic? 6
  • +

Replies To: OOP- Concept of Abstraction

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Posted 05 May 2010 - 03:14 AM

A great tutorial indeed Dogstopper! :^:
I see lots of threads considering this subject, it's good that now we have a new source to refer them to.

Also, thanks for referring to my tutorials as well :)
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon

Reputation: 12278
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Posted 15 May 2010 - 10:43 PM

Very well covered. :^:

The big concept with abstraction is polymorphism, which you did an excellent and thorough job of going over.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1