Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

9.3 Overriding Methods

2 min readdecember 31, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Now that we know how to make subclasses and their constructors, it is time to write their methods. A will inherit all from the superclass. These methods will remain public in the . Most methods that are inherited from the superclass do not need to be rewritten.

Any method that we call on an object must be defined within the class associated with that object or within the superclass.

However, some methods will have to be rewritten because their implementations will be different. This can be done by , where the parameters for the methods are different but the methods achieve the same thing.

Overriding

The other way of doing this is , which is when the method will have the same parameters, but their implementations will be different. Above the method header, we have to write @Override to show that this is an overridden method. For an overridden method, we usually don't have to write since those should be inherited from the superclass.

A can override methods inherited from its superclass. A can also introduce new and methods that are not defined in the superclass.

In the Rectangle class, we will override the area method as follows:

/** Represents a rectangle
*/
public class Rectangle extends Quadrilateral {

  /** Makes a rectangle given a length and width
  */
  public Rectangle(double length, double width) {
    super(length, width, length, width);
  }

  @Override
  public double Area() {
    //from the constructor, length is sideOne and width is sideTwo
    return sideOne * sideTwo; 
  }
}

Key Terms to Review (7)

@Override

: The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.

Instance Variables

: Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.

Javadoc comments

: Javadoc comments are special comments in Java that begin with /** and end with */. They are used to generate documentation for classes, methods, and fields, making it easier for other developers (including yourself) to understand how to use them.

Overloading

: Overloading refers to defining multiple methods with the same name but different parameters within a single class. The methods must have different parameter lists or different types of parameters.

Overriding

: Overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The overridden method in the subclass replaces the implementation inherited from the superclass.

Public Methods

: Public methods are functions or procedures in a class that can be accessed and called from anywhere in the program. They are accessible to all other classes and objects.

Subclass

: A subclass is a class that inherits properties and behaviors from another class, called the superclass. It can add new features or modify existing ones.

9.3 Overriding Methods

2 min readdecember 31, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Now that we know how to make subclasses and their constructors, it is time to write their methods. A will inherit all from the superclass. These methods will remain public in the . Most methods that are inherited from the superclass do not need to be rewritten.

Any method that we call on an object must be defined within the class associated with that object or within the superclass.

However, some methods will have to be rewritten because their implementations will be different. This can be done by , where the parameters for the methods are different but the methods achieve the same thing.

Overriding

The other way of doing this is , which is when the method will have the same parameters, but their implementations will be different. Above the method header, we have to write @Override to show that this is an overridden method. For an overridden method, we usually don't have to write since those should be inherited from the superclass.

A can override methods inherited from its superclass. A can also introduce new and methods that are not defined in the superclass.

In the Rectangle class, we will override the area method as follows:

/** Represents a rectangle
*/
public class Rectangle extends Quadrilateral {

  /** Makes a rectangle given a length and width
  */
  public Rectangle(double length, double width) {
    super(length, width, length, width);
  }

  @Override
  public double Area() {
    //from the constructor, length is sideOne and width is sideTwo
    return sideOne * sideTwo; 
  }
}

Key Terms to Review (7)

@Override

: The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.

Instance Variables

: Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.

Javadoc comments

: Javadoc comments are special comments in Java that begin with /** and end with */. They are used to generate documentation for classes, methods, and fields, making it easier for other developers (including yourself) to understand how to use them.

Overloading

: Overloading refers to defining multiple methods with the same name but different parameters within a single class. The methods must have different parameter lists or different types of parameters.

Overriding

: Overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The overridden method in the subclass replaces the implementation inherited from the superclass.

Public Methods

: Public methods are functions or procedures in a class that can be accessed and called from anywhere in the program. They are accessible to all other classes and objects.

Subclass

: A subclass is a class that inherits properties and behaviors from another class, called the superclass. It can add new features or modify existing ones.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.