• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface, Multiple Inheritance, Use?

 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10277
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Winston, Jan, Jayesh, Fred, Paul and Greg for your reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic