Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Partially overriding a method? Date: Thu, 21 Apr 2011 11:37:49 -0700 Organization: A noiseless patient Spider Lines: 40 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 21 Apr 2011 18:37:55 +0000 (UTC) Injection-Info: mx01.eternal-september.org; posting-host="8H8O82p94repkEapFAzX5w"; logging-data="23800"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uAgPST6xzRgCroAEV4gKAuwUTVSevdoM=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: Cancel-Lock: sha1:H/6pX4Gn9qQgkItQ0zsnTh7bML8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3189 On 4/21/2011 8:31 AM, raphfrk@gmail.com wrote: > 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. Yes and no. As you discovered, overriding is all or nothing. You either override or you don't. But you could add your own processing to do what you want with the "super" keyword. > > 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(Object x) { if( x instanceof String ) { System.out.println("Sub class: " + x); } else { super.check( x ); } > } > } > The code changes above are untested.