• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Multiple Inheritance

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying for the SCJP exam and I have bought the SCJP study guide by Kathy Sierra and Bert Bates.

In the first chapter when discussing inheritance, it mentions that an interface may extend one or more interfaces.

I was under the impression that Java did not support multiple inheritance, so I just wanted to check that "inheritance" is actually treated differently for interfaces ...

Many thanks in advance!
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Java doesn't support multiple inheritance.

The problem with multiple inheritance was with instance variables.
As interfaces don't have any variables (only constants), no problem with implementing multple interfaces.

Interface is the Java solution for multiple inheritance.
 
John Kaye
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies! I read ahead a chapter or so and there is a section confirming that Interfaces may extend multiple Interfaces!

Will try to research my questions a little more in the future! Still, at least I know I will remember this now!



 
John Kaye
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Surendra!

You got your response in just before I managed to correct myself!

 
author
Posts: 23873
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java language does use the "extends" keyword to add methods to an interface. And it is possible to extend from more than one interface at the same time.

However, this is not inheritence. This is just a case where they didn't want to use another keyword.

Henry
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.....extend and implements are keywords more than they are strictly concepts. Although the point about attributes above is 100% valid (yes, interfaces cannot have attributes and abstract or other base classes can) OO programming is more that just about attributes.

From a polymorphism perspective, Sun can say what they want and lots of people can read it and agree....but the ability to extend a class AND implement one or more interfaces is in fact a form, albeit limited, of multiple inheritance because by doing this, I can in my code certainly then treat a single object as an instance of multiple different kinds of Objects. Thats multiple inheritance.

And, IMHO, mutliple inheritance is bad. Swing is a classic example of this and the failure of Swing to be as great as it could be is a direct result of the multiple inheritance encouraged by interfaces. For example, I cannot imagine any button in the world on a screen that I wouldnt care when it was pressed. Rhetorically then, why must my class then implement an ActionListener in order to get that done?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, let me tell you that I studied the exam with the same book, so i can tell you that you have made a good choice!

Quite surprising what you have discovered, isn't it?
I had the same impression the first time i saw that one interface could extend many. Many times i had read that Java didn't support multiple inheritance but once, when i was working, i tried it (don't ask me why) and it worked!

Multiple inheritance is not supported for classes but it's allowed for interfaces. Nice, isn't it?
 
John Kaye
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the discussion around the topic everyone! It is much appreciated! I'm glad I picked out a good book.

I've been using Java for a while now, but as I am self taught and I generally pick things up as I go along, I only have knowledge of the concepts/techniques/specifics that I have been exposed to.

I thought that studying for the SCJP would help to fill in certain gaps in my knowledge ... which is proving to quite true!
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For example, I cannot imagine any button in the world on a screen that I wouldnt care when it was pressed. Rhetorically then, why must my class then implement an ActionListener in order to get that done?

Are you suggesting then that the Button itself decides what action needs to be taken once its pressed. IMHO that certainly violates separating the "dumb" GUI from the part of the application that houses the business logic.

Or maybe I'm missing the point of what you're saying. Perhaps this isn't a discussion for this forum as the OP's question has already been answered but I'd be interested in hearing your reasoning behind that statement.
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Garrett, I was making an oversimplified example of my overall point is all. I agree that the button should not know what to do itself.

But I do think that a panel that has a button should "know" that the button is going to be pressed. What else would I do with it? IMHO, this could be abstracted into some sort of class as part of the panel that handles this without my having to implement multiple interfaces.

For example, when using MFC in C++, when you add an OK button to a screen, it creates an onOK() method for you and you write your code there. Is the backside implementation better? I dont know, but probably not. But, they key is I dont have to think about what interface I want to implement, I just code my button action for that button in the method and all is handled for me. Thats nice, in part because part of the motication for OO is to abstract things such as to make end programming tasks easier. Thats what we are talking about here is OO. I dont mind knowing whats going on and coding most of this stuff explicitly. But I dont think its good OO.

The button example is a simple one and if you said its not a big deal to implement ActionListener and it isnt a bad thing, I'd agree. But when you have buttons, JTables and JTable cells and other components and have to implement mouselisteners, actionlisteners, componentlisteners, itemlisteners.....thats when it gets fairly bad IMHO. And thats a limitation of Swing also IMHO in the sense that the core design is flawed.

A better example of bad Swing design is JTable. What kind of table would I ever want that doesnt "know" when a user has edited its data?

I'd be interested in reading what you think on this
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic