Docsity

Inheritance and Abstract Classes in Java: Reid-Miller's 15-121 Fall 2020 Lecture Notes, Study notes of Object Oriented Programming

Partial preview of the text

Download Inheritance and Abstract Classes in Java: Reid-Miller's 15-121 Fall 2020 Lecture Notes and more Study notes Object Oriented Programming in PDF only on Docsity!

Inheritance & Abstract Classes

15 - 121 Fall 2020

Margaret Reid-Miller

Today

Hw7 is due Wednesday, Nov 4 at 11:55pm.

Today:

• Inheritance

• Abstract Classes

• (Clone)

Inheritance enables defining a new

class based on an existing class.

  • The key idea is to refine an existing class to derive a new class, by adding new fields, and adding new methods or redefining existing methods.
  • It enables code reuse because the new class inherits the all the fields and methods of the existing class.
  • The original class is called the superclass and the class based on the superclass class is called the subclass.

Inheritance

• Every class inherits (implicitly) from the Object

class in Java.

• Every class is-a Object

• There are no fields in Object, but there are

methods such as equals and toString.

• All classes are arranged in a hierarchy (a tree)

with Object at the top of the hierarchy.

Java's inheritance keywords

• A class that extends another class is a

subclass that inherits all fields and methods

(but not constructors) of the superclass.

• The subclass has direct access to all fields that

are public and protected.

• The subclass can override the definitions of

inherited methods with new implementations

(using the same signature) and can access

overridden methods using the super keyword.

Example: A superclass

public class BankAccount { private double balance; public BankAccount() {...} public BankAccount(double initBalance) {...} public void deposit(double amount) {...} public void withdraw(double amount) {...} public double getBalance() {...} public String toString() {...} } automatically inherits from Object if no other superclass is specified

Inheritance: extends

  • Fields of a subclass comes for two sources:
    • It inherits all the fields of all its ancestor classes.
    • It can define additional fields of its own.
  • That is, objects of the subclass may store more data than objects of the superclass.
  • Methods of a subclass can be defined as follows:
    • inherits from ancestor classes automatically,
    • override (redefine) inherited methods,
    • add instructions to inherited methods,
    • define new methods, possibly overloading inherited methods.

In the constructor, super() calls the

constructor of the superclass

public SavingsAccount(double initBalance, double initRate) { super (initBalance); interestRate = initRate; } public SavingsAccount(double initRate) { super (); interestRate = initRate; } must be first statement the superclass initializes balance

Field and Method visibility

  • Classes (and their parts) have visibility modifiers :
    • public : accessible to everyone
    • protected : inside package, inside class, inside subclass
    • package-private (default, no modifier used): inside package, inside class
    • private : accessible only within the class S

A subclass can access a field in an

ancestor class that is protected.

In BankAccount: protected double balance; In SavingsAccount: public void addInterest() { deposit( balance *interestRate); }

Use super. to call the superclass

method that is overridden.

  • A savings account subtracts a fee of $10 for withdrawals over $1000.
  • We need to override the withdraw method and provide a new implementation that is appropriate. public void withdraw(double amount) { if (amount > 1000.0) super.withdraw(amount + 10.0); else super.withdraw(amount); } same signature SavingsAccount class call to superclass method What happens if you forget to use super?

Exercise: Write a toString method for the

SavingsAccount class. Take advantage of

code reuse!

You should override the methods

inherited from Object class.

These methods typically do not work properly for our specific subclasses so we must override them. public boolean equals(Object obj) { SavingsAccount other = (SavingsAccount)obj; return this.interestRate == other.interestRate && this.balance == other.balance; } (assumes balance is protected in the BankAccount class) SavingsAccount class

Casting an Object type error

In the equals method, we used the following cast: SavingsAccount other = (SavingsAccount)obj; What if obj is not a SavingsAccount? A ClassCastException is thrown at runtime. We can use the instanceof operator to check the object's actual type during runtime to avoid the exception.