Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #3185
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!cu4g2000vbb.googlegroups.com!not-for-mail |
|---|---|
| From | "raphfrk@gmail.com" <raphfrk@gmail.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Partially overriding a method? |
| Date | Thu, 21 Apr 2011 08:31:46 -0700 (PDT) |
| Organization | http://groups.google.com |
| Lines | 66 |
| Message-ID | <ea560a6d-7156-4801-87c5-3b533b57c420@cu4g2000vbb.googlegroups.com> (permalink) |
| NNTP-Posting-Host | 137.71.226.54 |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Trace | posting.google.com 1303399907 5284 127.0.0.1 (21 Apr 2011 15:31:47 GMT) |
| X-Complaints-To | groups-abuse@google.com |
| NNTP-Posting-Date | Thu, 21 Apr 2011 15:31:47 +0000 (UTC) |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | cu4g2000vbb.googlegroups.com; posting-host=137.71.226.54; posting-account=iOAcbgoAAADC8J1iW-JMMSO74f5TboSg |
| User-Agent | G2/1.0 |
| X-HTTP-UserAgent | Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3,gzip(gfe) |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3185 |
Show key headers only | View raw
I was wondering if it is possible to override a method but only for
certain sub-classes of the method that the super-class supports.
For example:
class MainClass {
public static void main(String[] args) {
System.out.println("Started");
MainClass mc = new SubClass();
mc.check("Testing");
mc.check(7);
}
void check(Object x) {
System.out.println(x.toString());
}
}
class SubClass extends MainClass {
void check(String x) {
System.out.println("Sub class: " + x);
}
}
The call to mc.check() calls the main class's version of the method.
However, if I change the sub-class to:
class SubClass extends MainClass {
void check(Object x) {
System.out.println("Sub class: " + x);
}
}
then it uses the sub-class always.
One option would be something like:
class SubClass extends MainClass {
void check(Object x) {
if(x instanceof String) {
check((String)x);
} else {
super.check(x);
}
}
void check(String x) {
System.out.println("Sub class: " + x);
}
}
This would override and only use the sub-class for processing Strings.
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar
Partially overriding a method? "raphfrk@gmail.com" <raphfrk@gmail.com> - 2011-04-21 08:31 -0700
Re: Partially overriding a method? Lew <noone@lewscanon.com> - 2011-04-21 12:42 -0400
Re: Partially overriding a method? markspace <-@.> - 2011-04-21 11:37 -0700
Re: Partially overriding a method? Lew <noone@lewscanon.com> - 2011-04-21 15:58 -0400
Re: Partially overriding a method? Michal <kleku75@gmail.com> - 2011-04-21 21:00 +0200
Re: Partially overriding a method? Roedy Green <see_website@mindprod.com.invalid> - 2011-04-21 12:59 -0700
Re: Partially overriding a method? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-21 21:10 -0400
Re: Partially overriding a method? "raphfrk@gmail.com" <raphfrk@gmail.com> - 2011-04-24 09:39 -0700
csiph-web