• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inheritence

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i understand some inheritence. i understand what happens with with methods when we have class A , and class B extends A . same thing with the interfaces.

i also understand that you can say A ab = new B(); where A is the superclass of B, but I dont understand the purpose of this? or what is this useful for?

ive seen several examples such as List<String> l = new LinkedList<String>(); and Animal a = new Dog();

why do this? why not just say LinkedList<String> l = new LinkedList<String>(); or Dog d = new Dog();

thank you
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am also interested in this question.
If we say List<String> = new ArrayList<String>();
for example, does this mean that we get the functionality
of List and also ArrayList?

Thanks.
Geoff.

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

priv prive wrote: why not just say LinkedList<String> l = new LinkedList<String>();


Because that ties you to a specific List implementation, even if the implementation doesn't matter.

http://www.google.com/search?q=code+to+interface+not+implementation
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"code to the interface, not the implementation"

I agree. Also,

In the example:



This is espescially used when Animal is abstract. I mean, what is an animal anyways??

This way you can have



and you can do animal things to all of them. And for the animal things that never change (like breathe(), eat(), or sleep()), that gets taken care of in the abstract superclass so they can all be taken care of without writing more code and no worries about someone doing this:



I dunno what would come of this in the real world.... so it would be best not to let it happen in a zoo program
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:and you can do animal things to all of them.


Ewwww!

Except the giraffe, they're hot.
 
jerry washington
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your replies

but I still dont get it. if Dog extends Animal. If you write: Dog d = new Dog(); and Animal d = new Dog(); and then call a method d.eat(); the result will be the same.

I understand like polymorphic arguments. and i understand for example that if you had an animal array: Animal[] a = new Animal[2] then you could say a[0] = new Dog(); a[1] = new Cat(); and then a[0].eat(); would call Dogs eat().

but if you only have an abstract Animal class which a Dog class extends. whats the point of doing Animal d = new Dog(); instead of Dog d = new Dog(); ?


basically i didnt understand :

Because that ties you to a specific List implementation, even if the implementation doesn't matter.



thanks
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read any of the Google hits?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this, extending Janeice's example:
 
jerry washington
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did but my english is not the best so i was not so sure if i understood it. Is it if I want to change how an animal is? so then i would edit abstract Animal class , and the Dog class code that was inherited from Animal would change automaticly? if i wrote Animal a = new Dog(); ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about the example with the List:

The idea is that the rest of the program does not need to know that 'things' is a LinkedList - the rest of the program just needs to know that it's a List, and what particular implementation of the List interface is behind it, is something that it shouldn't be concerned about.

The advantage of this is that you might later want to change it to a different implementation of List. Suppose that you discover that for this particular List, it would be more efficient to use an ArrayList. If you wrote the code as above, you have to change only that one line, to:

For the rest of the program, no changes are needed, because 'things' is still a List.

If you would have written:

the change might have been much harder, because now the rest of the program would have known that 'things' is a LinkedList, and maybe somewhere else in the program some method might have been called on 'things' that's specific to LinkedList.

This principle is called polymorphism and it's an important principle in object oriented programming.
 
jerry washington
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that i understood. thank you
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jerry washington wrote:but I dont understand the purpose of this? or what is this useful for?



It allows you to write general code. If you write your code using supertype variables the code will work with objects of any subtype.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll remind you again about the JavaRanch naming policy. Please change your display name to conform with this policy. Thanks!
 
Sha Jar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:I'll remind you again about the JavaRanch naming policy. Please change your display name to conform with this policy. Thanks!



I already did. It was Tasha first so I changed to Natashia.

By the way David Newton I really enjoyed your show,

http://www.davidnewton.co.za/
 
I can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic