0% found this document useful (0 votes)
8 views

Lecture 3

The document discusses inheritance in Java programming, explaining how new classes can be created by acquiring members from existing classes to avoid redundancy. It outlines the concepts of superclasses and subclasses, the is-a and has-a relationships, and the importance of constructors in the inheritance chain. Additionally, it emphasizes the use of the 'super' keyword to call superclass constructors and methods.

Uploaded by

papacog403
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 3

The document discusses inheritance in Java programming, explaining how new classes can be created by acquiring members from existing classes to avoid redundancy. It outlines the concepts of superclasses and subclasses, the is-a and has-a relationships, and the importance of constructors in the inheritance chain. Additionally, it emphasizes the use of the 'super' keyword to call superclass constructors and methods.

Uploaded by

papacog403
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Inheritance

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 1
Motivations
Suppose you will define classes to model circles,
rectangles, and triangles. These classes have many
common features. What is the best way to design
these classes so to avoid redundancy? The answer
is to use inheritance.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 2
Inheritance
➢A new class is created by acquiring an existing
class’s members and possibly embellishing
them with new or modified capabilities.
➢Can save time during program development by
basing new classes on existing proven and
debugged high-quality software.
➢Increases the likelihood that a system will be
implemented and maintained effectively.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
➢ When creating a class, rather than declaring completely
new members, you can designate that the new class should
inherit the members of an existing class.
➢ Existing class is the superclass
➢ New class is the subclass
➢ A subclass can be a superclass of future subclasses.
➢ A subclass can add its own fields and methods.
➢ A subclass is more specific than its superclass and
represents a more specialized group of objects.
➢ The subclass exhibits the behaviors of its superclass and
can add behaviors that are specific to the subclass.
➢ This is why inheritance is sometimes referred to as
specialization.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
➢ The direct superclass is the superclass from which
the subclass explicitly inherits.
➢ An indirect superclass is any class above the direct
superclass in the class hierarchy.
➢ The Java class hierarchy begins with class
Object (in package java.lang)
➢Every class in Java directly or indirectly
extends (or “inherits from”) Object.
➢ Java supports only single inheritance, in which
each class is derived from exactly one direct
superclass.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
➢ We distinguish between the is-a relationship
and the has-a relationship
➢ Is-a represents inheritance
➢In an is-a relationship, an object of a subclass
can also be treated as an object of its superclass
➢ Has-a represents composition
➢In a has-a relationship, an object contains as
members references to other objects

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Superclasses and Subclasses
➢ Figure 9.1 lists several simple examples of
superclasses and subclasses
➢Superclasses tend to be “more general” and
subclasses “more specific.”
➢ Because every subclass object is an object
of its superclass, and one superclass can
have many subclasses, the set of objects
represented by a superclass is typically
larger than the set of objects represented by
any of its subclasses.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Superclasses and Subclasses (Cont.)

➢ A superclass exists in a hierarchical relationship with its subclasses.


➢ Fig. 9.2 shows a sample university community class hierarchy
➢ Also called an inheritance hierarchy.
➢ Each arrow in the hierarchy represents an is-a relationship.
➢ Follow the arrows upward in the class hierarchy
➢ “an Employee is a CommunityMember”
➢ “a Teacher is a Faculty member.”
➢ CommunityMember is the direct superclass of Employee, Student
and Alumnus and is an indirect superclass of all the other classes in
the diagram.
➢ Starting from the bottom, you can follow the arrows and apply the is-a
relationship up to the topmost superclass.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Superclasses and Subclasses (Cont.)
➢ Fig. 9.3 shows a Shape inheritance hierarchy.
➢ Van follow the arrows from the bottom of the
diagram to the topmost superclass in this class
hierarchy to identify several is-a relationships.
➢A Triangle is a TwoDimensionalShape
and is a Shape
➢A Sphere is a ThreeDimensionalShape
and is a Shape.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Superclasses and Subclasses (Cont.)
➢ Not every class relationship is an
inheritance relationship.
➢ Has-a relationship
➢Create classes by composition of existing
classes.
➢Example: Given the classes Employee,
BirthDate and TelephoneNumber, it’s
improper to say that an Employee is a
BirthDate or that an Employee is a
TelephoneNumber.
➢However, an Employee has a BirthDate, and
an Employee has a TelephoneNumber.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Superclasses and Subclasses (Cont.)

➢ Objects of all classes that extend a common superclass can


be treated as objects of that superclass.
➢ Commonality expressed in the members of the
superclass.
➢ Inheritance issue
➢ A subclass can inherit methods that it does not need or
should not have.
➢ Even when a superclass method is appropriate for a
subclass, that subclass often needs a customized version
of the method.
➢ The subclass can override (redefine) the superclass
method with an appropriate implementation.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Superclasses and Subclasses
GeometricObject
-color: String The color of the object (default: white).
-filled: boolean Indicates whether the object is filled with a color (default: false).
-dateCreated: java.util.Date The date when the object was created.
+GeometricObject() Creates a GeometricObject.
+GeometricObject(color: String, Creates a GeometricObject with the specified color and filled
filled: boolean) values.
+getColor(): String Returns the color.
+setColor(color: String): void Sets a new color.
+isFilled(): boolean Returns the filled property.
+setFilled(filled: boolean): void Sets a new filled property.
+getDateCreated(): java.util.Date Returns the dateCreated.
+toString(): String Returns a string representation of this object.

Circle Rectangle
-radius: double -width: double
+Circle() -height: double
+Circle(radius: double) +Rectangle() GeometricObject
+Circle(radius: double, color: String,
filled: boolean)
+Rectangle(width: double, height: double)
+Rectangle(width: double, height: double
GeometricObject
+getRadius(): double color: String, filled: boolean) Circle

+setRadius(radius: double): void +getWidth(): double Circle


+getArea(): double +setWidth(width: double): void Rectangle
+getPerimeter(): double +getHeight(): double
+getDiameter(): double +setHeight(height: double): void
Rectangle
TestCircleRectangle
+printCircle(): void +getArea(): double
+getPerimeter(): double TestCircleRectangle
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 15
Are superclass’s Constructor
Inherited?
No. They are not inherited.
They are invoked explicitly or implicitly.
Explicitly using the super keyword.
A constructor is used to construct an instance of a class.
Unlike properties and methods, a superclass's
constructors are not inherited in the subclass. They can
only be invoked from the subclasses' constructors, using
the keyword super. If the keyword super is not explicitly
used, the superclass's no-arg constructor is
automatically invoked.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 16
Superclass’s Constructor Is Always Invoked
A constructor may invoke an overloaded constructor or its
superclass’s constructor. If none of them is invoked
explicitly, the compiler puts super() as the first statement
in the constructor. For example,

public A() { public A() {


is equivalent to
} super();
}

public A(double d) { public A(double d) {


// some statements is equivalent to
super();
} // some statements
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 17
Using the Keyword super
The keyword super refers to the superclass
of the class in which super appears. This
keyword can be used in two ways:
❑ To call a superclass constructor
❑ To call a superclass method

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 18
CAUTION

You must use the keyword super to call the


superclass constructor. Invoking a
superclass constructor’s name in a subclass
causes a syntax error. Java requires that the
statement that uses the keyword super
appear first in the constructor.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 19
Constructor Chaining
Constructing an instance of a class invokes all the superclasses’ constructors
along the inheritance chain. This is known as constructor chaining.
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 20
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); 1. Start from the
} main method
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 21
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); 2. Invoke Faculty
} constructor
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 22
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
} 3. Invoke Employee’s no-
class Employee extends Person {
arg constructor
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 23
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
4. Invoke Employee(String)
class Employee extends Person { constructor
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 24
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
} 5. Invoke Person() constructor
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 25
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}
6. Execute println
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 26
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}
7. Execute println
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 27
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}
8. Execute println
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 28
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
9. Execute println
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 29
Example on the Impact of a Superclass
without no-arg Constructor
Find out the errors in the program:
public class Apple extends Fruit {
}

class Fruit {
public Fruit(String name) {
System.out.println("Fruit's constructor is invoked");
}
}

Since no constructor is explicitly defined in Apple, Apple’s default no-arg


constructor is defined implicitly. Since Apple is a subclass of Fruit, Apple’s
default constructor automatically invokes Fruit’s no-arg constructor.
However, Fruit does not have a no-arg constructor, because Fruit has an
explicit constructor defined. Therefore, the program cannot be compiled.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 30
Defining a Subclass
A subclass inherits from a superclass. You can also:
❑ Add new properties
❑ Add new methods
❑ Override the methods of the superclass

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 31
Calling Superclass Methods
The syntax is
super.method(arguments);

You could rewrite the printCircle() method in the Circle class as


follows:

public void printCircle() {


System.out.println("The circle is created " +
super.getDateCreated() + " and the radius is " + radius);
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 32
Overriding Methods in the Superclass
A subclass inherits methods from a superclass. Sometimes it is
necessary for the subclass to modify the implementation of a method
defined in the superclass. This is referred to as method overriding.

public class Circle extends GeometricObject {


// Other methods are omitted

/** Override the toString method defined in GeometricObject */


public String toString() {
return super.toString() + "\nradius is " + radius;
}
}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 33
NOTE

An instance method can be overridden only


if it is accessible. Thus a private method
cannot be overridden, because it is not
accessible outside its own class. If a method
defined in a subclass is private in its
superclass, the two methods are completely
unrelated.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 34
NOTE

Like an instance method, a static method


can be inherited. However, a static method
cannot be overridden. If a static method
defined in the superclass is redefined in a
subclass, the method defined in the
superclass is hidden.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 35
Overriding vs. Overloading
Overloading means to define multiple methods with the same name
but different signatures. Overriding means to provide a new
implementation for a method in the subclass.
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.p(10); a.p(10);
a.p(10.0); a.p(10.0);
} }
} }

class B { class B {
public void p(double i) { public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
} }
} }

class A extends B { class A extends B {


// This method overrides the method in B // This method overloads the method in B
public void p(double i) { public void p(int i) {
System.out.println(i); System.out.println(i);
} }
} }

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 36
The Object Class and Its Methods
Every class in Java is descended from the
java.lang.Object class. If no inheritance is
specified when a class is defined, the
superclass of the class is Object.

public class Circle { public class Circle extends Object {


... Equivalent
...
} }

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 37
The toString() method in Object
The toString() method returns a string representation of the
object. The default implementation returns a string consisting
of a class name of which the object is an instance, the at sign
(@), and a number representing this object.

Loan loan = new Loan();


System.out.println(loan.toString());

The code displays something like Loan@15037e5 . This


message is not very helpful or informative. Usually you should
override the toString method so that it returns a digestible string
representation of the object.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 38
Designing with Composition vs.
Inheritance
➢ There’s much discussion in the software
engineering community about the relative
merits of composition and inheritance
➢ Each has its own place, but inheritance is
often overused and composition is more
appropriate in many cases
➢ A mix of composition and inheritance often
is a reasonable design approach.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Designing with Composition vs.
Inheritance (cont.)
➢ Inheritance-Based Designs
➢ Inheritance creates tight coupling among the classes in a
hierarchy
➢ Each subclass typically depends on its direct or indirect
superclasses’ implementations
➢ Changes in superclass implementation can affect the
behavior of subclasses, often in subtle ways
➢ Tightly coupled designs are more difficult to modify than
those in loosely coupled, composition-based designs
➢ Change is the rule rather than the exception—this
encourages composition
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Designing with Composition vs.
Inheritance (cont.)
➢ In general, use inheritance only for true is-a
relationships in which you can assign a
subclass object to a superclass reference
➢ When you invoke a method via a superclass
reference to a subclass object, the subclass’s
corresponding method executes
➢This is called polymorphic behavior, which we
explore in next chapter.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Designing with Composition vs.
Inheritance (cont.)
➢ Composition-Based Designs
➢ Composition is loosely coupled
➢ When you compose a reference as an instance
variable of a class, it’s part of the class’s
implementation details that are hidden from the
class’s client code
➢ If the reference’s class type changes, you may
need to make changes to the composing class’s
internal details, but those changes do not affect the
client code

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Designing with Composition vs.
Inheritance (cont.)
➢ Composition-Based Designs
➢ In addition, inheritance is done at compile time
➢ Composition is more flexible—it, too, can be done at
compile time, but it also can be done at execution time
because non-final references to composed objects can be
modified
➢ We call this dynamic composition
➢ This is another aspect of loose coupling—if the reference
is of a superclass type, you can replace the referenced
object with an object of any type that has an is-a
relationship with the reference’s class type

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Designing with Composition vs.
Inheritance (cont.)
➢ Composition-Based Designs
➢ When you use a composition approach instead of
inheritance, you’ll typically create a larger number of
smaller classes, each focused on one responsibility
➢ Smaller classes generally are easier to test, debug and
modify
➢ Java does not offer multiple inheritance—each class in
Java may extend only one class
➢ However, a new class may reuse the capabilities of one or
more other classes by composition. As you’ll see in next
chapter, we can get many of multiple inheritance's benefits
by implementing multiple interfaces
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
Chapter Summary
❑ You can define a new class from an existing class. This is
known as class inheritance. The new class is called a
subclass, child class, or extended class. The existing class is
called a superclass, parent class, or base class.
❑ A constructor is used to construct an instance of a class.
Unlike properties and methods, the constructors of a
superclass are not inherited in the subclass. They can be
invoked only from the constructors of the subclasses, using
the keyword super.
❑ A constructor may invoke an overloaded constructor or its
superclass’s constructor. The call must be the first statement
in the constructor. If none of them is invoked explicitly, the
compiler puts super() as the first statement in the constructor,
which invokes the superclass’s no-arg constructor.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 45
❑ To override a method, the method must be defined in the
subclass using the same signature and the same or
compatible return type as in its superclass.
❑ An instance method can be overridden only if it is
accessible. Thus, a private method cannot be overridden
because it is not accessible outside its own class. If a
method defined in a subclass is private in its superclass,
the two methods are completely unrelated.
❑ Like an instance method, a static method can be inherited.
However, a static method cannot be overridden. If a static
method defined in the superclass is redefined in a subclass,
the method defined in the superclass is hidden.
❑ Every class in Java is descended from the java.lang.Object
class. If no superclass is specified when a class is defined,
its superclass is Object.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 46

You might also like