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


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

Re: Partially overriding a method?

From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: Partially overriding a method?
Date 2011-04-21 11:37 -0700
Organization A noiseless patient Spider
Message-ID <iopti2$n7o$1@dont-email.me> (permalink)
References <ea560a6d-7156-4801-87c5-3b533b57c420@cu4g2000vbb.googlegroups.com>

Show all headers | View raw


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.

<http://download.oracle.com/javase/tutorial/java/IandI/super.html>

>
> 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.

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