Unit04A - 1 OOP A Deeper Look Inheritance, Interfaces, and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 OOP A Deeper Look Inheritance, Interfaces, and Abstract Classes 4A 2 Inheritance All classes inherit from the Object class in Java. All classes are arranged in a hierarchy (a tree, more about this later in the semester) with Object at the top of the hierarchy. A class that inherits from another is called a subclass . A class that provides attributes and methods for inheritance by subclasses is called a superclass . 3 Superclasses & Subclasses Object Shape Circle Polygon Triangle Rectangle Square 4 Inheritance keywords A class that extends another class is a subclass that inherits all fields and methods 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. 5 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() {...} } automatically inherits from Object if no other superclass is specified 6 Example: A subclass public class SavingsAccount extends BankAccount { private double interestRate; 7 Constructors public SavingsAccount(double initRate, double initBalance) { super(initBalance); interestRate = initRate; } public SavingsAccount(double initRate) { super(); interestRate = initRate; } 8 New method public void addInterest() { deposit(getBalance()*interestRate); } 9 Another way: protected In BankAccount: protected double balance; In SavingsAccount: public void addInterest() { deposit( balance *interestRate); } A field (or method) with protected visibility can be accessed in either the class that defines it, its subclasses or any class in the same package. 10 Overriding 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 same return type 11 Overriding Object methods Classes inherit methods from the Object class. These methods typically do not work properly for our specific subclasses so we must override them....
View Full Document

Page1 / 6

Unit04A - 1 OOP A Deeper Look Inheritance, Interfaces, and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online