Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Nice way to override method in superclass, so that it's used in multiple subclasses?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Nice way to override method in superclass, so that it's used in multiple subclasses?

    I'm working with this library JFreeChart, though the question has nothing to do with it, just general Java programming. I'm fixing it up a little overriding methods in the middle of the class-hierarchy, for example I've changed methods in NumberAxis which is extended by both DateAxis and SymbolAxis.

    Is there a "nice" way to use these extended methods in both DateAxis and SymbolAxis without re-compiling the whole thing (or really just NumberAxis I suppose)? It feels like there should be some pattern for this as it doesn't seem too unusual?

    I suppose I could do something like this

    class A { ... }
     
    class B extends A { ... }
     
    class C extends A { ... }
     
    class D extends A {
     
    	private A BorC = null;
     
    	public D(A BorC) {
     
    		this.BorC = BorC;
     
    	}
     
    	protected void overriddenAfunction() { ... }
     
    	protected void overriddenBfunction() { 
     
    		((B) BorC).overriddenBfunction();
     
    	}
     
    	protected void overriddenCfunction() { 
     
    		((C) BorC).overriddenCfunction();
     
    	}
     
    }
     
    Usage:
     
    A testB = new D(new B());
    A testC = new D(new C());

    However if it even works I need to put in every function from B and C and call them from D.

    Now I've just extended SymbolAxis and overridden the method from NumberAxis there.
    Last edited by daver690; July 19th, 2012 at 09:05 AM.


Similar Threads

  1. Replies: 2
    Last Post: March 15th, 2012, 08:25 PM
  2. Overriding Superclass Static Methods, Using the Superclass Method
    By snowguy13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 21st, 2011, 07:30 AM
  3. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM
  4. override list add method?
    By ober0330 in forum Collections and Generics
    Replies: 10
    Last Post: July 17th, 2011, 07:34 PM
  5. Override class method
    By mekie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 07:06 PM