Suppose I have two Classes, A and B. The A class is defined as abstract, while B extends this abstract class, and finally i test the result and both classes are part of same package.
public abstract class A {
protected abstract void method1();
protected void method2() {
System.out.println("This is Class A's method");
}
}
public class B extends A {
@Override
protected void method1() {
System.out.println("This is B's implementaiton of A's method");
}
}
and now when i test them:
B b = new B();
b.method1();
b.method2();
I get expected output:
This is B's implementaiton of A's method
This is Class A's method
QUESTIONS:
- What is the purpose of
@Override
keyword, because if I omit it, it
still works the same.
- If I don't implement the abstract method, I get a compilation error. So what is the difference from implementing an interface?
- Also, I can implement
method2()
in B as well. Then the output changes to what is use in B. Isn't this also overriding the parent class method? Then what is the purpose of explicitly defining a method as abstract in Class A?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…