1

I'm preparing to interview and think that would be good if my answer for the question like "Explain inheritance in java with an example" will be real implementation from core java classes. There are many examples like Animals hierarchic or Shape but I think it would be more wisdom represent real situation and try answer for question - why this implementation good for this situation.
With that You show that you have good knowledge not just inheritance but and core Java :))) So what do you think about that??

Addition: Good another article: What can be the bad example of inheritance in Java? But in this article question is opposite to my.

Community
  • 1
  • 1
dwz
  • 377
  • 1
  • 3
  • 13
  • Why don't you `grep` the API for the keyword `extends` and go over the results yourself? – mre Oct 24 '11 at 19:23
  • No problems, but I want to know what think about this question another programmers. This is very FAQ at job interview )) So it would be helpful for all – dwz Oct 24 '11 at 19:27
  • If we give you the answer, it won't mean you know and understand the Java core, but rather that learned by heart something that someone else told you. I suggest you Google it and try to learn from the various websites that show up in the search results. – Laf Oct 24 '11 at 19:28
  • This question does not have a certain answer, so I want to see some discussion =) – dwz Oct 24 '11 at 19:31

5 Answers5

3

I think the fact that every class has Object as a superclass is a good starting point.

mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    I don't. `Object` is the superclass of everything because there has to be something which is (well, i suppose there doesn't, but it's terribly convenient that there is). It's not an example which generalises terribly well. – Tom Anderson Oct 24 '11 at 20:07
2

I don't know what you exactly want to know but here is one inheritance example:

Collection<T> extends Iterable <T>

The Iterable interface makes it possible, that every data structure implementing it can be used with enhanced for loops. The Collection interface is the superinterface for most datastructures in Java. Getters can return a Collection to make an implementation more flexible. For internal data representation in the Object you could for example use Lists, Queues or others. You don't have to bother about other places you used the class when changing the implementation.

EDIT

So you want discussion.

I think, for an employer it is important, that you can use Java, not that you know how it works behind the API. The API just uses standart design concepts, not much really special. As a professional programmer, you should have understood them and know where to use them, but not know where they are used by other programmers.

Sibbo
  • 3,796
  • 2
  • 23
  • 41
  • The `implements` is an inheritence example?Really? – Cratylus Oct 24 '11 at 19:44
  • 1
    Thanks for your answer - very good) I mean not just show example but and explain benefits of using inheritance in that situation – dwz Oct 24 '11 at 19:44
  • 1
    Implements - is a inheritance ( @user384706) – dwz Oct 24 '11 at 19:48
  • Not really.It just implements.An interface can be extended but the `implements` by itself does not indicate inheritence per se – Cratylus Oct 24 '11 at 19:50
  • My example is about inheritance, and Collection inherits some methods from Iterable, so it inherits Iterable... – Sibbo Oct 24 '11 at 19:52
  • `Extends` means you extend the functionality/behavior of an `existing` class.Implements can be about a brand new class that implements an interface.There is a subtle difference – Cratylus Oct 24 '11 at 19:55
  • This little argument is predicated on a fallacy: `Collection` *does not* implement `Iterable`. Both are interfaces, and one interface cannot implement another. `Collection` extends `Iterable`. If someone thinks that this is not an inheritance relationship, they should argue their case. – Tom Anderson Oct 24 '11 at 20:10
  • Oh, right!Well I take half of the fallacy since I forgot this and just commented on the erroneous `implements` of the answer – Cratylus Oct 24 '11 at 20:23
  • But never the less, `implements` is not inheritence.But they are related concepts – Cratylus Oct 24 '11 at 20:56
  • Hm ok, changed my implements to extends, forgot that thing. – Sibbo Oct 25 '11 at 10:26
2

class Throwable is super class of all the Exceptions and Errors. And of course Throwable extends from Object.

Bhushan
  • 18,329
  • 31
  • 104
  • 137
1

You can also mention the String class which can not be extended to show you know also how to stop inheritence if you need to

Cratylus
  • 52,998
  • 69
  • 209
  • 339
1

The collections framework. It's good because it contains several kinds of inheritance:

  • List extends Collection, an example of interface inheritance
  • AbstractList implements List, which is implementation rather than inheritance, but is a related idea
  • ArrayList extends AbstractList, an example of class inheritance for implementation reuse

What i can't find an example of is class inheritance for the purpose of defining a subtype relationship. However, if you move sideways a little, the java.util package also contains:

  • GregorianCalendar extends Calendar
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133