CPCS 202
WEEK 11 WEEK 11
LECTURE 3
Inheritance (cont..)
Inheritance (cont..)
What Gets Inherited?
• All fields marked as “protected” or “public”
– “private” fields are only visible to the class that declared them
– “protected” fields are only visible to the class that declared them, and any subclasses
that declared them, and any subclasses
• All public and protected methods.
• A child class has access to the fields and methods in the parent class, depending on the access specifier.
Example
public class Employee {
…
protected int SSN;
private int number;
…
public class Salary extends Employee {
…
public float computePay() {
SSN = getSSN(); // legal
number++; // illegal
The toString() Method
• public String toString(). This method returns a string representation of the object.
• Representing an object as a String can be
• Representing an object as a String can be helpful for debugging or testing purposes.
• The Java documentation recommends that you add the toString() method to all of
your classes, a widely used practice in Java programming.
Example
public class Radio {
public int volume;
public double channel;
public char band;
public Radio(int v, double c, char b) {
volume = v;
channel = c;
channel = c;
band = b;
} }
public class ToStringDemo {
public static void main(String [] args) {
Radio radio = new Radio(7, 100.3, ‘F’);
System.out.println(“toString returns “ + radio.toString());
System.out.println(“Just printing the reference: “ + radio);
} }
Terminologies
• Overloading
– Creating methods with the same name but different signatures. Eg. constructor methods with different arguments or parameters
• Overriding
– The child class/subclass replaces the parent’s or the inherited method.
Method Overriding
• A child class can override a method that it inherits from a parent, thereby allowing the child class to add or change the behavior of the method in the parent class.
of the method in the parent class.
• When a child class overrides the parent class is is referred to as method
overriding and is a feature of OOP.
Rules for Method Overriding
• The return type, method name, and parameter list must be identical.
• The access specifier must be at least that of the parent. Eg. if the parent method is public, the child must be public. If the
public, the child must be public. If the
parent method is protected, the child must be protected or public (public allows more access than protected).
• The overriding exception cannot throw more exceptions than the parent.
Example
public class Radio {
public int volume;
public double channel;
public char band;
public Radio(int v, double c, char b) {
volume = v;
volume = v;
channel = c;
band = b;
}
public String toString() {
System.out.println(“Inside Radio toString”);
String rep = “Radio volume = “ + volume + “, channel = “ + channel + “ and band = “ + band;
return rep;
public class SuperClass {
public void method1 () { ... }
public void method1 (int param1) { ... }
public void method2 () { ... }
} }
public class SubClass extends SuperClass {
public void method1 () { ... }
public void method2 (int param2) { ... }
}
public class SuperClass {
public void method1 () { ... }
public void method1 (int param1) { ... }
public void method2 () { ... }
}
Override
}
public class SubClass extends SuperClass {
public void method1 () { ... }
public void method2 (int param2) { ... }
}
Overload
If a method in the child class has the same name as a method in the
parent class, but the child class method changes the parameter list, then
public class SuperClass {
public void method1 () { ... }
public void method1 (int param1) { ... }
public void method2 () { ... }
}
public class SubClass extends SuperClass {
public void method1 () { ... }
public void method2 (int param2) { ... }
} ...
SubClass bob = new SubClass();
Single versus Multiple Inheritance
• Some OOP languages (such as C++) allow a child class to have more than one parent;
however, this is not allowed in Java.
• A Java class can only have one parent. For example, the Salary class cannot extend both example, the Salary class cannot extend both the Employee class and a Manager class.
• Multiple inheritance is one of those capabilities that only tends to add confusion to a language.
• A Java class can have a parent class, and that parent class can have a parent, and so on.