Answer to Question #57481 in Java | JSP | JSF for Nancy Rodriguez

Question #57481
How do you override a method from another class?
1
Expert's answer
2016-01-26T04:25:27-0500
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS