Skip to content
Advertisement

Head First Java-Page 192 (Mixed Messages)

I am attemping an example question from Head First Java:

Question I am attempting

I typed it as it is on netbeans. Creating 3 classes as classA, classB, classC and Main method as mixed class. In class mixed to I get a error, can some explain me why this happens and how to solve it?

        public class classA {
            int ivar =7;
            void m1(){
                System.out.println("A's m1, ");
            }

            void m2(){
                System.out.println("A's m2, "); }

            void m3(){
                System.out.println("A's m3, ");
            }
        }

    public class classB extends classA {
        void m1(){
            System.out.println("B's m1, ");
        }

    }

    public class classC extends classB {
        void m3(){
            System.out.println("C's m3, "+(ivar + 6));}

        }

public class Mixed2 {
    public static void main(String[] args) {
        a=new A(); 
//Error Cant find symbol,Symbol:Variable a     location:class mixed 2

       b=new B(); //Same error above and below
        c=new C();
        a2=new C();
    }

}

Advertisement

Answer

yes you can type it all in one class. Kindly have a look at the code below.Since you are still learning its best you type it as it is without trying things out differently which can be done later on as you master the language.Happy learning ! .

public class mixedsample {

    public static void main(String[] args) {
        A a=new A();
        B b=new B();
        C c=new C();
        A a2=new C();

      /*  b.m1();
          c.m2();
          a.m3();
      Answer=B's,m1 ,A's,m2 ,A's,m3*/

     /*  c.m1();
       c.m2();
       c.m3();
A's,m1 ,A's,m2 ,A's,m3 */

     /* a.m1();
     b.m2();
     c.m3();
A's,m1 ,A's,m2 ,A's,m3 */

   /*  a2.m1();
     a2.m2();
     a2.m3();

A's,m1A's,m2 ,A's,m3  */




    }
}

    class A{
        int ivar=7;
        void m1(){
            System.out.println("A's,m1 ");
        }

        void m2(){
            System.out.println("A's,m2 ");

        }

        void m3(){
            System.out.println("A's,m3 ");

        }
    }

    class B extends A {
    void m1(){
            System.out.println("B's,m1 ");

    }
    }
    class C extends B{
    void m1(){
            System.out.println("A's,m1 ");

}
    }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement