CS100A, 15 Sept. 1998. Lecture 5 1 CS100A, 5 Sept. 1998 This lecture continues the discussion of classes. The important new concept of this lecture is.

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.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
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.
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.
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 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)
CSC 142 Computer Science II Zhen Jiang West Chester University
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.
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.
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.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
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, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
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,
CS1110 Classes, stepwise refinement 23 Sep 2009
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Polymorphism 2nd Lecture
Review of Object-Oriented Concepts in JAVA
Inheritance I Class Reuse with Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
CMSC 202 Inheritance.
Class Reuse with Inheritance
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 11 Inheritance and Polymorphism
Recitation 7 October 7, 2011.
Review of Object-Oriented Concepts in JAVA
Building Java Programs
Chapter 11 Inheritance and Polymorphism Part 1
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is inheritance. This will require to introduce constructors and to use the Java keywords extends, this, and super. Inheritance is motivated by extending class Employee of the previous lecture to distin- guish between kinds of employees: VIPs (the CEO and other major officers), salaried em- ployees, and regular employees. Each kind of employee requires different information in their record, and 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, 15 Sept Lecture 5 2 CS100A, 15 September 1998 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 define a subclass. Using the constructor of a super class. Access modifier protected Overriding methods Method toString() for every class Reading: Holmes --look in index and try to find appropriate pages to read

CS100A, 15 Sept 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 automatically used to convert it to a String.

CS100A, 15 Sept 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 pay;// The person's yearly 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; pay= s; hireDate= d; } Our old class Employee

CS100A, 15 Sept Lecture 5 5 // Raise the salary by p percent public void raiseSalary(double p) {pay= pay * (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 + " " + pay + " " + hireDate; } (old class Employee)

CS100A, 15 Sept Lecture 5 6 Modify class 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 salary. 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, 15 Sept 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, 15 Sept 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 pay would have to be changed. Instead, use a different approach: inheritance.

CS100A, 15 Sept Lecture 5 9 // 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; } // Change the bonus to p public void changeBonus(double p) {bonues= p;} }

CS100A, 15 Sept Lecture 5 10 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(“Cardie”,1994, 90000,1000); Employee Name Gries pay 0 hireDate 1969 Employee, hireYear, getName, toString x VIP Name Cardie pay hireDate 1994 Employee, hireYear, getName, toString bonus 1000 VIP changeBonus y

CS100A, 15 Sept Lecture 5 11 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 in same package --concept discussed much later!!!

CS100A, 15 Sept Lecture 5 12 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.getBonus() refers to method getBonus of VIP. This is because getBonus 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.