O'Reilly logo

Java Quick Syntax Reference by Mikael Olsson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 13

image

Overriding

A member in a subclass can redefine a member in its superclass. This is most often done to give instance methods new implementations.

Overriding members

In the example below, Rectangle’s getArea method is overridden in Triangle, by redeclaring it there with the same method signature. The signature includes the name, parameters and return type of the method. However, the access level may be changed to allow for more access than the method being overridden.

class Rectangle{   public int w = 10, h = 10;  public int getArea() { return w * h; }}  class Triangle extends Rectangle{   public int getArea() { return w * h / 2; } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required