Consider the following code snippet where C2 is a child clas

Consider the following code snippet, where C2 is a child class of C1:

Assuming this code compiles, in which class must the \"go()\" method be declared?

Solution

go() must be declared in the base class C1.This is because in the main method of the child class,object is created of C2 but its an instance of C1.

Following code Gives Error :


class C1{
   /*public void go()
   {
       System.out.println(\"C1\");
   }*/
}
public class C2 extends C1 {
   public void go()
   {
       System.out.println(\"C2\");
   }
   public static void main(String[] args) {
       C1 c = new C2();
       c.A();
   }
}

Consider the following code snippet, where C2 is a child class of C1: Assuming this code compiles, in which class must the \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site