Godcode
Godcode

Reputation: 141

Overriding a method

public class Testing extends JDialog {

    public MyClass myClass;

    public Testing() {

    }
}

given the above code, is it possible to override a method in myClass in Testing class?

say myClass has a method named computeCode(), will it be possible for me to override it's implementations in Testing? sorry it's been a long time since I've coded.

Upvotes: 0

Views: 1924

Answers (8)

Catchwa
Catchwa

Reputation: 5865

You override methods of classes that you extend. Therefore, in your example your Testing class could override the various existing methods of JDialog. If you wanted to override computeCode() from MyClass (assuming it's not final), you should make Testing extend MyClass.

public class Testing extends MyClass
{ 
    @Override
    public int computeCode()
    {
        return 1;
    }
} 

Upvotes: 2

HZhang
HZhang

Reputation: 223

Since Testing class has already inherited JDialog, there is no way let it inherit MyClass again unless to implement an interface. What you can do is to use some design pattern. However this is not overriding, since there is no inheritance. The Adapter is the one you need. Again you are losing the flexibility of polymorphism.

public class Testing extends JDialog {

 MyClass myClass = new MyClass();

 public Testing() {

 }

 public void methodA(){
     myClass.methodA();
 }

}

 class MyClass {
     public void methodA(){}
 }

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 384016

The wording of the question is confused and lost.

Here are some key points:

  • You can't @Override something that you didn't inherit to begin with
  • You can't @Override something that is final

Here's a small example:

import java.util.*;

public class OverrideExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>(
            Arrays.asList("a", "b", "c")
        ) {
            @Override public String toString() {
                return "I'm a list and here are my things : " + super.toString();
            }
        };
        System.out.println(list);
        // prints "I'm a list and here are my things : [a, b, c]"
    }
}

Here, we have an anonymous class that @Override the toString() method inherited from java.util.ArrayList.

Note that here, it's not class OverrideExample that overrides the ArrayList.toString(); it's the anonymous class that (implicitly) extends ArrayList that does.

Upvotes: 1

Ravindra Gullapalli
Ravindra Gullapalli

Reputation: 9188

All the above answers are valid. But, if you want to extend JDialog but still if you want to override a method of another class it is possible through interfaces. Interfaces won't have method definitions but will have method declarations. More about interfaces, you can read at http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

In your case, you can make use of interface like this

public interface MyInterface{
     public void myMethod();
}
public class Testing extends javax.swing.JDialog implements MyIterface{
     public void myMethod(){
          // code for your method
     }
}

Upvotes: 0

Kobi
Kobi

Reputation: 138147

Yes, it is generally possible (note that as others have correctly mentioned - you'd need to extend it to override the method). Refer to this sample:

public class Animal {
    public void testInstanceMethod() {
        System.out.println("The instance method in Animal.");
    }
}

public class Cat extends Animal {
    public void testInstanceMethod() {
        System.out.println("The instance method in Cat.");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        myAnimal.testInstanceMethod();
    }
}

Not only is it possible, but it is a key feature in polymorphism an code reusability.

Note, however, that MyClass.computeCode might be final - in this case, it cannot be overridden.

Upvotes: 2

Parth
Parth

Reputation: 1281

See, if you want to override method from MyClass then you need to extend it. As per your code, it seems you want to make a wrapper wround MyClass.

Wrapper means, calling implemented class method will call method of MyClass.

I am just clearing how wrapping works as below.

public class Testing extends JDialog {

    public MyClass myClass;

    public Testing() {

    }

    public void someMethod() {
       //Add some more logic you want...
      ...
      ..
       myClass.computeCode();

    }

}

thanks.

Upvotes: 1

Alex Martelli
Alex Martelli

Reputation: 882751

You can override a class's method only in a subclass (a class that extends the class whose method you want to override). However, given your skeletal code, you can (within Testing) have a nested class that extends MyClass and force an instance of that nested class into the myClass instance variable... so, the answer must be "yes".

Whether that's the best choice (as opposed to using interfaces, rather than subclassing concrete classes, and relying on Dependency Injection to get the implementations most suited for your testing), that's a different question (and my answer would be, unless you're testing legacy code that you can't seriously refactor until it's well test-covered... then, probably not;-).

Upvotes: 1

M.J.
M.J.

Reputation: 16676

if you want to override a method from MyClass then your testing class must extend that. for overriding a method one must complete IS-A relationship whereas your code comes under HAS-A relationship.

Upvotes: 2

Related Questions