How do you override a method from another class?
In Java, when a subclass contains a method that overrides a method of the superclass. Example:
class A {
public void message() {
System.out.println("Hi");
}
}
public class B extends A {
@Override
public void message() {
System.out.println("Hello");
}
}
Class A represents the superclass and implements a method call message(). The subclass called B inherits every method that could be in the A class. However, class B overrides the method message(), replacing its functionality from A.
A parking = new A();
parking.message(); // Prints "Hi"
A dates = new B(); // Polymorphism
dates.message(); // Prints "Hello"
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
JavaJSPJSF