We know that subclass inherits all of the public and protected members of its Parent.
So, it means that Parent or your class had inherited all those methods from its Parent (your class Grandparent) and are able to invoke each of them:
However, You can't do the same thing with class constructors:
So, it means that Parent or your class had inherited all those methods from its Parent (your class Grandparent) and are able to invoke each of them:
public class A
{
public void doSomething(String coolStuff){}
}
class B extends A{}
class C extends B
{
public void doSomething(String coolStuff)
{
super.doSomething(coolStuff); // valid statement.
}
}
public class A
{
public A(){}
public A(String id){}
}
class B extends A{}
class C extends B
{
public C()
{
super(); // valid statement. Cause compiler will create default constructor for B.
}
public C(String id)
{
super(id); // compile-error. Cause there no such constructor in B.
}
}
Class can invoke ONLY his super class constructors!
Class can't invoke super.super.method(); !
Because It violates encapsulation. You shouldn't be able to bypass the Parent class behavior. It makes sense to be able to bypass your own class's behavior via overriding (but not to change) your Parent class behavior.
Because It violates encapsulation. You shouldn't be able to bypass the Parent class behavior. It makes sense to be able to bypass your own class's behavior via overriding (but not to change) your Parent class behavior.
No comments:
Post a Comment