• Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple inheritance and method re-use?  RSS feed

 
S Fox
Ranch Hand
Posts: 68
1
Eclipse IDE Firefox Browser Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, is there a way to inherit and use methods from another class without extending it? I thought you can do this with an interface?
I tried an interface, but I don't want to override and re-implement everything. I don't want to copy/paste all the methods and make redundant code.
Heres an example of what I'm trying to do:

// classes B and C absolutely must extend A
// classes B and C need to inherit methods from the TimeStamp class

interface TimeStampInterface
class TimeStamp implements TimeStampInterface

public class A
class B extends A implements TimeStampInterface
class C extends B
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have the choice of implements or extends, so you could make an Abstract Class where everything is already implemented except some trivial method that you declare as Abstract. That way you get all the benefits for a lot less pain. Remember too that you can implement multiple interfaces, but only extend 1 class.

[edit]You can also make a class that implements all of the interfaces you want, then extend it in your other classes.[/edit]

S Fox wrote:Hello, is there a way to inherit and use methods from another class without extending it? I thought you can do this with an interface?
I tried an interface, but I don't want to override and re-implement everything. I don't want to copy/paste all the methods and make redundant code.
Heres an example of what I'm trying to do:

// classes B and C absolutely must extend A
// classes B and C need to inherit methods from the TimeStamp class

interface TimeStampInterface
class TimeStamp implements TimeStampInterface

public class A
class B extends A implements TimeStampInterface
class C extends B
 
S Fox
Ranch Hand
Posts: 68
1
Eclipse IDE Firefox Browser Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my example A, B, and C are nearly identical objects, they have an "is-a" relationship so they are extending. The timestamp represents a property that some of the subclasses need. I thought i had read somewhere before that you can use an instance of an object which implements a interface in order to call those methods? or a reference from one object to another and then call it's methods? I'm trying to learn the best way of dealing with this type of scenario in order to complete a homework project. The timestamp must be a seperate class to satisfy the homework requirements. I think that means an abstract class implementing everything wouldn't be allowed. duplicating code with override seems really wrong to me, but maybe it's the only correct solution?
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That actually seems pretty straight forward to me: Implement the TimeStamp in a Class, you can use an Interface to set up the signature for the TimeStamp, then make a base class that uses the TimeStamp appropriately, then Extend that class.
S Fox wrote:in my example A, B, and C are nearly identical objects, they have an "is-a" relationship so they are extending. The timestamp represents a property that some of the subclasses need. I thought i had read somewhere before that you can use an instance of an object which implements a interface in order to call those methods? or a reference from one object to another and then call it's methods? I'm trying to learn the best way of dealing with this type of scenario in order to complete a homework project. The timestamp must be a seperate class to satisfy the homework requirements. I think that means an abstract class implementing everything wouldn't be allowed. duplicating code with override seems really wrong to me, but maybe it's the only correct solution?
 
S Fox
Ranch Hand
Posts: 68
1
Eclipse IDE Firefox Browser Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I ended up doing except for using an interface to set the timestamp signature, that's a good idea. This was a database style project with lots of subclassed employee types. I don't really understand why java requires me to make a no-arg constructor for the superclass and certain subclasses. That allows you to create bad data and invalid objects. Do you think I should throw an exception from these constructors, is that better than creating database entries that are initialized with empty/default values?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really understand why java requires me to make a no-arg constructor for the superclass and certain subclasses.

There isn't a requirement like this. Could you explain more what's happening and maybe show use the code and any error?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:That actually seems pretty straight forward to me: Implement the TimeStamp in a Class, you can use an Interface to set up the signature for the TimeStamp, then make a base class that uses the TimeStamp appropriately, then Extend that class.

Actually, I think it's even easier, and I also think that S Fox may have made a mistake.

@S Fox: Correct me if I'm wrong, but isn't the reason why your classes need to implement TimeStamp because they all have a TimeStamp?

If I'm correct, then that relationship is a "HAS-A" relationship, not an "IS-A" one; and the answer is simple:
1. Create a an interface (DateTime?) that ALL your classes - including TimeStamp - implement.
2. Include a DateTime field in each of your A, B, C classes (or perhaps the "superest" class where all subclasses have timestamps), and initialize it with a TimeStamp object.
3. Implement your DateTime interface methods as "forwarders" to the methods in the DateTime/TimeStamp field - probably a 1-line implementation for each.
4. Wherever you need to use an object as a "timestamp", use the DateTime (interface) type, rather than TimeStamp.

Of course if I am wrong, then it probably needs another solution; but that's my first thought.

HIH

Winston
 
S Fox
Ranch Hand
Posts: 68
1
Eclipse IDE Firefox Browser Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I made a mistake earlier by thinking the properties of the timestamp object needed to be inherited. it's just a date really, not a full timestamp. All these classes are defined in the same file, i'm not sure if i should move them into their own seperate files or not.

I get compile errors without adding no-arg constructors Person(), Employee(), and Faculty().
java error: "Implicit super constructor Person() is undefined. Must explicitly invoke another constructor"
java error: "Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor"
java error: "The constructor Faculty() is undefined"

Heres what I ended up creating:


 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get compile errors without adding no-arg constructors Person(), Employee(), and Faculty().
java error: "Implicit super constructor Person() is undefined. Must explicitly invoke another constructor"
java error: "Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor"
java error: "The constructor Faculty() is undefined"

It's would be easier if I had the code in front of me, but let me take a guess at what's happening.

Because you explicitly defined a constructor, you no longer have a default constructor with no args.

Oops, you called for a no-arg constructor in Person by calling super() in Student. What you need is:

Am I close?
 
S Fox
Ranch Hand
Posts: 68
1
Eclipse IDE Firefox Browser Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes that's what's happening, so am I meant to always call the topmost constructor? what about objects farther down the inheritance chain, do i call every constructor in the chain til i reach my current object? i was not using the super keyword but i was calling the methods from super like this

constructors:
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't have a call to a 'super' as the first line of a constructor then the compiler inserts a call to the default constructor (super()) of the parent class.
 
S Fox
Ranch Hand
Posts: 68
1
Eclipse IDE Firefox Browser Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, well I added calls to the full arg versions of the super constructors now, and there's still a problem. it tells me Faculty needs a default constructor even though nothing extends it. and when i add the default constructor for Faculty, it then says:
"Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor."

I thought I understood what was going on, but now I know I don't!
 
S Fox
Ranch Hand
Posts: 68
1
Eclipse IDE Firefox Browser Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aha, I had a call to a no-arg Faculty constructor in my GUI which was not supposed to be there. I got rid of it and everything works! this makes the code much more readable with only calls to super constructors instead of having tons of setter methods in every single subclass constructor. big thanks to everyone for helping me!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!