• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract methods.

 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then what would be the best way to g about doing what I want?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds as if you are trying to write a generic method.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It sounds as if you are trying to write a generic method.

Clippy?!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does clippy mean?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What does clippy mean?

http://en.wikipedia.org/wiki/Office_Assistant
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nonononononononono. Not that clippy!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic