newcoder1010 Posted July 14, 2020 Report Share Posted July 14, 2020 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! Link to comment Share on other sites More sharing options...
Eyad Syria-lover Posted July 31, 2020 Report Share Posted July 31, 2020 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now