Week4 - Week 4 Inheritance and Polymorphism CS 180 Sunil...

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

View Full Document Right Arrow Icon
Week 4 Inheritance and Polymorphism CS 180 Sunil Prabhakar Department of Computer Science Purdue University
Background image of page 1

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

View Full DocumentRight Arrow Icon
Objectives Understand Inheritance expressing inheritance: extends visibility and inheritance: protected overriding, final constructors and inheritance: super Polymorphism polymorphic messages instanceof operator abstract classes & methods: abstract 2
Background image of page 2
3 Introduction Inheritance and polymorphism are key concepts of Object Oriented Programming. Inheritance facilitates the reuse of code. A subclass inherits members (data and methods) from all its ancestor classes. The subclass can add more functionality to the class or replace some functionality that it inherits. Polymorphism simplifies code by automatically using the appropriate method for a given object. Polymorphism also makes it easy to extend code.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Inheritance 4
Background image of page 4
5 Sample application Banking Example: There are two types of accounts: checking and savings. All accounts have a number, and an owner (with name, and a Social Security number), and balance. There are different rules for interest and minimum balance for checking accounts and savings accounts. How should we model this application? Two classes, one for each type of account? Have to repeat code for common parts. can lead to inconsistencies, harder to maintain. Create three classes: Account; SavingsAccount, and CheckingAccount
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Inheritance A superclass corresponds to a general class, and a subclass is a specialization of the superclass. E.g. Account, Checking, Savings. Behavior and data common to the subclasses is often available in the superclass. E.g. Account number, owner name, data opened. Each subclass provides behavior and data that is relevant only to the subclass. E.g. Minimum balance for checking a/c, interest rate and computation for savings account. The common behavior is implemented once in the superclass and automatically inherited by the subclasses.
Background image of page 6
Inheritance In order to inherit the data and code from a class , we have to create a subclass of that class using the extends keyword. public class SavingsAccount extends Account { SavingsAccount will inherit the data members and methods of Account. SavingsAccount is a sub ( child , or derived ) class; Account is a super ( parent or base ) class. A parent (of a parent . ..) is an ancestor class. A child (of a child . ..) is a descendant class. 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 The Account class class Account { protected String ownerName; protected int socialSecNum; protected float balance; public Account () { this ( “Unknown”, 0, 0.0 ) ; } public Account ( String name, int ssn ) { this ( name, ssn, 0.0 ) ; } public Account ( String name, int ssn, float bal ) { ownerName = name; socialSecNum = ssn; balance = bal; } public String getName ( ) { return ownerName; } public String getSsn ( ) { return socialSecNum; } public float getBalance () { return balance; } public void setName ( String newName ) { ownerName = newName; } public void accrueInterest () { System.out.println(“No interest”); } public void deposit ( float amount ) { balance += amount; } }
Background image of page 8
9
Background image of page 9

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

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

This note was uploaded on 03/26/2012 for the course STAT 350 taught by Professor Staff during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 48

Week4 - Week 4 Inheritance and Polymorphism CS 180 Sunil...

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

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