W3schools - Abstraction
찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요
Abstraction
Data abstraction is the process of hiding certain details and showing only essential information
Abstraction can be achived with either abstract class or interface
Why and when to use abstract class and method?
-
to achive security
- hide certain details and and only show the important details of an object
Abstract
Is a non-access modifier
-
abstract class is a restricted class that cannot be used to create object (it must be inherited from another class)
- it can have both abstract and regular methods
-
abstract method cans only be used in an abstract class, and it does not have a body
- the body is provide by the subclass, must override it
abstract class Animal {
public abstract void animalSound();
public void sleep(){
System.out.println(“Zzz”);
}
}
class Pig extends Animal {
public void animalSound(){
System.out.println(“The pig says : wee wee”);
}
}
Interface
Why and when to use interface?
- “multiple inheritance”
Is a completely abstract class
To access the interface methods, the interface must be implemented(kinda like inherited) by another class with the “implements” keyword(instead of extends) the body of the interface method is provided by the “implement” class
Its methods are by default “abstract” and “public”
Its attributes are by default “public”, “static” and “final”
Cannot contain a constructor
To implement multiple interfaces, separate them with a comma
interface FirstInterface {
public void myMethod();
}
interface SecondInterface {
public void myOtherMethod();
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod(){
System.out.pritnln(“Some text...”);
}
public void myOtherMethod(){
System.out.println(“Some other text...”);
}
}
abstract and interface
Can’t be used to create objects
Abstract method do not have a body, must override