This site requires JavaScript, please enable it in your browser!
Greenfoot back
Loopy
Loopy wrote ...

2012/5/26

Private vs Public

Loopy Loopy

2012/5/26

#
If a class has a method that is private but you want to do a similar action in another class, how would I do this without changing private to public?
erdelf erdelf

2012/5/26

#
copy the method
ttamasu ttamasu

2012/5/28

#
you can always try protected ... its kind of like private but can be accessible to all its decendents or subclasses.
kiarocks kiarocks

2012/5/28

#
Or you could get into complicated stuff if you wanted to access that particular method
plcs plcs

2012/5/28

#
Loopy wrote...
If a class has a method that is private but you want to do a similar action in another class, how would I do this without changing private to public?
Inheritance is the best way to achieve this behaviour by having a sub class extend that class and changing it from private to protected as ttamasu suggested. However, if they really cannot be compared/contrasted then you could copy the contents of it across. But it is most likely that those two classes will have some common connections/traits that either mean they will share the same abstract super class, or that one will extend the other.
You need to login to post a reply.