CLASS DESIGN. Objectives MODULE 4

Similar documents
Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

ENCAPSULATION AND POLYMORPHISM

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Polymorphism. return a.doublevalue() + b.doublevalue();

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Java Fundamentals (II)

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

INHERITANCE. Spring 2019

Inheritance -- Introduction

Introduction to Programming Using Java (98-388)

Super-Classes and sub-classes

CS260 Intro to Java & Android 03.Java Language Basics

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Practice for Chapter 11

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

Java Object Oriented Design. CSC207 Fall 2014

Java Magistère BFA

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

More about inheritance

What is Inheritance?

Inheritance (Part 5) Odds and ends

The class Object. Lecture CS1122 Summer 2008

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Introduction to Inheritance

Rules and syntax for inheritance. The boring stuff

CS-202 Introduction to Object Oriented Programming

AP CS Unit 6: Inheritance Notes

C12a: The Object Superclass and Selected Methods

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Computer Science II (20073) Week 1: Review and Inheritance

1 Shyam sir JAVA Notes

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Java: introduction to object-oriented features

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Class, Variable, Constructor, Object, Method Questions

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Lecture 4: Extending Classes. Concept

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Methods Common to all Classes

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Practice Questions for Chapter 9

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

ITI Introduction to Computing II

More On inheritance. What you can do in subclass regarding methods:

Inheritance and Polymorphism

ITI Introduction to Computing II

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

CMSC 132: Object-Oriented Programming II

CS313D: ADVANCED PROGRAMMING LANGUAGE

OVERRIDING. 7/11/2015 Budditha Hettige 82

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

CS Programming I: Inheritance

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Questions Answer Key Questions Answer Key Questions Answer Key

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Chapter 5 Object-Oriented Programming

CO Java SE 8: Fundamentals

index.pdf January 21,

Programming in C# Inheritance and Polymorphism

Overriding Variables: Shadowing

Making New instances of Classes

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

Java Classes, Inheritance, and Interfaces

First IS-A Relationship: Inheritance

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Overriding methods. Changing what we have inherited from e.g. Object

University of Palestine. Mid Exam Total Grade: 100

Exam Duration: 2hrs and 30min Software Design

Example: Count of Points

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Inheritance and Polymorphism

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Inheritance. Transitivity

Abstract Classes and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 11: Collections and Maps

Lecture Notes Chapter #9_b Inheritance & Polymorphism

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

Building Java Programs. Inheritance and Polymorphism

Programming Languages and Techniques (CIS120)

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

CS 251 Intermediate Programming Inheritance

Transcription:

MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors and other methods appropriately Use the instanceof operator to compare object types Use virtual method invocation Use upward and downward casts Override methods from the Object class to improve the functionality of your class

Using Access Control > You have seen the keywords public and private. There are four access levels that can be applied to data fields and methods. The following table illustrates access to a field or method marked with the access modifier in the left column. Modifier (keyword) Same Class Same Package Subclass in Another Package Universe private Yes default Yes Yes protected Yes Yes Yes * public Yes Yes Yes Yes > Classes can be default (no modifier) or public. Protected Access Control: Example 1 package demo; 2 public class Foo { 3 protected int result = 20; 4 int other = 25; 5 subclass-friendly declaration 1 package test; 2 import demo.foo; 3 public class Bar extends Foo { 4 private int sum = 10; 5 public void reportsum () { 6 sum += result; 7 sum += other; 8 9 compiler error

Field Shadowing: Example 1 package demo; 2 public class Foo2 { 3 protected int result = 20; 4 1 package test; 2 import demo.foo2; 3 public class Bar2 extends Foo2 { 4 private int sum = 10; 5 private int result = 30; 6 public void reportsum() { 7 sum += result; 8 9 resultfield shadows the parent's field. Access Control: Good Practice > A good practice when working with fields is to make fields as inaccessible as possible, and provide clear intent for the use of fields through methods. 1 package demo; 2 public class Foo3 { 3 private int result = 20; 4 protected int getresult() { return result; 5 1 package test; 2 import demo.foo3; 3 public class Bar3 extends Foo3 { 4 private int sum = 10; 5 public void reportsum() { 6 sum += getresult(); 7 8

Overriding Methods > Consider a requirement to provide a String that represents some details about the Employee class fields. 1 public class Employee { 2 private int empid; 3 private String name; 4 //... other fields and methods 5 public String getdetails () { 6 return "Employee id: " + empid + 7 " Employee name:" + name; 8 9 Overriding Methods > In the Manager class, by creating a method with the same signature as the method in the Employee class, you are overriding the getdetails method: 1 public class Manager extends Employee { 2 private String deptname; 3 //... other fields and methods 4 public String getdetails () { 5 return super.getdetails () + 6 " Department: " + deptname; 7 8 > A subclass can invoke a parent method by using the super keyword.

Invoking an Overridden Method > Using the previous examples of Employee and Manager: Employee e = new Employee (101,"Jim Smith","011-12-2345", 100_000.00); Manager m = new Manager (102, "Joan Kern", "012-23-4567", 110_450.54, "Marketing"); System.out.println (e.getdetails()); System.out.println (m.getdetails()); > The correct getdetails method of each class is called: Employee id: 101 Employee name: Jim Smith Employee id: 102 Employee name: Joan Kern Department: Marketing Virtual Method Invocation > What happens if you have the following? Employee e = new Manager (102, "Joan Kern", "012-23-4567", 110_450.54, "Marketing"); System.out.println (e.getdetails()); > During execution, the object s runtime type is determined to be a Manager object: Employee id: 102 Employee name: Joan Kern Department: Marketing > The compiler is satisfied because the Employee class has a getdetails method, and at run time the method that is executed is referenced from a Manager object. > This is an aspect of polymorphism called virtual method invocation.

Accessibility of Overridden Methods > An overriding method must provide at least as much access as the overridden method in the parent class. public class Employee { //... other fields and methods public String getdetails() {... public class Manager extends Employee { //... other fields and methods private String getdetails() { //... // Compile time error Applying Polymorphism > Suppose that you are asked to create a new class that calculates a stock grant for employees based on their salary and their role (manager, engineer, or admin): 1 public class EmployeeStockPlan { 2 public int grantstock (Manager m) { 3 // perform a calculation for a Manager 4 5 public int grantstock (Engineer e) { 6 // perform a calculation for an Engineer 7 8 public int grantstock (Admin a) { 9 // perform a calculation for an Admin 10 11 //... one method per employee type 12 not very object-oriented!

Applying Polymorphism > A good practice is to pass parameters and write methods that use the most generic form of your object as possible. public class EmployeeStockPlan { public int grantstock (Employee e) { // perform a calculation based on Employee data // In the application class EmployeeStockPlan esp = new EmployeeStockPlan (); Manager m = new Manager (); int stocksgranted = grantstock (m);... Applying Polymorphism > Adding a method to Employee allows EmployeeStockPlan to use polymorphism to calculate stock. public class Employee { protected int calculatestock() { return 10; public class Manager extends Employee { public int calculatestock() { return 20; public class EmployeeStockPlan { private float stockmultiplier; // Calculated elsewhere public int grantstock (Employee e) { return (int)(stockmultipier * e.calculatestock());

Using the instanceof Keyword > The Java language provides the instanceof keyword to determine an object s class type at run time. 1 public class EmployeeRequisition { 2 public boolean canhireemployee(employee e) { 3 if (e instanceof Manager) { 4 return true; 5 else { 6 return false; 7 8 9 Casting Object References > In order to access a method in a subclass passed as an generic argument, you must cast the reference to the class that will satisfy the compiler: 1 public void modifydeptformanager (Employee e, String dept) { 2 if (e instanceof Manager) { 3 Manager m = (Manager) e; 4 m.setdeptname (dept); 5 6 > Without the cast to Manager, the setdeptname method would not compile.

Casting Rules > Upward casts are always permitted and do not require a cast operator. Director d = new Director(); Manager m = new Manager(); Employee e = m; // OK Manager m = d; // OK // OK Employee e = d; Casting Rules > For downward casts, the compiler must be satisfied that the cast is at least possible. Employee e = new Manager(); Manager m = new Manager(); Manager m = (Manager)e; // Would also work if // e was a Director obj Director d = (Director)m; // fails at run time Engineer eng = (Engineer)m; // compiler error

Overriding Object methods > One of the advantages of single inheritance is that every class has a parent object by default. The root class of every Java class is java.lang.object. You do not have to declare that your class extends Object. The compiler does that for you. public class Employee { //... is equivalent to: public class Employee extends Object { //... The root class contains several non-final methods, but there are three that are important to consider overriding: tostring, equals, and hashcode Object tostring Method > The tostring method is called to return the string value of an object. For example, using the method println: Employee e = new Employee (101, "Jim Kern",...) System.out.println (e); String concatenation operations also invoke tostring: String s = "Employee: " + e; You can use tostring to provide instance information: public String tostring () { return "Employee id: " + empid + "\n" "Employee name:" + name; This is a better approach to getting details about your class than creating your own getdetails method.

Object equals Method > The Object equals method compares only object references. If there are two objects x and y in any class, x is equal to y if and only if x and y refer to the same object. Example: Employee x = new Employee (1,"Sue","111-11-1111",10.0); Employee y = x; x.equals (y); // true Employee z = new Employee (1,"Sue","111-11-1111",10.0); x.equals (z); // false! Because what we really want is to test the contents of the Employee object, we need to override the equals method: public boolean equals (Object o) {... Overriding equals in Employee > An example of overriding the equals method in the Employee class compares every field for equality: 1 public boolean equals (Object o) { 2 boolean result = false; 3 if ((o!= null) && (o instanceof Employee)) { 4 Employee e = (Employee)o; 5 if ((e.empid == this.empid) && 6 (e.name.equals(this.name)) && 7 (e.ssn.equals(this.ssn)) && 8 (e.salary == this.salary)) { 9 result = true; 10 11 12 return result; 13

Overriding Object hashcode > The general contract for Object states that if two objects are considered equal (using the equals method), then integer hashcode returned for the two objects should also be equal. 1 // Generated by NetBeans 2 public int hashcode() { 3 int hash = 7; 4 hash = 83 * hash + this.empid; 5 hash = 83 * hash + Objects.hashCode(this.name); 6 hash = 83 * hash + Objects.hashCode(this.ssn); 7 hash = 83 * hash + 8 (int)(double.doubletolongbits(this.salary) ^ 9 (Double.doubleToLongBits(this.salary) >>> 32)); 10 return hash; 11