aspose file tools*
The moose likes Java in General and the fly likes Interface, Multiple Inheritance, Use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface, Multiple Inheritance, Use?" Watch "Interface, Multiple Inheritance, Use?" New topic
Author

Interface, Multiple Inheritance, Use?

Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
In this program, I try to show the wastage of code. There are two windows in a room. We can see two different visuals from each of them. There are two locations from where we can see the both windows. From Location1 we can see visual1 and visual2 of Window1 and visual1 of Window2. And from Location2 we can see visual1 of Window1 and visual1 and visual2 of Window2.

In the class "Location1", method "window2visual1()" is wasted. Similarly, in the class "Location2", method "window2visual1()" is wasted. Here the program is small, but think, if it is big then?? There may be a useless coding of many lines. So my question is: Is there any way to avoid these kind of situation? Is it right to say this multiple inheritance because it inherits nothing except the method name? What is the real use of Interface(considering the big picture)?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

I think if you're going to discuss the use of interfaces intelligently, you should use real examples instead of making up contrived stuff like that. There are plenty of real examples of interfaces available, so I would encourage you to go and look at them.

That would also help to answer your bold-faced question "What is the real use of Interface" -- what you posted isn't it.
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
Paul, I tried to represent my problem through a simplest program and my all 3 questions are equally important. If you've understood my questions, why don't you answer. I'll be thankful to you.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

I think the problem is you're not modeling things right. I'm trying to imagine the visuals as statues outside the room. You can see some subset of the statues through each window, but it depends on where you're standing in the room, i.e., Location. So, let's say Statues A and B can be seen from Window 1, and B and C from Window 2. However, from Location I, you can only see A through Window 1 and C through Window 2, and from Location II you can see B through Window 1, and C through Window 2. Is that about how you're seeing the problem?

I'm not exactly sure how I'd model it, but I wouldn't have different interfaces for each window. Essentially, a window contains a collection of visuals (or statues in my case), and it might also contain something like a position, but there's no behavioral difference between the two windows. I also wouldn't have Location implement the Window interface, because what that says is, "A Location is a Window", which doesn't make sense. I'd probably have Location contain a position too, and somewhere have code to compute based on the Location's position, the Window's position, and I guess the statue's position as well, whether or not that statue was visible from the Location through the Window.

In short, you're not getting wasted methods because multiple inheritance, or a class that implements multiple interfaces, never makes sense, but because you're not using it effectively in this particular example.
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
Thanks! Greg. Yup you can take 'Visual' as a 'Statue'. In this program there are 4 different statues(2 in each window). For example: Windows1 contains(we can see through) the statue of a Dog(Visula1) and a Cat(Visual2), similarly Windows2 contains(we can see through) the statue of a Man(Visula1) and a Woman(Visual2). From the Location1 we can see the statues of Dog, Cat and Man and from the Location2 we can see the statues of Man, Woman and Cat. I hope you understand the program now. Please try to consider all the 3 questions.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

Why is it a useless function? Sure, you didn't have it do anything, but I wouldn't say that is useless. You could have just as easily implemented it like this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
fred rosenberger wrote:Why is it a useless function? Sure, you didn't have it do anything, but I wouldn't say that is useless.

Thanks! Fred.. I just use this program as a model to state the problem(which is the extra/useless code). Here my point is: if we implement an Interface into a class, we must have to give a body to all the functions/methods into that class regardless of their need.
Now I refer you to my questions:
Robby Ames wrote:Here the program is small, but think, if it is big then?? There may be a useless coding of many lines. So my question is: Is there any way to avoid these kind of situation? Is it right to say this multiple inheritance because it inherits nothing except the method name? What is the real use of Interface(considering the big picture)?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

I will just repeat what I said before: if you want to know the real use of interfaces, there is no shortage of examples of the real use of interfaces. And I suggest that very little can be learned from examples which aren't based on real-life requirements.

If you want to see examples of "multiple inheritance" via implementing several interfaces, then looking at a Swing program which implements several of the interfaces used to deal with user interaction would be a good start. I would suggest ActionListener and MouseMotionListener as examples.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

Robby Ames wrote:
fred rosenberger wrote:Why is it a useless function? Sure, you didn't have it do anything, but I wouldn't say that is useless.

Thanks! Fred.. I just use this program as a model to state the problem(which is the extra/useless code). Here my point is: if we implement an Interface into a class, we must have to give a body to all the functions/methods into that class regardless of their need.


Interface should define A behavior. The methods in the interface should be related to that behavior. You shouldn't be putting unrelated methods into an interface. If you have 2 differrent behaviors, define 2 intrfaces. Classes implement only the interfaces that describe their behavior
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
Paul Clapham wrote:I will just repeat what I said before: if you want to know the real use of interfaces, there is no shortage of examples of the real use of interfaces. And I suggest that very little can be learned from examples which aren't based on real-life requirements.

If you want to see examples of "multiple inheritance" via implementing several interfaces, then looking at a Swing program which implements several of the interfaces used to deal with user interaction would be a good start. I would suggest ActionListener and MouseMotionListener as examples.


Thanks! Paul.. Paul, it would be helpful if you provide me a link to the concerned page keeping in mind that I'm a Greenhorn(Beginner). Thanks! again.
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
Jayesh, First of all I'll Thank! You for your reply on my previous post. Your example was really good.
Jayesh A Lalwani wrote:
Interface should define A behavior. The methods in the interface should be related to that behavior. You shouldn't be putting unrelated methods into an interface. If you have 2 differrent behaviors, define 2 intrfaces. Classes implement only the interfaces that describe their behavior

Jayesh, do you want to say that I should put the methods window1visual1,window1visual2, window2visual1 in one interface and window2visual1,window2visual2, window1visual2 in other interface?
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

Robby Ames wrote:
Paul Clapham wrote:I will just repeat what I said before: if you want to know the real use of interfaces, there is no shortage of examples of the real use of interfaces. And I suggest that very little can be learned from examples which aren't based on real-life requirements.

If you want to see examples of "multiple inheritance" via implementing several interfaces, then looking at a Swing program which implements several of the interfaces used to deal with user interaction would be a good start. I would suggest ActionListener and MouseMotionListener as examples.


Thanks! Paul.. Paul, it would be helpful if you provide me a link to the concerned page keeping in mind that I'm a Greenhorn(Beginner). Thanks! again.


Paul has provided links. Click on ActionListener or MouseMotionListener to be taken there ;)
But you should try the Swing Tutorials on the Oracle page.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7042
    
  16

Robby Ames wrote:Jayesh, do you want to say that I should put the methods window1visual1,window1visual2, window2visual1 in one interface and window2visual1,window2visual2, window1visual2 in other interface?

I hate to say, but you're still stuck trying to understand an important concept through your own "dreamt-up" interfaces which, as Paul has already said more than once, don't bear any relation to something you'd try to do (or model) in real life.

My suggestion: Chuck those interfaces of yours in the bin, and look at the ones that he's pointed you to.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
Thanks! Winston, Jan, Jayesh, Fred, Paul and Greg for your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface, Multiple Inheritance, Use?
 
Similar Threads
Getting Session on web service client
usage of "this"
JSF 2.0 MyConverter.getAsObject(...) how to get a handle on the backing bean
Wake up a thread
Give the easiest example which differentiate "interface" and "abstract class" in Java.