• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Abstract methods.

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Clippy?!
 
Campbell Ritchie
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does clippy mean?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What does clippy mean?


http://en.wikipedia.org/wiki/Office_Assistant
 
Campbell Ritchie
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nonononononononono. Not that clippy!!!
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic