1

This is a Java question:

When instantiating an Object that has a Reference type that is different from the Object type, what are the scenarios that determine member availability?

For example:

Shape shp = new Square(2, 4); //Where Square extends Rectangle and implements Shape

Will the Shape or Square methods be associated with this code? Does it matter if all methods are static? Does class hiding have any bearing on the choice? If methods are overridden, does that affect the choice?

Here is a more detailed question about the same thing:

public abstract class Writer {
public static void write() {System.out.println("Writing...");}
}
public class Author extends Writer {
public static void write() {System.out.println("Writing book");}
}
public class Programmer extends Writer {
public static void write() {System.out.println("Writing code");}
public static void main(String[] args) {
Writer w = new Programmer();
w.write();
}
}

Why does the code above produce an output -> Writing...

And the following code produces output -> Writing code

public abstract class Writer {
public void write() {System.out.println("Writing...");}
}
public class Author extends Writer {
public void write() {System.out.println("Writing book");}
}
public class Programmer extends Writer {
public void write() {System.out.println("Writing code");}
public static void main(String[] args) {
Writer w = new Programmer();
w.write();
}
}

When instantiating an Object that has a Reference type that is different from the Object type (like this example), what are the scenarios that determine member availability?

Splink01
  • 11
  • 3
  • 3
    It seems like you are asking for a full explanation of inheritance and polymorphism. There are loads of books that have entire chapters on this. Far too much information to fit on one Stack Overflow page. – Dawood ibn Kareem Oct 17 '18 at 19:00
  • Thanks for the comment. I have added more detail. – Splink01 Oct 17 '18 at 19:22
  • OK, short answer - static methods are resolved at compile time, based on the type of the variable that you call them on; non-static methods are resolved at run time, based on the class of the object that's referenced by the variable that you call them on. – Dawood ibn Kareem Oct 17 '18 at 19:29
  • @DawoodibnKareem - that is exactly what I needed to know. Thanks! – Splink01 Oct 22 '18 at 18:19

1 Answers1

0

Will the Shape or Square methods be associated with this code? Yes

Methods known to Shape will only be allowed to be called using shp reference variable.

Does it matter if all methods are static?

Polymorphic calls can't be made using shp reference variable if all methods are static.

Does class hiding have any bearing on the choice?

Yes, type of shp reference variable will decide exactly which method gets called. Will be decided at compile-time itself.

If methods are overridden, does that affect the choice?

Static methods are not polymorphic, so there won't exist any overriding scenario.

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
  • Saurav, thank you for attempting to answer my rather broad question. I have added more detail to my original question if you care to take a look. – Splink01 Oct 17 '18 at 19:23