pcyuen98 / covidAngular

0 stars 0 forks source link

KT - Java Data Abstraction #14

Closed pcyuen98 closed 3 years ago

pcyuen98 commented 3 years ago

Provide some elaboration what is Data Abstraction .

Provide 1-2 examples base on the practical.

nadizeq commented 3 years ago

Data Abstraction is process of hiding information and only display necessary information to the user and there are two ways to achieve it by using abstract classes or interface. For example, in layman term, like ordering food in a restaurant but the customer does not know the full detail in what happen in the kitchen, in terms of ingredient, preparing method and et cetera.

In addition, by using interface method, the outcome enable 100% abstraction to be achieved.

Method 1: abstract classes (Source: w3schools.com) Create a class with abstract keyword where this keyword is a non-access modifier, used for classes and methods. In order to access abstract class is to use extends keyword in another class in which that class can use abstract method in the abstract class. The example is shown in the Picture 1.

image Picture 1

Method 2: interface First, create an interface which contain interface method but does not have body like an example in Covid-Angular practical as shown in Picture 2.

image Picture 2

In order to access the interface and interface method is to use the keyword "implements" as shown in the Picture 3.

image Picture 3 To be able use the interface method that is declared in the interface method, there are two possible situation that may occur.

Situation A: The created interface is created in the same location as the classes. Then that specific class can use the keyword "implement".

Situation B: The class that wants to use interface method in a different location (different file), then '@Override' keyword is used and this keyword allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed.

For example, in order for the class in Picture 3 to use the interface method in Picture 2, '@Override' keyword must be used and the way to use it as shown in Picture 4. The name of the function must be the same as interface method.

image Picture 4

nadizeq commented 3 years ago

Source used:

  1. https://www.w3schools.com/java/java_interface.asp
  2. https://www.w3schools.com/java/java_abstract.asp
  3. https://www.geeksforgeeks.org/abstraction-in-java-2/#:~:text=Data%20Abstraction%20is%20the%20property,not%20displayed%20to%20the%20user.&text=In%20java%2C%20abstraction%20is%20achieved%20by%20interfaces%20and%20abstract%20classes.