Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #3198

Re: Partially overriding a method?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Partially overriding a method?
Date Thu, 21 Apr 2011 21:10:07 -0400
Organization A noiseless patient Spider
Lines 74
Message-ID <ioqkil$3us$1@dont-email.me> (permalink)
References <ea560a6d-7156-4801-87c5-3b533b57c420@cu4g2000vbb.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Fri, 22 Apr 2011 01:10:46 +0000 (UTC)
Injection-Info mx01.eternal-september.org; posting-host="KiwfXDyOjqGhZBXcfNnZBg"; logging-data="4060"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1+9razjEzlvCQXV3XtUIK"
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9
In-Reply-To <ea560a6d-7156-4801-87c5-3b533b57c420@cu4g2000vbb.googlegroups.com>
Cancel-Lock sha1:NZAedB29/9hqSiBMaWGhG1Ta38c=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3198

Show key headers only | View raw


On 4/21/2011 11: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.
>
> 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.

     Right.  You've made the mistake (and you're not the first,
nor the last) of confusing overRIDING with overLOADING.  In your
first example, the `check' method of SubClass does not override
the `check' method of MainClass; is is an overload (of the `check'
identifier).  SubClass has two different methods named `check':

	void check(String) ... // inherited
	void check(Object) ... // defined locally

     In your second example things are quite different: SubClass
has only one `check' method:

	void check(Object) ... // overrides MainClass method

Note that this `check' has exactly the same signature as the
MainClass `check'; that's why it overrides.  In your first example
the two `check' methods have different signatures and have nothing
to do with each other, as you may see by experimenting with

	class HeroClass extends MainClass {
	    double check(short shrift, String along) ...
	}

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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