On the similarities and differences between interface and abstract class in Java
•
Java
On the similarities and differences between interface and abstract class in Java
Abstract classes and interfaces have puzzled me for a long time. In my opinion, interfaces are really similar to abstract classes. During this period, I also looked for many materials and referred to the opinions of various gods. I can only understand the difference between the two in grammar. Harden your head and make a small summary. Perhaps, after learning the knowledge, the understanding will be more profound, Ollie!
Grammatical differences
In depth understanding
abstract class People{
private int age;
public abstract void doSomething();
}
interface studyable {
void howToStudy();
default void info() {
System.out.println("study is important for everyone..");
}
}
public static void main(String[] args) {
studyable[] studyables = new studyable[]{new Robot(),new Student()};
for (studyable s : studyables) {
s.howToStudy();
s.info();
}
System.out.println("*************************************");
People[] peoples = new People[]{new Student(),new Teacher()};
for (People p : peoples) {
p.doSomething();
}
}
//测试结果
Robot comes out
Student comes out!
robot need to practice
study is important for everyone..
student will read book..
study is important for everyone..
*************************************
Student comes out!
Teacher comes out!
student should study..
teacher should teach..
Reference link: what is the difference between an interface and abstract class
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码