Answer:
Yes.
Overriding abstract Methods
An abstract class will usually contain abstract methods. An abstract method definition consists of:
- optional access modifier (
public, private,and others), - the reserved word
abstract, - the class of the return value,
- a method signature,
- a semi-colon.
No curly braces or method body follow the signature.
Here is the abstract class
Parent including the abstract compute() method:
abstract class Parent
{
public abstract int compute( int x, String j);
}
If a class has one or more abstract methods
it must be declared to be abstract.
An abstract class may have methods that are not abstract
(the usual sort of method).
These methods are inherited by children classes in the usual way.
A non-abstract child class of an abstract parent class
must override each of the abstract methods
of its parent.
- A non-abstract child must override
each abstract method inherited from its parent by
defining a method with the same signature
and same return type.
- Objects of the child class will include this method.
- A child may define additional methods
with signatures different from the parent's method.
- Child objects will include these methods in addition to the first one.
- It is an error if a child defines a method with the same signature as a parent method, but with a different return type.
These rules are not really as terrible as they seem.
After working with inheritance for a while the rules will seem clear.
Here is a child of Parent:
class Child extends Parent
{
public int compute( int x, String j )
{ . . . }
}
The child's compute() method correctly overrides the parent's abstract method.
QUESTION 3:
Does the following correctly override the parent's abstract method?
class Child extends Parent
{
public double compute( int x, String j )
{ . . . }
}