roony
roony

Reputation: 165

sending a child object as a parent parameter

I ran into this code:

public class B extends A
{....}

public class C extends A
{...
 public boolean f(A a)
   {...}
}
//driver
...
A y1=new B();
B y2=new B();
A z1=new C();
C z2=new C();
//1 System.out.println(z1.f(z2));
//2 y1.dosmth()==((B)(z1).dosmth());

I initially thought that #1 would be incorrect because f can only receive an object defined as A, but apparently it also works with an object that is a child of A(z2 is defined as C and is an object of C). Doesn't inheritance limit to the use of methods to receiving arguments defined the same as in the method?

About #2: I understood that casting is only possible if the object has any inheritance relationship. i.e, I can't do (Animal)table.However it appears to work here. Is it because B extends A, like C does? Though it still looks to me like (Dog)cat or something like this.

Would appreciate your input!

Upvotes: 4

Views: 3600

Answers (2)

Floam
Floam

Reputation: 704

Nice question!

A a means that a is of type A. Since C is a Child of A, System.out.println(z1.f(z2)) is valid. It's like saying:

A a = new C()

f(z2) --> f(a) .

If dosmth() is in A, or if it is in both B and C, your statement #2 should run as well. My explanations below should help explain why:

If B and C extend A, they both have all of A's non-private properties.

For example, if A has a public method, getMe(), then B and C will have a public method, getMe(), regardless of whether you put a getMe() method in B and C.

However, overriding may take place if you have a getMe() in the B and C classes. When overriding occurs, the properties within the Child Class's getMe() method will take precedence over the Parent Class's getMe() method. If the properties of your Child Class getMe() differ from the Parent Class getMe(), overriding may affect what happens when getMe() is called on your objects. If you do not put a getMe() in the B and C classes, B and C will automatically receive A's getMe().

You need to cast a Parent Class to its Child Class when you are setting a Child Class object to a variable of type Parent Class and calling a Child Class method on the Parent Class variable that is not found in the Parent Class.

Animal var1 = new Dog()

Animal var2 = new Cat()

(Dog) var1.bark() // since not all animals bark!

By casting, you are telling the compiler, "Trust me! I know this is a dog. Just go with it!", because the compiler does not know which type of Animal you are working with. The compiler only knows that it is working with an Animal. Since the Animal class does not have a bark() method, the compiler will think that an error will occur when you do not cast. By casting an Animal to a Dog, you are telling the compiler to assume that this particular instance of an Animal is a Dog.

Hope this helps!

Upvotes: 1

Jiazhi Guo
Jiazhi Guo

Reputation: 1

  1. You should be able to use a subclass in the same way as the parent class by OOP principles

  2. Java allows you to cast something to anything in the same inheritance tree in Compile-Time. But you might get java.lang.ClassCastException in Run-Time if you try to cast a cat to dog. It's just like "Hey Java, trust me, cat is obviously a dog! There won't be any problem."

Upvotes: 0

Related Questions