aspose file tools*
The moose likes Beginning Java and the fly likes inheritence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritence" Watch "inheritence" New topic
Author

inheritence

jerry washington
Greenhorn

Joined: Apr 02, 2010
Posts: 5
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
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
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.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

"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


When you do things right, people won't be sure you've done anything at all.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Ewwww!

Except the giraffe, they're hot.
jerry washington
Greenhorn

Joined: Apr 02, 2010
Posts: 5
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

Did you read any of the Google hits?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Consider this, extending Janeice's example:
jerry washington
Greenhorn

Joined: Apr 02, 2010
Posts: 5
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(); ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
jerry washington
Greenhorn

Joined: Apr 02, 2010
Posts: 5
that i understood. thank you
Sha Jar
Greenhorn

Joined: Mar 02, 2010
Posts: 23
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'll remind you again about the JavaRanch naming policy. Please change your display name to conform with this policy. Thanks!
Sha Jar
Greenhorn

Joined: Mar 02, 2010
Posts: 23
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/
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: inheritence