CPCS 202
The super Keyword
• Every object has a reference to itself called the this reference, and there are situations where a class needs to explicitly use the this reference when referring to its fields or
reference when referring to its fields or methods.
• Similarly, a class can use the super keyword to explicitly refer to a field or method that is inherited from a parent. You can think of
super as a child object’s reference to its
parent object.
public class Salary extends Employee {
public float salary; //Annual salary public float computePay()
{
System.out.println(“Computing salary pay for “ + super.name);
return salary/52;
} }
public void mailCheck() {
System.out.println(“Inside Salary mailCheck”);
super.mailCheck();
System.out.println(“Mailed check to “ + this.name + “ with salary “ + this.salary);
} }
The final Keyword
• The final keyword is used for creating constant variables.
• A final variable cannot be changed after it has
• A final variable cannot be changed after it has been assigned a value.
• Other two uses of the final keyword:
▫ Final class. A class can be declared final. A final class cannot be subclassed.
▫ Final method. A method can be declared final. A final method cannot be overridden.
Summary …
• Inheritance allows new class to extend an
existing class. The new class will inherit all the attributes and behaviour of the parent class.
attributes and behaviour of the parent class.
• The extends keyword is used to implement
inheritance. A class in Java can extend only one class.
• The parent object of a Java class is the
java.lang.Object, provided that it does not extends another class.
Summary
• Method overriding is when a child class contains the same method as its parent class.
• The super keyword is used by the child class to
• The super keyword is used by the child class to explicitly access the fields or methods of the
parent class.
• A final class cannot be extended. A final method cannot be overridden.