简体   繁体   中英

I want to check if my output is correct. And, more details on this 'getClass()' method

I just started Java, and I came across this question. I am not sure if my output is the correct one. I also want more details on the 'getClass()' method in this context, since 'Orc' and 'Unicorn' do not inherit from the same class.

 abstract class Creature {
        public abstract void makeNoise( );
    }

class Monster extends Creature { 
    public void makeNoise( )
    { 
      System.out.println("Raaa!");
    } 
}

class Orc extends Monster { }

abstract class Myth extends Creature { }

class Unicorn extends Myth { 
  public void makeNoise( )
  { 
    System.out.println("Neigh!"); 
  }
}

Unicorn x = new Unicorn( ); 
if (x instanceof Myth) {
   System.out.println("myth");
}
else {
   System.out.println("not myth"); 
}
x.makeNoise( );

Orc y = new Orc( );
if ( x.getClass( ) == y.getClass( ) ) { //I need more explanation on this 'getClass()' method
   System.out.println("yes");
}
else {
   System.out.println("no");
}
y.makeNoise( );

My output is:

  • myth
  • Neigh!
  • no
  • Raaa!

Is it correct?

Last question:

  • Is an object of a subclass an instance of the parent class?

Your code works fine and the output also correct.

Last question:

Is an object of a subclass an instance of the parent class?

to get the clarity on the above question, you need to learn inheritance concept. Please consider the following code . I have explained in the comments

    class Super {
    public void test() {
        System.out.println("From Super Class TestMethod");
    }
}

class Sub extends Super {
    public void subTest() {
        System.out.println("From the Sub Class Test ");
    }
}

public class SubSuperCheck {

    public static void main(String[] args) {
        Sub s = new Sub();// when JVM find this, it will create the memory for
                            // the
                            // two methods test() from Super class and subTest()
                            // from Sub Class
                            // if the reference(Sub s) is also the Sub then we
                            // can access the two methods(test() and subTest())
                            // if the reference(Super s) then we are able to
                            // access the Super class method only.
        System.out.println(s.getClass());// getClass() method of Object class
                                            // will return the the class name
                                            // with which we created the object
                                            // here we have created the the
                                            // class with Sub. So that we will
                                            // get the out put as Sub
        s.subTest();// Sub class method
        s.test();// Super class method
        // if we create in the following way
        Super s2 = new Sub();
        // then we are able to access the Super class method only
        s2.test();// Okay
        s2.subTest();// we will get compilation error--The method subTest() is
                        // undefined for the type Super
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM