Jump to content
Sign in to follow this  
newcoder1010

How does abstract class / method hide certain details

Recommended Posts

Hello,

I am just playing around with the Java abstraction. 

I did follow the steps in this link and I am able to understand what it is doing. But I am not able to understand the concept of "Data abstraction is the process of hiding certain details and showing only essential information to the user.
"

If you could provide me a real example of how does abstraction hide certain details of an object, it would be helpful. 

Thanks!

 

Share this post


Link to post
Share on other sites

This means that you expose only the functionality without any knowledge of how the actual implementation works.

To demonstrate this, let's have an Animal class which is an abstract class:

abstract class Animal {
	public abstract void makeSound();
  	public void sleep() {
    	System.out.println("Zzz");
    }
}

As you see, the Animal class contains one abstract method, the implementation of this method will soon be provided by the subclasses

Let's have a Bird class:

class Bird extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Coo");
    }
}

Let's also have a Cat class:

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Meow");
    }
}

Now you can instantiate a subclass of Animal and call the makeSound() method without caring the actual implementation of the method in each subclass:

public class App {
    public static void main(String[] args) throws Exception {
        Animal animal;

        animal = new Bird();
        animal.makeSound(); //  Coo

        animal = new Cat();
        animal.makeSound(); //  Meow
    }
}

Do you notice how you call makeSound() without actually caring what kind of animal you have ?, that's what's meant by hiding certain details and showing only essential information to the user

Unfortunately, in w3schools they don't clarify it enough, this is usually explained even better using a Factory Class.

The idea behind factory class method is to hide the details of object instantiation by creating a mediate class that creates objects for you

Let's have a AnimalFactory class:

class AnimalFactory {
    public static Animal getAnimal(String animal) {
        if (animal == null) {
            return null;
        }
        else if (animal.equalsIgnoreCase("cat")) {
            return new Cat();
        }
        else if (animal.equalsIgnoreCase("bird")) {
            return new Bird();
        }
        return null;
    }
}

Modify the main program:

public class App {
    public static void main(String[] args) throws Exception {
        Animal animal;

        animal = AnimalFactory.getAnimal("bird");
        animal.makeSound(); //  Coo

        animal = AnimalFactory.getAnimal("cat");
        animal.makeSound(); //  Meow
    }
}

Do you notice the abstraction ?, you don't instantiate manually, the instantiation process is hidden from you.

I would like to say one last sentence:

It's always preferable to program to an interface (or maybe an abstract class) rather than an implementation, this means to always create a variable which has a type of the abstract class and assign it an instance of a subclass, just like in the examples above

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...