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

Lecture 9

The document discusses inheritance in object-oriented programming. It introduces a superclass called GeometricObject and subclasses like Circle and Rectangle. It explains that a superclass's constructors are not inherited by subclasses, but can be invoked using the super keyword. The document provides examples of constructor chaining when creating objects in subclasses.

Uploaded by

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

Lecture 9

The document discusses inheritance in object-oriented programming. It introduces a superclass called GeometricObject and subclasses like Circle and Rectangle. It explains that a superclass's constructors are not inherited by subclasses, but can be invoked using the super keyword. The document provides examples of constructor chaining when creating objects in subclasses.

Uploaded by

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

Motivations

• Suppose you will define classes to model circles,


Lecture 10
rectangles, and triangles.
Inheritance
• 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, Eighth Edition, (c) 2011 Pearson Education, Inc. All 1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 2
rights reserved. 0132130807 rights reserved. 0132130807

Superclasses and Subclasses


GeometricObject
Are superclass’s Constructor Inherited?
-color: String The color of the object (default: white).
-filled: boolean
-dateCreated: java.util.Date
Indicates whether the object is filled with a color (default: false).
The date when the object was created.
• No. They are not inherited.
+GeometricObject() Creates a GeometricObject.
+GeometricObject(color: String,
filled: boolean)
Creates a GeometricObject with the specified color and filled
values. • They are invoked explicitly or implicitly.
+getColor(): String Returns the color.


+setColor(color: String): void Sets a new color.
+isFilled(): boolean Returns the filled property. Explicitly using the super keyword.
+setFilled(filled: boolean): void Sets a new filled property.
+getDateCreated(): java.util.Date
+toString(): String
Returns the dateCreated.
Returns a string representation of this object.
• A constructor is used to construct an instance of a class.
• Unlike properties and methods, a superclass's constructors are
Circle Rectangle
-radius: double -width: double not inherited in the subclass.
+Circle() -height: double
+Circle(radius: double)
+Circle(radius: double, color: String,
+Rectangle()
+Rectangle(width: double, height: double)
• They can only be invoked from the subclasses' constructors,
filled: boolean)
+getRadius(): double
+Rectangle(width: double, height: double
color: String, filled: boolean) using the keyword super.
+setRadius(radius: double): void +getWidth(): double
+getArea(): double
+getPerimeter(): double
+setWidth(width: double): void
+getHeight(): double • If the keyword super is not explicitly used, the superclass's
+getDiameter(): double
+printCircle(): void
+setHeight(height: double): void
+getArea(): double
no-arg constructor is automatically invoked.
+getPerimeter(): double

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 4
rights reserved. 0132130807 rights reserved. 0132130807
Superclass’s Constructor Is Always Invoked
Using the Keyword super
A constructor may invoke an overloaded constructor or its
superclass’s constructor. If none of them is invoked The keyword super refers to the superclass
explicitly, the compiler puts super() as the first statement of the class in which super appears. This
in the constructor. For example, keyword can be used in two ways:

public A() { public A() {


• To call a superclass constructor
is equivalent to
super();
}
}
• To call a superclass method

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


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

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 6
rights reserved. 0132130807 rights reserved. 0132130807

Constructor Chaining
CAUTION Constructing an instance of a class invokes all the superclasses’ constructors
along the inheritance chain. This is called constructor chaining.
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
• You must use the keyword super to call public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
the superclass constructor. }
}

• Invoking a superclass constructor’s class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
name in a subclass causes a syntax error. }
System.out.println("(3) Employee's no-arg constructor is invoked");

public Employee(String s) {
• Java requires that the statement that uses }
System.out.println(s);

}
the keyword super appear first in the class Person {
constructor. public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 7 } Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 8
rights reserved. 0132130807 rights reserved. 0132130807
Trace Execution Trace Execution
public class Faculty extends Employee { public class Faculty extends Employee {
public static void main(String[] args) { public static void main(String[] args) {
new Faculty(); 1. Start from the new Faculty(); 2. Invoke Faculty
} main method } constructor
public Faculty() { public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked"); System.out.println("(4) Faculty's no-arg constructor is invoked");
} }
} }

class Employee extends Person { class Employee extends Person {


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

public Employee(String s) { public Employee(String s) {


System.out.println(s); System.out.println(s);
} }
} }

class Person { class Person {


public Person() { public Person() {
System.out.println("(1) Person's no-arg constructor is invoked"); System.out.println("(1) Person's no-arg constructor is invoked");
} }
} }
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 10
rights reserved. 0132130807 rights reserved. 0132130807

Trace Execution Trace Execution


public class Faculty extends Employee { public class Faculty extends Employee {
public static void main(String[] args) { public static void main(String[] args) {
new Faculty(); new Faculty();
} }

public Faculty() { public Faculty() {


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

public Employee(String s) { public Employee(String s) {


System.out.println(s); System.out.println(s);
} }
} }

class Person { class Person {


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

public Faculty() { public Faculty() {


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

class Employee extends Person { class Employee extends Person {


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

public Employee(String s) { public Employee(String s) {


System.out.println(s); System.out.println(s);
} }
} 5. Invoke Person() constructor }
6. Execute println
class Person { class Person {
public Person() { public Person() {
System.out.println("(1) Person's no-arg constructor is invoked"); System.out.println("(1) Person's no-arg constructor is invoked");
} }
} }
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 13 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 14
rights reserved. 0132130807 rights reserved. 0132130807

Trace Execution Trace Execution


public class Faculty extends Employee { public class Faculty extends Employee {
public static void main(String[] args) { public static void main(String[] args) {
new Faculty(); new Faculty();
} }

public Faculty() { public Faculty() {


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

class Employee extends Person { class Employee extends Person {


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

public Employee(String s) { public Employee(String s) {


System.out.println(s); System.out.println(s);
} }
} }
7. Execute println 8. Execute println
class Person { class Person {
public Person() { public Person() {
System.out.println("(1) Person's no-arg constructor is invoked"); System.out.println("(1) Person's no-arg constructor is invoked");
} }
} }
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 15 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 16
rights reserved. 0132130807 rights reserved. 0132130807
Trace Execution Example on the Impact of a Superclass without
public class Faculty extends Employee {
public static void main(String[] args) {
no-arg Constructor
new Faculty();
}

public Faculty() { Find out the errors in the program:


System.out.println("(4) Faculty's no-arg constructor is invoked");
}
} public class Apple extends Fruit {
9. Execute println }
class Employee extends Person {
public Employee() {
class Fruit {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked"); public Fruit(String name) {
} System.out.println("Fruit's 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, Eighth Edition, (c) 2011 Pearson Education, Inc. All 17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 18
rights reserved. 0132130807 rights reserved. 0132130807

Declaring a Subclass Calling Superclass Methods


You could rewrite the printCircle() method in the Circle class as
A subclass extends properties and methods from the follows:
superclass. You can also:
 Add new properties public void printCircle() {
 Add new methods System.out.println("The circle is created " +
super.getDateCreated() + " and the radius is " + radius);
 Override the methods of the superclass }

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 19 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 20
rights reserved. 0132130807 rights reserved. 0132130807
Overriding Methods in the Superclass NOTE
• A subclass inherits methods from a superclass.
• Like an instance method, a static method
• Sometimes it is necessary for the subclass to modify the
implementation of a method defined in the superclass. can be inherited.
• This is referred to as method overriding. • However, a static method cannot be
overridden.
public class Circle extends GeometricObject {
// Other methods are omitted
• If a static method defined in the
/** Override the toString method defined in GeometricObject */ superclass is redefined in a subclass, the
public String toString() {
return super.toString() + "\nradius is " + radius; method defined in the superclass is
}
}
hidden.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 21 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 22
rights reserved. 0132130807 rights reserved. 0132130807

NOTE Overriding vs. Overloading


public class Test { public class Test {
• An instance method can be overridden public static void main(String[] args) {
A a = new A();
public static void main(String[] args) {
A a = new A();
a.p(10); a.p(10);
only if it is accessible. }
a.p(10.0);
}
a.p(10.0);

} }

• Thus a private method cannot be class B {


public void p(double i) {
class B {
public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
overridden, because it is not accessible }
}
}
}

outside its own class. class A extends B {


// This method overrides the method in B
class A extends 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);
• If a method defined in a subclass is }
}
}
}

private in its superclass, the two methods


are completely unrelated.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 23 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 24
rights reserved. 0132130807 rights reserved. 0132130807
The Object Class and Its Methods The toString() method in Object
The toString() method returns a string representation of the
Every class in Java is descended from the object. The default implementation returns a string consisting
java.lang.Object class. If no inheritance is of a class name of which the object is an instance, the at sign
(@), and a number representing this object.
specified when a class is defined, the
superclass of the class is Object. Loan loan = new Loan();
System.out.println(loan.toString());
public class Circle { public class Circle extends Object {
... Equivalent
... 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, Eighth Edition, (c) 2011 Pearson Education, Inc. All 25 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 26
rights reserved. 0132130807 rights reserved. 0132130807

You might also like