I was embarrassed that I couldn't answer the Java language specification at the interview, so I will relearn it.
# | interface | abstract |
---|---|---|
Specifications | Used when you want to define as a class specification | Used when there is an inheritance relationship and you want to reuse the process |
Relationship with implementation class or concrete class | Implementation classCANAbstract function | Concrete classISAbstract class |
interface
//abstract
public interface Cashier {
void bill();
}
//Implementation class 1
public class CreditCart implements Cashier {
public CreditCart(){
}
@Override
public void bill(){
System.out.println("billed by credit card");
}
}
//Implementation class 2
public class Cash implements Cashier {
@Override
public void bill() {
System.out.println("billed by cash");
}
}
abstract
//Abstract class
public abstract class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void sleep() {
System.out.println("Sleeping");
}
public abstract void speak();
}
//Implementation class 1
public class Human extends Animal {
public Human(String name) {
super(name);
}
@Override
public void speak() {
System.out.println(name + " speack human languages");
}
}
//Implementation class 2
public class Cat extends Animal {
public Cat(String name) {
super(name);
}
@Override
public void speak() {
System.out.println(name + " speack cat's language");
}
}
# | Same class | Same package | Sublux | all |
---|---|---|---|---|
default | ◯ | ◯ | - | - |
private | ◯ | - | - | - |
protected | ◯ | ◯ | ◯ | - |
public | ◯ | ◯ | ◯ | ◯ |
Recommended Posts