File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How many types of inheritance is supported by Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How many types of inheritance is supported by Java?" Watch "How many types of inheritance is supported by Java?" New topic
Author

How many types of inheritance is supported by Java?

Guy Emerson
Greenhorn

Joined: Dec 14, 2010
Posts: 27
Hi everybody,

How many types of inheritance are there in java ? As per my knowledge there are two types 1. class inheritance and 2. interface inheritance (with composition)

But on different forums I got different responses. As per sun's own directives how many are there?

Thanks and regards
Guy
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

One as far as I can tell.
Inheritance is Java happens when you use the extends keyword. Other techniques may help code reuse (composition, implementing interfaces) but isn't Inheritance.
Inheritance is something you get from your "Parent" ... if your parent happens to be "Object" you don't inherit a lot! Bad luck

http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Guy Emerson wrote:Hi everybody,

How many types of inheritance are there in java ?


There is no single correct answer to this question. If this was another interview question, I hope the interviewer was interested in how you explained your views on Java's inheritance, and not on a right or wrong answer. If he was looking for right or wrong, then he doesn't know what he's talking about, and I'd want to make sure he's not representative of the skill level at that place before taking a job there.

As per my knowledge there are two types 1. class inheritance and 2. interface inheritance (with composition)


Composition has nothing to do with inheritance. Otherwise, yes, I suppose you could say that extending a class and implementing an interface are two different "types" of inheritance. I wouldn't view it that way though. But again, this is a completely subjective question.


As per sun's own directives how many are there?


As far as I know, nothing official answers that question.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sam Mercs wrote:One as far as I can tell.
Inheritance is Java happens when you use the extends keyword.


Implementing an interface is also inheritance.

Other techniques may help code reuse (composition, implementing interfaces) but isn't Inheritance.


And inheritance is not about code reuse. That's a secondary concern, a side benefit. The main purpose of inheritance is type specialization and implementation.

Inheritance is something you get from your "Parent"


And any interfaces you implement are your parent types, as are all the classes you extend up through object.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Jeff Verdegan wrote:
Implementing an interface is also inheritance.


Some reference? I did a bit of looking around but I didn't get much. I did come across http://en.wikipedia.org/wiki/Interface_inheritance but it wasn't so convincing and wasn't particularly in the context of Java

Jeff Verdegan wrote:
Other techniques may help code reuse (composition, implementing interfaces) but isn't Inheritance.


And inheritance is not about code reuse. That's a secondary concern, a side benefit. The main purpose of inheritance is type specialization and implementation.


Yep I agree. In one sense Implementing the interface - provides a "specialization" of that interface, but no quite.
When I think interface I think contract - something I agree to do.
When I think inheritance I think of something "I am" and maybe I might refine(specialize).

Both are a type of "specialization" but I don't see implementing an interface as inheritance.

Code reuse is typical "way" to define inheritance though I agree its not the primary concern!

From wikipedia
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support.

http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29

Oracle Java documents distinguish between Inheritance and Interface
http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html
http://docs.oracle.com/javase/tutorial/java/concepts/interface.html

I guess its like you said,
There is no single correct answer to this question
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sam Mercs wrote:
Jeff Verdegan wrote:
Implementing an interface is also inheritance.


Some reference?


I don't have a reference. Just logic. First, as I stated, inheritance is primarily about specialization of type, not about code reuse. If you take code reuse out of class inheritance, you're left with the same thing you get with interfaces. Additionally, extending a purely abstract class is essentially no different than implementing an interface. (Yes, you can implement multiple interfaces, but not extend multiple classes, but that's not relevant here.) Finally, implementing an interface, extending an interface, and extending a class are all IS-A relationships--which is really the meat of inheritance.


Sam Mercs wrote:
Jeff Verdegan wrote:
Sam Mercs wrote: Other techniques may help code reuse (composition, implementing interfaces) but isn't Inheritance.


And inheritance is not about code reuse. That's a secondary concern, a side benefit. The main purpose of inheritance is type specialization and implementation.


Yep I agree. In one sense Implementing the interface - provides a "specialization" of that interface, but no quite.
When I think interface I think contract - something I agree to do.
When I think inheritance I think of something "I am" and maybe I might refine(specialize).


They're basically the same thing. In both cases, you're saying "I AM an X, and therefore I DO Y." The fact that in some cases when inheriting from a base class a parent type might provide an implementation and you may or may not override that implementation doesn't really change the fundamental nature of the relationship.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Jeff Verdegan wrote:
They're basically the same thing. In both cases, you're saying "I AM an X, and therefore I DO Y."



I see Interfaces mostly as HAS-A relationships.
e.g. a List is not "IS-A" Iterator. Sure I can cast a List to Iterator since it implements that interface - but that's just Java. Basically List isn't a Type of Iterator (even though the List instance of Iterator would be true ).

Consider a example of a Rectangle that Is-A Shape and Has-A Color --> Rectangle extends Shape implements Color
I would never say Rectangle IS-A color though I can do
  • Color c = (Color) rectangle and
  • rectangle instance of Color


  • The same argument goes for Thread class - why do we usually recommend implementing Runnable v/s extending Thread?
    Extending Thread = creating a special Type of Thread (Inheritance)
    Implementing Runnable = gives that class something it can do but does not make the class specialized Thread!

    Additionally, extending a purely abstract class is essentially no different than implementing an interface.


    For purely abstract classes - yes it's more interface like in implementation. (Difference exists? Yes, but lets leave that for a while)
    Honestly, I never thought of purely abstract classes, but they aren't used so much in Java. I've never come across a code base that has a purely abstract class, nor can I think of any in-build java classes that is purely abstract. All I'm saying is that purely abstract classes are not popular in Java!! To me it's more a remains from C++ where purely abstract class could be used in conjunction with virtual function to support multiple inheritance!

    The fact that in some cases when inheriting from a base class a parent type might provide an implementation and you may or may not override that implementation doesn't really change the fundamental nature of the relationship.

    True. I agree with what you said before too, defining Inheritance in terms of code-reuse, overloaded methods, overriding it's not purely correct.
    But that's how its popularly defined and so those words do come naturally!
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61451
        
      67

    Sam Mercs wrote:I see Interfaces mostly as HAS-A relationships.

    I could not disagree more. If it looks like a duck and quacks like a duck, it's a duck. It doesn't "have a" duck.

    e.g. a List is not "IS-A" Iterator. Sure I can cast a List to Iterator since it implements that interface

    No, it doesn't. It implements Iterable<E>.

    And so, yes, List is an Iterable. It implements all the methods that define what makes an Iterable, and so it is one.

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Guy Emerson
    Greenhorn

    Joined: Dec 14, 2010
    Posts: 27
    Thanks all for all your inputs. They were very insightful and enlightening.

    Guy Emerson
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Sam Mercs wrote:
    Jeff Verdegan wrote:
    They're basically the same thing. In both cases, you're saying "I AM an X, and therefore I DO Y."



    I see Interfaces mostly as HAS-A relationships.


    No, that's definitely wrong. Composition/aggregation is HAS-A. Implementing an interface is absolutely IS-A. IS-A is basically the Liskov Substitution principle. It means wherever I'm expecting X, I'll accept Y if it inherits from X (which in Java means "extends" OR "implements"), since inheritance means Y IS-AN X.

    For example, look at methods that accept List, Collection, or Comparable.

    e.g. a List is not "IS-A" Iterator.


    Of course not. Because it doesn't extend Iterator. It does extend Iterable, however, and therefore IS-A iterable, just as every ArrayList and LinkedList IS-A List and IS-A Iterable and IS-A Collection.

    Sure I can cast a List to Iterator since it implements that interface


    No you can't because no it doesn't.
     
    jQuery in Action, 2nd edition
     
    subject: How many types of inheritance is supported by Java?