• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

hierarchy doubt

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lets say...


wat's the actual or practical benefit out of that declaration line in main() ?
can Parent ever do what Child can do?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by adam lui:

wat's the actual or practical benefit out of that declaration line in main() ?
can Parent ever do what Child can do?



The benefit isn't that a Parent can do what a Child can do - it's actually the other way around - a Child can do whatever a Parent can do. The benefit is that you don't have to care what the Child is. It'll just do the appropriate thing.

Rather than pure inheritance, let's look at an example that uses Interfaces. The J2SE API contains an interface called "List". The classes LinkedList and ArrayList both implement that interface. You could certainly write this code:



That's fine and it'd work, but what happens if you decide that the performance of your application isn't very good? ArrayLists aren't particularly efficient when it comes to inserting new elements, but LinkedLists are great at it. You could change your code to be like this:




Yup, that'd work. It would also work much faster than the previous version. The problem is that anywhere you use that variable, you need to make a change in your code. I only had to make a change in the declaration and instantiation, but what if you were passing this object around? All of those lines would have to be modified, as well. It could turn into a nightmare.

Now, what if you had written the original line like this:



If you just use a List as your compile-time type, polymorphism will take care of invoking the correct method and it makes maintenance much easier. Given the same need for change, you could do this:



How many places does your code need to change? Just one. Only where the actual object is instantiated.

Programming to an interface (or supertype) is a great way to reduce the amount of dependencies in your code. It helps keep your systems flexible and prevents maintenance nightmares.
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:


The benefit isn't that a Parent can do what a Child can do - it's actually the other way around - a Child can do whatever a Parent can do. The benefit is that you don't have to care what the Child is. It'll just do the appropriate thing.

Rather than pure inheritance, let's look at an example that uses Interfaces. The J2SE API contains an interface called "List". The classes LinkedList and ArrayList both implement that interface. You could certainly write this code:



That's fine and it'd work, but what happens if you decide that the performance of your application isn't very good? ArrayLists aren't particularly efficient when it comes to inserting new elements, but LinkedLists are great at it. You could change your code to be like this:




Yup, that'd work. It would also work much faster than the previous version. The problem is that anywhere you use that variable, you need to make a change in your code. I only had to make a change in the declaration and instantiation, but what if you were passing this object around? All of those lines would have to be modified, as well. It could turn into a nightmare.

Now, what if you had written the original line like this:



If you just use a List as your compile-time type, polymorphism will take care of invoking the correct method and it makes maintenance much easier. Given the same need for change, you could do this:



How many places does your code need to change? Just one. Only where the actual object is instantiated.

Programming to an interface (or supertype) is a great way to reduce the amount of dependencies in your code. It helps keep your systems flexible and prevents maintenance nightmares.



wow Corey I am so appreciated with your reply, no one has ever given me such detail! I am so much clarified now,
may i conclude in other words, for this example - List l = new LinkedList ();
reference variable l is of type List, as an LinkedList object, can do what a LinkedList can do, and also is able to do what a List typed object can do?
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also consider this example:

output: Child eating

Note that with this polymorphic object you created at line 1 the only methods you can call are those declared in the reference type(Parent) but which method to invoke is selected dynamically at runtime based on the object type(Child), in this example the eat() has been overriden in Child so the JVM choose it.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by adam lui:

may i conclude in other words, for this example - List l = new LinkedList ();
reference variable l is of type List, as an LinkedList object, can do what a LinkedList can do, and also is able to do what a List typed object can do?



Note that the compile-time type of the object reference l is "List". That means that, according to the compiler, you can invoke any method on l that is defined in List. Even though the run-time type of l is LinkedList, the compiler doesn't know that. Therefore, if you want to invoke a method that is defined in LinkedList and not in List (addFirst, for example), you need to first cast the object as a LinkedList, thereby breaking your usage of an interface, in the first place.
 
Are we home yet? Wait, did we forget the tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic