CS100A, Fall 1997. Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Inheritance Inheritance Reserved word protected Reserved word super
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation. Inheritance. Abstract Classes.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Intro to OOP with Java, C. Thomas Wu
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
CS100A, Fall Lecture 4 1 CS100A, Fall 1997 Lecture, Thursday, 12 September. This lecture continues the discussion of classes. In addition, the following.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
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,
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Review of Object-Oriented Concepts in JAVA
Inheritance I Class Reuse with Inheritance
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
CMSC 202 Inheritance.
Class Reuse with Inheritance
Chapter 11 Inheritance and Polymorphism
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 11 Inheritance and Polymorphism Part 1
CS100J Final Class on Classes 10 February 2005
CIS 110: Introduction to computer programming
Presentation transcript:

CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept of this lecture is inheritance. This will require us to study the Java keywords extends, this, and super. The idea of inheritance will be motivated by extending class Employee of the previous lecture to distinguish between kinds of employees: VIPs (the CEO and other major officers), salaried employees, and regular employees. Each kind of employee requires different information in their record, so there is different processing for each. Placing the information for all three kinds of employees in class Employee wastes space and causes complications in the code; using classes that are extensions of class Employee eliminates these problems.

CS100A, Fall Lecture 5 2 CS100A, Fall 1997, 11 September Concepts for this lecture: Inheritance: a subclass B of a class A inherits fields and methods from class A. A is a super-class of B. In Java, keyword extends is used to in defining a subclass. Using the constructor of a super class. Access modifier protected Overriding methods Method toString() for every class Reading: Lewis/Loftus,

CS100A, Fall Lecture 5 3 Method toString // Yield a String containing the data for // the person public String toString() {return name + " " + salary + " " + hireDate; } Convention: Every class implements method toString! In … + B (catenation), if B is not a String, method B.toString() is used to convert it to a String.

CS100A, Fall Lecture 5 4 // An instance of Employee contains a person's name, // salary, and year hired. It has a constructor and // methods for raising the salary, printing the data, and // retrieving the person's name and the year hired. public class Employee {public String name; // The person's name public double salary;// The person's salary public int hireDate;// The year hired // Constructor: a person with name n, salary s, and // year d hired public Employee(String n, double s, int d) {name= n; salary= s; hireDate= d; } Our old class Employee

CS100A, Fall Lecture 5 5 // Raise the salary by p percent public void raiseSalary(double p) {salary= salary * (1 + p/100.0);} // Yield the year the person was hired public int hireYear() {return hireDate;} // Yield the person's name public String getName() {return name;} // Yield a String containing the data for the person public String toString() {return name + " " + salary + " " + hireDate; } (old class Employee)

CS100A, Fall Lecture 5 6 Modify our record of employees to take into account three different kinds: VIPS (e.g. CEO, president, vice president): Need a field bonus, since VIPS get (big) bonuses. Get a yearly wage. Salaried: Expected to work overtime whenever necessary, without extra pay. Regular: Time cards! Have an hourly wage instead of a yearly salary. (Need also to record the number of hours worked on each day, but we’ll forego that here and assume 40 hours per week. Our task today

CS100A, Fall Lecture 5 7 How to implement? Add fields: int employeeKind; // 1 = VIP, 2 = salaried, // 3 = regular double bonus; // bonus for VIP (only) double hourlyWage; // (for regular // employees only // Set bonus to b (if employee is a VIP) public setBonus(double b) {if (employeeKind = 1) bonus= b; } A lot of other changes are required.

CS100A, Fall Lecture 5 8 Problems with this approach. Each employee’s record has a field for a bonus and for an hourly wage, even though it may not need it. Within the methods of class Employee that deal with salary and wages and hours, tests will have to be made to determine which kind of employee is being processed. This makes the code longer, less well-structured, and confusing. If a new kind of employee were to be added that required a different processing for pay, all the code that deals with salary would have to be changed. Instead, use a different approach: inheritance.

CS100A, Fall Lecture 5 9 // An instance of Employee contains a person's name, // yearly pay, and year hired. public class Employee {protected String name; // The person's name protected double pay;// The person's yearly pay protected int hireDate;// The year hired // Constructor: a person with name n and year d hired public Employee(String n, int d) {name= n; salary= 0; hireDate= d;} // Yield the year the person was hired public int hireYear() {return hireDate;} // Yield the person's name public String getName() {return name;} // Yield a String containing the data for the person public String toString() {return name + " " + pay + " " + hireDate;} }

CS100A, Fall Lecture 5 10 Employee x; x= new Employee(“Gries”, 1969); x Employee Name Gries pay 0 hireDate 1969 Employee, hireYear, getName, toString

CS100A, Fall Lecture 5 11 // An instance of VIP contains a VIP's data public class VIP extends Employee {private double bonus; // The VIP's bonus // Constructor: a person with name n, year d hired, // yearly pay s, and bonus b public VIP(String n, int d, double s, double b) {super (n,d); pay= s; bonus= b;} // Yield a String containing the data for the person public String toString() {return “VIP + " name + " " + pay + " " + " " + bonus + " " + hireDate; } // Raise the salary by p percent public void raiseSalary(double p) {pay= pay * (1 + p/100.0);} }

CS100A, Fall Lecture 5 12 An instance of class VIP has every field and method that an instance of class Employee does, plus the ones VIP declares. Employee x; x= new Employee(“Gries”, 1969); Employee y= new VIP(“Perkins”,1983, 90000,1000); Employee Name Gries pay 0 hireDate 1969 Employee, hireYear, getName, toString x VIP Name Perkins pay hireDate 1983 Employee, hireYear, getName, toString bonus 1000 VIP raiseSalary y

CS100A, Fall Lecture 5 13 Use of super For an instance of VIP. A constructor of super class Employee should be called to initialize the fields declared within Employee. This is as the first statement in a constructor for VIP: super (n,d); Use of protected If fields of Employee are public, they can be referenced from anywhere. If they are private, they can be referenced only from instances of Employee. If they are protected, they can be referenced only from Employee and its extensions (subclasses).

CS100A, Fall Lecture 5 14 Referencing methods VIP y; y= new VIP(“Perkins”, 1983, 90000, 1000); y.getName() refers to method getName of its superclass, Employee. This is because getName is not defined in VIP. y.raiseSalary() refers to method raiseSalary of VIP. This is because raiseSalary is defined in VIP. y.toString() refers to method toString of VIP. This is because toString is defined in VIP. Method toString of superclass Employee has been overridden in class VIP.