Saturday 28 September 2013

Java Inheritance

Java Inheritance

I have a class A
public class A{
public void doWork(){
..............
.............
}
}
Now a public class B extends A
public class B extends A{
@override
public void doWork(){
............
............
}
}
Now Public class C create a object of B into it and pass it to a method of
class D
import xxx.xxx.B;
import xxx.xxx.D;
public class C{
B b= new B();
D d = new D();
d.method(b);
}
now class D takes a argument of type A into its method and operate on it.
public class D{
public void method(A a){
..........
..........
}
}
Actually It is allowed, but I cannot understand why it is allowed ?
method() in class D should take an object of type A. Please help me out ?

No comments:

Post a Comment