• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused with Oracle's example of Returning a Class or Interface.  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Can someone clarify Oracle's example here
https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html


Suppose that you have a class hierarchy in which ImaginaryNumber is a subclass of java.lang.Number, which is in turn a subclass of Object, as illustrated in the following figure. Now suppose that you have a method declared to return a Number:



The returnANumber method can return an ImaginaryNumber but not an Object. ImaginaryNumber is a Number because it's a subclass of Number. However, an Object is not necessarily a Number — it could be a String or another type.


In the above quote.  Is the example an example where

  • the class ImaginaryNumber is being imported into another class ( not mentioned ) and a method returnANumber is on this imaginary ( not mentioned ) class

  • OR
  • is this an example which  imaginaryNumber is the class and returnNumber is a method of the ImaginaryNumber class ?


  • I can't tell.   Thank You for clarifying this for me. 

     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    It looks like ImaginaryNumber is a subclass of Number. And this class is imported, and used by the method.

    Henry
     
    Junilu Lacar
    Sheriff
    Posts: 11493
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lisa Austin wrote:
  • the class ImaginaryNumber is being imported into another class ( not mentioned ) and a method returnANumber is on this imaginary ( not mentioned ) class


  • It's this one.  Here's what the unnamed class could look like:

    The example is meant to show polymorphism via inheritance. Since ImaginaryNumber is a subclass of Number, it can be used wherever a Number is expected and the code should still work. Formally, this is called the Liskov Substitution Principle.
     
    Junilu Lacar
    Sheriff
    Posts: 11493
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    BTW, the Liskov Substitution Principle (LSP) also includes polymorphism via interfaces. This is why you will be told to prefer programming to interfaces rather than implementations.

    The first version declares the method parameter as an interface type (List<Integer>). The second version declares the method parameter as an implementation type (ArrayList is an implementation of List). The first version that's programmed to an interface follows LSP in that it can accept any List implementation and can still be expected to work.  The second version only works with an ArrayList<Integer>, making it very inflexible.

    This is also why you are encouraged to declare any variable using an interface type if possible, instead of the implementation type:

    The declared types are all interfaces. The objects that you assign to the variables are specific implementations that conform to the contract of behavior specified by their respective interface types.
     
    Junilu Lacar
    Sheriff
    Posts: 11493
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's another example along the same lines. If you have an Animal class and you have Dog, Cat, Hamster, etc. as subclasses, you could have this:

    The class PetOwner can accept any kind of Animal as a Pet. Any code that uses the getPet() method of this class must be written such that it doesn't expect the pet to be any specific subclass of Animal; it must be programmed to assume only the general capabilities of Animal, i.e., it should be programmed only to use whatever methods the Animal class has in its API.
     
    Lisa Austin
    Ranch Hand
    Posts: 178
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry Wong wrote:
    It looks like ImaginaryNumber is a subclass of Number. And this class is imported, and used by the method.

    Henry


    Thank You
     
    Lisa Austin
    Ranch Hand
    Posts: 178
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:BTW, the Liskov Substitution Principle (LSP) also includes polymorphism via interfaces. This is why you will be told to prefer programming to interfaces rather than implementations.

    The first version declares the method parameter as an interface type (List<Integer>). The second version declares the method parameter as an implementation type (ArrayList is an implementation of List). The first version that's programmed to an interface follows LSP in that it can accept any List implementation and can still be expected to work.  The second version only works with an ArrayList<Integer>, making it very inflexible.

    This is also why you are encouraged to declare any variable using an interface type if possible, instead of the implementation type:

    The declared types are all interfaces. The objects that you assign to the variables are specific implementations that conform to the contract of behavior specified by their respective interface types.



    Awesome!  Thank You so much for all the examples.  This really helps.  
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!