aspose file tools*
The moose likes Beginning Java and the fly likes Abstract methods. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract methods." Watch "Abstract methods." New topic
Author

Abstract methods.

Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
How would I write an abstract method that would allow me to take in difffering objects?

For example if I had an abstract class like:



and I had two classes like:



and


How would I write setRepresentation in Room so that subclasses are required to have this method, but I'm permitted to place whatever in. If I have it where it takes in an Object, I get an error.

Thanks in advance.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
If the method setRepresentation is abstract, then in every Room child class the method override's signature must match the parent class exactly. In other words, every child of Room must have a setRepresentation method and it must take an Object parameter, not Character, not Image, but Object.

Having said this, even if your plan could work, it shouldn't be done as it breaks one of the rules of OOPs (though I can't remember which one!).

Edit: perhaps it's the Liskov substitution principle: Wikipedia: Liskov Substitution Principle
Edit2: Nope, it's not this. Hm, perhaps I'm wrong about this... Anyone?
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
Then what would be the best way to g about doing what I want?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Atrus Greyor wrote:Then what would be the best way to g about doing what I want?


I don't know. What is the purpose of this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36590
    
  16
It sounds as if you are trying to write a generic method.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Campbell Ritchie wrote:It sounds as if you are trying to write a generic method.


Of course! Why didn't I think of that??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Campbell Ritchie wrote:It sounds as if you are trying to write a generic method.

Clippy?!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36590
    
  16
What does clippy mean?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Campbell Ritchie wrote:What does clippy mean?

http://en.wikipedia.org/wiki/Office_Assistant
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36590
    
  16
Nonononononononono. Not that clippy!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract methods.
 
Similar Threads
Quetions abt Overriding a Method in a Concrete Class
When to use an interface VS when to use abstract class
Abstract
Java Basics
abstract class and method