0

Let's say we have some class structure:

     public interface IA { public void doA(); }
     public class A implements IA { public void doA(){ System.out.println("A class doA");}
     public class B extends A { public void doA(){ System.out.println("B class doA"); }

And now in a main I state this:

     //in a main
     IA ia = new B();
     ia.doA();

Am I correct in stating that the the runtime object of ia is a B class and it has inherited all the super classes methods and attributes?

The printout from within the imaginary main is B class doA as the method doA in B will hide the same methods in its super classes?

  • 1
    What's the difference between override and new? Whoops. Wrong language. Java documentation – Nathan Cooper Mar 5 '14 at 12:40
  • As for your last question: have you tried running the code? This is very easy to test... – Andres F. Mar 5 '14 at 13:02
  • 2
    The way I prefer to think of inheritance hierarchies including interfaces is by understanding that "subclass" and "subtype" are different qualities. B is a subclass of A, but a subtype of A and IA. Subtyping relationships are important for defining what variable type you can assign some entity to, and subclass relationships are important for defining what behaviour some entity has. – Phoshi Mar 5 '14 at 13:08
  • C# or Java, still same basic idea ;). @AndresF. I have compiled the code and it does what i expected it to do, Biggest issue for me is how to define why so i can learn it. I think i have got a clearer view of this type of override & overload. Found another link that is not that bad ;) Java docs Thnx everyone! – user1501127 Mar 5 '14 at 13:44
4

Yes, you are correct.

  1. ia is of class B
  2. ia is also of class A since B inherits A.
  3. B overrrides A's doA() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.