Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you explain please? Also, what does parantheses mean, for example printName

ID: 3781074 • Letter: C

Question

Can you explain please? Also, what does parantheses mean, for example printName ((Parakeet) parrot1);

Consider the following hierarchy of classes: A program is written to print data about various birds: public Class Birdstuff { public static void printName (Bird b) { / * implementation not shown */ } public static void prinBirdCall(Parrot p) { /* implementation not shown */ } //several more Bird methods public static void main (String [] args) { Bird bird1 = new Bird(); Bird bird2 = new Parrot(); Parrot parrot1 = new Parrot (); Parrot parrot2 = new Parakeet(); /* more code */ } } Assuming that none of the given classes is abstract and all have default constructors, which of the following segments of /* more code */ will not cause an error? (A) printName(parrot2); printBirdCall((Parrot) bird2); (B) printName((Parrot) bird1); printBirdCall(bird2); (C) printName(bird2); printBirdCall(bird2); (D) printName ((Parakeet) parrot1); printBirdCall(parrot2); (E) printName((Owl) parrot2); printBirdCall ((Parakeet) parrot2);

Explanation / Answer

The parantheses in printName((Parakeet)parrot1) is used for casting where parrot type is being down casted to the type parakeet. To understand this first lets understand what is casting

Casting is converting one type to another type. It doesnot change the objects but only manipulates references. It is of 2 types :

1. Up casting is casting a child type to a parent type i.e upwards in inheritance. eg casting parakeet to parrot , Parror parrot = (Parrot) new Parakeet();.Here parrot is a refernce type and Parakeet() is a contructor which is making an object of type Parakeet. Up cating is safe and results in no exceptions as a child will always have all the behaviors of its parents.

2. Down casting is opposite to up casting where the parent type is casted to child type i.e downwards in inheritance, Bird bird = new Parrot(); Parrot parrot = (Parrot) bird;

This operation is not safe as this can result in ClassCastException in java. for eg when Bird bird = new Parrot(); Owl owl= (Owl) bird; as owl is not parrot even though both parrot and owl are birds. If you are going to do downcasting always use instanceof operator to check the type of object being casted.

Option A is correct.

Explanation of answer

option A> printName(parrot2) ..parrot2 reference is pointing to parakeet object. parakeet is also a bird hence calling this method won;'t create any exceptions due to upcating parrot2 to bird. similarly printBirdCall((parrot)bird2) is also safe. Hence Right

option B > printBirdCall(bird2) will give error as it needs to be casted to type parrot. Hence wrong.

option C > same reason as option B . Hence wrong

option D> printName((Parakeet) parrot1) will gove classcastexception as parrot1 cannot be casted to Parakeet

option E> printName((Owl) parrot2) will give error as both are in compatible types . hence wrong

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote