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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
 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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
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.
1 CS100J Classes, stepwise refinement 14 Feb 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
1 CS1110 Classes, stepwise refinement 12 Feb 2009 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
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.
Inheritance and Polymorphism
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
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,
Chapter 11 Inheritance and Polymorphism
CS1110 Classes, stepwise refinement 23 Sep 2009
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 9 Inheritance and Polymorphism
Inheritance 2nd Lecture
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Inheritance.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Inheritance 2nd Lecture
Chapter 11 Inheritance and Polymorphism Part 1
CS100J Classes, stepwise refinement 21 September 2005
CMSC 202 Inheritance II.
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

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 extends is used to define a subclass. Using the constructor of a superclass. Access modifier protected Overriding methods Readings from Weiss: classes, etc.: Sections subclasses: Section: packages: Section 3.6

Java boot camp2 // 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 { private String name; // The person's name private double pay;// The person's yearly salary private 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; } // = the person's name public String getName() {return name;} Class Employee

Java boot camp3 Class Employee, continued // Raise this Employee’s salary by p percent public void raiseSalary(double p) {pay= pay * (1 + p/100.0);} // = this Employee’s pay public double getPay() { return pay; } // = the year this Employee was hired public int getHireDate() {return hireDate;} // = a String containing the data for this Employee public String toString() { return name + " " + pay + " " + hireDate; }

Java boot camp4 Modify class Employee 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

Java boot camp5 Subclass VIP // An instance of VIP contains a VIP's data public class VIP extends Employee { private double bonus; // The VIP's bonus // Constructor: a VIP with name n, year d hired, // yearly pay s, and bonus b public VIP(String n, int d, double s, double b) { super (n, s, d); bonus= b; } // = a String containing the data for this VIP public String toString() { return “VIP ” + getName() + “ ” + getPay() + “ ” + getHireDate() + “ ” + bonus; } // Change this VIP’s bonus to b public void changeBonus(double b) {bonus= b;} }

Java boot camp6 An instance of class VIP has every field and method that an instance of class Employee does, plus the ones that are declared in VIP. Employee x; new Employee(“Gries”, 1969); Employee y= new VIP(“Hall”,1994, 90000,1000); x a0 y a1 Employee name Gries pay 0 hireDate 1969 Employee, hireYear, getName, getPay, toString. raiseSalary, getHireDate Employee name Hall pay hireDate 1994 Employee, hireYear, getName, getPay, toString raiseSalary, getHireDate bonus VIP, toString, changeBonus VIP a0 a1

Java boot camp7 // An instance of Salaried contains a salaried person’s data public class Salaried extends Employee { // Constructor: instance with name n, hire date d, yearly // pay s public Salaried(String n, double s, int d, double b) { super (n, s, d); } // = a String containing the data for the person public String toString() { return “Salaried “ + super.toString(); } // = yearly pay public double getPay() { return super.getPay(); } } Important points: (1) Overriding a method of the superclass. (2) Calling a constructor of the superclass. (3) calling other methods of the superclass.

Java boot camp8 // An instance of Hourly contains an hourly person’s data public class Hourly extends Employee { // private double hourlyPay; // pay for one hour // private int numHours; // no. hours worked in year /* Try writing suitable methods to finish this class. You need a suitable constructor, methods to access private fields, a toString method, and function getPay. Your constructor should initialize the inherited fields correctly!!! (to the product of hourlyPay and numHours). Place all four classes --Employee, VIP, Salaried, and Hourly-- into files and compile them in a Java project, so that you can get the syntax bugs out of your code. */ }

Java boot camp9 Use of protected A public field of Employee can be referenced from anywhere. A private field can be referenced only from instances of Employee. A protected field can be referenced only in the same package and in subclasses. A field without a modifier can be referenced only in the same package. Packages are not discussed now! For now, use protected for instance variables that you want a subclass to be able to reference.

Java boot camp10 Referencing methods VIP y; y= new VIP(“Hall”, 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. Rule: When looking for a nonstatic method in an instance, start at the bottom of the instance and search upwards. In other words, the method called is the lowest one in the class hierarchy in the instance.

Java boot camp11 Calling an overridden method public class VIP { private double bonus; //... // = a String containing the data for this VIP public String toString() { return “VIP ” + getName() + “ ” + getPay() + “ ” + getHireDate() + “ ” + bonus; } There is already toString in superclass Employee. How to call it? // = a String containing the data for this VIP public String toString() { return “VIP ” + super.toString() + “ ” + bonus; } To call an overridden method, prefix the method name with super. Principle: rely as much as possible on fields and methods of the superclass.

Java boot camp12 Summary of this Assume an instance a0 of some class contains a method m. Within the body of m: this refers to instance a0. (0) You can precede a reference to any field or method by “this.”, without changing its meaning: this.toString() is equivalent to toString() Also: // Set this Employee’s pay to pay public void setPay(double pay) { this.pay= pay; } (1) You can use this as an argument, to pass the name a0 as an argument: bigger(this, e) (2) You can use this to refer to another constructor: // Constructor: an Employee with name n, pay d, // and hire date 2001 public Employee(String n, double d) { this (n, d, 2000); }

Java boot camp13 Summary of super Assume an instance of some subclass C contains a method m. Within the body of m: super refers to the superclass of C. (0) You can precede a reference to a method by “super.” to refer to the method of the superclass (instead of the overriding method toString in C, if there is one). This is a way to override the overriding method. super.toString() (2) You can use super to refer to a constructor of the superclass, to initialize the fields of the superclass: // Constructor: a VIP with name n, pay d, // hire date 2001, and bonus b public VIP(String n, double d, int b) { super(n, d, 2001); bonus= b; }

Java boot camp14 Class Object; the class hierarchy Class Object is automatically the superclass of all classes that don’t extend anything. In our example, we have: Object Employee VIP Salaried Regular Class object has (at least) two methods // = “this Object has the same name as b // (we give a possible implementation) public boolean equals(Object b) { if ( b == null) return false; return this == b; } // = a String representation of this Object public String toString()