Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Chapter 1 Inheritance University Of Ha’il.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
ITM 352 Class inheritance, hierarchies Lecture #.
What is inheritance? It is the ability to create a new class from an existing class.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Topic 4 Inheritance.
John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.
Inheritance A Review of Objects, Classes, and Subclasses.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Chapter 13 - Inheritance.
Data Structures and Algorithms revision
Lecture 12 Inheritance.
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Computing with C# and the .NET Framework
CSC 205 Java Programming II
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Agenda About Homework for BPJ lesson 36 About practice of last class
Adapted from Java Concepts Companion Slides
Inheritance.
JAVA CLASSES.
Presentation transcript:

Inheritance CSC 171 FALL 2004 LECTURE 18

READING Read Horstmann, Chapter 11

Design Methodology 1. Problem Definition 2. Requirements Analysis 3. Architecture 4. Construction 5. Testing 6. Future Improvements

Classes and Objects Object oriented programs – Define classes of objects – Make specific object out of class definitions – Run by having the objects interact A class is a type of thing – Instructor An object is a specific thing – Ted An object is an instance of a class

Hierarchies Humans have found that organizing concepts into hierarchies a useful method of organizing information

HIERARCHIES Hierarchies are nested groupings. Examples of hierarchies Levels of organization in our bodies: – Organism: Organ Systems: Organs: Tissues: Cells: Organelles Ecology: – Biome: Community: Population: Organism Political boundaries: – USA: New York State: Monroe County: City of Rochester Notice how each group is completely subordinate to any group on its left.

Inheritance Hierarchies Object oriented languages, such as JAVA allows us to group classes into inheritance hierarchies. The most general classes are near the root – superclasses The more specific classes are near the leaves – Subclasses Subclasses inherit attributes from superclasses

Example: Banking Systems Consider a system that supports Savings and Checking accounts – What are the similarities? – What are the specifics

Accounts Both savings and Checking accounts support the idea of – Balance – Deposit – Withdraw Savings accounts pay interest checking accounts do not Checking accounts have transaction fees, savings accounts do not

Super & Sub classes More general concepts are in super classes More specific concepts are in sub classes Sub classes extend (inherit from) superclasses

Why inheritance? The power of inheritance is that sub-classes inherit the capabilities of the super-classes they extend This reduces code redundancy

Example: Banking systems Bank accounts are a type of object Savings accounts are a type of bank account Checking Accounts are bank accounts public class BankAccount {... } public class SavingsAccount extends BankAccount {...} public class CheckingAccount extends BankAccount {...}

BankAccount Instance variable “balance” Methods “deposit” and “withdraw”

public class BankAccount { public void deposit (double amount) { balance += amount; } public void withdraw(double amount) { if (amount <= balance) balance -= amount; } private double balance; }

SavingsAccount A bank account with an interest rate

public class SavingsAccount extends BankAccount { public SavingsAccount(double rate) { interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } private double interestRate; }

Inheritance and Methods Override method: Supply a different implementation of a method that exists in the superclass Inherit method: Don't supply a new implementation of a method that exists in the superclass Add method: Supply a new method that doesn't exist in the superclass

Inheritance and Fields Inherit field: All fields from the superclass are automatically inherited Add field: Supply a new field that doesn't exist in the superclass Can't override fields

Checking Account A Bank Account with transaction fees Need to keep tack of transactions

Checking Account public class CheckingAccount extends BankAccount { private int transactionCount; public CheckingAccount() { transactionCount = 0; } }

public void deposit(double amount) { transactionCount++; super.deposit(amount); } public void withdraw(double amount) { transactionCount++; super.deposit(amount); }

Excercise Define a base class “Employee” – Employees have names – Define constructor

public class Employee { private String name; public Employee(String name) { this.name = name; } public String name() { return name;} }

Excercise Define a sub class “HourlyEmployee” – Names, hourly rates, and hours worked – Define constructor, clockhours and “getPay()”

public class HourlyEmployee extends Employee { private double wage, hoursworked; public HourlyEmployee(String name, double wage) { super(name); this.wage = wage; hoursworked = 0; }

public void clockHours(double hours) { hoursworked += hours; } public double getPay() { double pay = hoursworked * wage; hoursworked = 0 ; return pay; }

Excercise Define a sub-class “SalaryEmployee” – Names and annual Salary – Define constructor, and “getPay()” (montly)

public class SalaryEmployee extends Employee { private double annualSalary; public SalaryEmployee(String name, double salary) { super(name); annualSalary = salary; }

public double getPay() { return annualSalary / 12; }

TERMINOLOGY A base class is often called a parent class. A sub-class is then called a child class (or derived class) Parents of parents are called ancestor classes Children of children are called descendent classes

Overriding vs. Overloading Overloading refers to having a different configuration of parameters for the same method name Overriding refers to the redefinition of a method in a subclass

ACCESS ISSUES private instance variables or methods in a super (base,parent) class are not accessible in sub (derived, child) classes. So, private methods and variable are effectively not inherited. – They are “there” but you have to use the super- class accessor and mutator methods.

Protected protected methods and variables can be accessed inside derived classes, or in any class in the same package.

Package Access If you don not place any of public, private, or protected before an instance variable or method definition, then it will have package access (also know as default access or friendly access). It will be visible to any class in the same package, but not outside.