Chapter 10 Classes Continued. Fundamentals of Java

Similar documents
Chapter 11 Classes Continued

APCS Java Lesson 10: Classes Continued Modifications by Mr. Dave Clausen Updated for Java 1.5

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming. Java-Lecture 11 Polymorphism

CMSC 132: Object-Oriented Programming II

Inheritance and Polymorphism

Chapter 14 Abstract Classes and Interfaces

Inheritance. Transitivity

Chapter 6 Introduction to Defining Classes

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Inheritance and Interfaces

Programming II (CS300)

Programming II (CS300)

CS 251 Intermediate Programming Inheritance

Polymorphism and Interfaces. CGS 3416 Spring 2018

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

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

Chapter 5 Object-Oriented Programming

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

8. Polymorphism and Inheritance

INHERITANCE. Spring 2019

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

PROGRAMMING LANGUAGE 2

Inheritance, Polymorphism, and Interfaces

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Inheritance (Part 5) Odds and ends

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

More Relationships Between Classes

CMSC 132: Object-Oriented Programming II

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

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

ITI Introduction to Computing II

More on Objects in JAVA TM

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Inheritance and Polymorphism

Polymorphism and Inheritance

OVERRIDING. 7/11/2015 Budditha Hettige 82

ITI Introduction to Computing II

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

OBJECT ORİENTATİON ENCAPSULATİON

CS111: PROGRAMMING LANGUAGE II

Inheritance, and Polymorphism.

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

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

Self-review Questions

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

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

Object Oriented Issues in VDM++

What are the characteristics of Object Oriented programming language?

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Lecture 18 CSE11 Fall 2013 Inheritance

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Inheritance & Polymorphism

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

Data Abstraction. Hwansoo Han

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

INHERITANCE AND EXTENDING CLASSES

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

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

Inheritance and object compatibility

Computer Science 210: Data Structures

CS 11 java track: lecture 3

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

First IS-A Relationship: Inheritance

Inheritance and Polymorphism. CS180 Fall 2007

Object-oriented Programming. Object-oriented Programming

Inheritance (Deitel chapter 9)

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS-202 Introduction to Object Oriented Programming

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

BBM 102 Introduction to Programming II Spring Inheritance

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Chapter 7. Inheritance

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Arrays Classes & Methods, Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

Comp 249 Programming Methodology

Inheritance Motivation

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CST242 Object-Oriented Programming Page 1

Inheritance. OOP: Inheritance 1

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance -- Introduction

Inheritance and Polymorphism

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

Inheritance and Polymorphism

Type Hierarchy. Lecture 6: OOP, autumn 2003

Transcription:

Chapter 10 Classes Continued

Objectives Know when it is appropriate to include class (static) variables and methods in a class. Understand the role of Java interfaces in a software system and define an interface for a set of implementing classes. Understand the use of inheritance by extending a class. 2

Objectives (cont.) Understand the use of polymorphism and know how to override methods in a superclass. Place the common features (variables and methods) of a set of classes in an abstract class. 3

Objectives (cont.) Understand the implications of reference types for equality, copying, and mixed-mode operations. Know how to define and use methods that have preconditions, postconditions, and throw exceptions. 4

Vocabulary Abstract class Abstract method Aggregation Class (static) method Class (static) variable Concrete class Dependency 5

Vocabulary (cont.) Final method Inheritance Interface Overriding Postcondition Precondition 6

Class (static) Variables and Methods static variables and methods belong to a class. Not an instance of the class Class variable: Storage allocated at program startup Independent of number of instances created Class method: Activated when message sent to the class rather than to an object 7

Class (static) Variables and Methods (cont.) Class variables and methods are declared with the keyword static. Example: private static int studentcount = 0; Shared by all instances of the class with this declaration Use a static variable in any situation in which all instances share a common data value. 8

Class (static) Variables and Methods (cont.) Use static methods to provide public access to static variables. Class constants: Combine keyword final with keyword static Example: public static final int MIN_SCORE = 0; 9

Class (static) Variables and Methods (cont.) 10 Static variables, methods, and constants example

Class (static) Variables and Methods (cont.) 11 Static variables, methods, and constants example (cont.)

Class (static) Variables and Methods (cont.) Using class variables example: Using class constants example: 12

Class (static) Variables and Methods (cont.) 13 Two rules for using static variables: Class methods can reference only static variables. Never instance variables Instance methods can reference static and instance variables. The main method for an executable java class is static. JVM sends main message to start a program.

Turtle Graphics Open-source Java package for drawing Used in this text to illustrate features of objectoriented programming Pen used for drawing on a window StandardPen is a specific type of Pen. 14

Turtle Graphics (cont.) 15 Table 10-1: Pen messages

Turtle Graphics (cont.) 16 Example 10.1: Drawing a square using Turtle Graphics

Java Interfaces The Client Perspective 17 Interface: A list of a class s public methods Provides information to use a class without revealing its implementation When related classes have same interface, they can be used interchangeably in a program. In Turtle Graphics, Pen is an interface. StandardPen, WigglePen, and RainbowPen are examples of classes that conform to the Pen interface.

Java Interfaces The Client Perspective (cont.) A Java interface specifies the method signatures for an interface. 18

Java Interfaces The Client Perspective (cont.) An interface is not a class. But can be used as a data type Having multiple classes conform to the same interface allows for polymorphic behavior: 19

Java Interfaces The Client Perspective (cont.) A class that conforms to an interface is said to implement the interface. When declaring a variable or parameter, use the interface type when possible. Methods using interface types are more general. They are easier to maintain. 20

Java Interfaces The Implementation Perspective A class implements an interface using the implements keyword. 21

Java Interfaces The Implementation Perspective (cont.) A class that implements an interface must implement every method in the interface. A variable declared with the interface type can reference objects of any class that implements the interface. 22

Java Interfaces The Implementation Perspective (cont.) 23 The Circle class

Java Interfaces The Implementation Perspective (cont.) 24 The Circle class (cont.)

Java Interfaces The Implementation Perspective (cont.) Example 10.3: Try out some shapes 25

Java Interfaces The Implementation Perspective (cont.) 26 Example 10.3: Try out some shapes (cont.)

Java Interfaces The Implementation Perspective (cont.) Figure 10.3: Output from the TestShapes program 27

Java Interfaces The Implementation Perspective (cont.) Important interface concepts: Interface contains only methods, never variables. Interface methods are usually public. If more than one class implements an interface, its methods are polymorphic. 28

Java Interfaces The Implementation Perspective (cont.) Important interface concepts (cont.): A class can implement methods in addition to those listed in the interface. A class can implement more than one interface. Interfaces can be in an inheritance hierarchy. 29

Code Reuse Through Inheritance All classes are part of a large class hierarchy. Object class is the root. 30 Each class inherits variables and methods of the classes above it in the hierarchy. New classes can add new variables, add new methods, or alter existing inherited methods. Class immediately above a class is a superclass. Any classes that inherit are subclasses.

Code Reuse Through Inheritance (cont.) A class can only have one superclass, but may have many subclasses. The descendents of a class consist of its subclasses, their subclasses, etc. 31 Figure 10-4: Part of a class hierarchy

Code Reuse Through Inheritance (cont.) A class can inherit the characteristics of another class using the extends keyword. 32 The Wheel class extends the Circle class.

Code Reuse Through Inheritance (cont.) 33 The Wheel class extends the Circle class (cont.).

Code Reuse Through Inheritance (cont.) Example 10.4: Draw a wheel and a circle. 34

Code Reuse Through Inheritance (cont.) 35 Figure 10-5: A circle and a wheel with the same radius but different positions

Code Reuse Through Inheritance (cont.) Wheel implements Shape because it extends Circle, which implements Shape. xpos, ypos, and radius inherited by Wheel from Shape Must modify these instance variables in Shape to be protected rather than private Protected access modifier means variables are accessible only in current class and its descendents. 36

Code Reuse Through Inheritance (cont.) Methods may also be protected. A constructor may call superclass constructor using super();. To call one of superclass methods: Overriding: A subclass can modify a superclass method by re-implementing it. 37

Inheritance and Abstract Classes Abstract class: Provides functionality (methods), but can never be instantiated Can only be extended Declared with keyword abstract May contain standard methods and abstract methods 38

Inheritance and Abstract Classes (cont.) Abstract method: A method with no body Defined using keyword abstract A class with an abstract method will also be abstract. A subclass of an abstract class must implement every abstract method in that class. Final methods: Methods that cannot be overridden in a subclass 39

Inheritance and Abstract Classes (cont.) Partial abstract class/method example: 40

Interfaces, Inheritance, and Relationships among Classes A Java interface has a name and consists of a list of method headers. One or more classes can implement the same interface. If a variable is declared as an interface type, it can be associated with an object from any class that implements the interface. 41

Interfaces, Inheritance, and Relationships among Classes (cont.) 42 If a class implements an interface, then all of its subclasses do so implicitly. A subclass inherits all of the characteristics of its superclass. Subclass can add new variables and methods or modify inherited methods. Characteristics common to several classes can be collected in a common abstract superclass that is never instantiated.

Interfaces, Inheritance, and Relationships among Classes (cont.) An abstract class can contain headers for abstract methods that are implemented in the subclasses. A class s constructors and methods can use constructors and methods in the superclass. Inheritance reduces repetition and promotes the reuse of code. 43

Interfaces, Inheritance, and Relationships among Classes (cont.) Interfaces and inheritance promote the use of polymorphism. When a message is sent to an object, Java looks for a matching method. Search starts in the object s class and, if necessary, continues up the class hierarchy 44

Interfaces, Inheritance, and Relationships among Classes (cont.) Four ways in which methods in a subclass can be related to methods in a superclass: Implementation of an abstract method Extension Overriding Finality It is possible to work only with abstract classes, forgoing interfaces. 45

Interfaces, Inheritance, and Relationships among Classes (cont.) 46 Figure 10-7: Three types of relationships among classes

Acceptable Classes for Parameters and Return Values If an object of class BBB is expected, it is always acceptable to substitute an object of a subclass. But never of a superclass A subclass of BBB inherits all of BBB s methods. No guarantees about the methods in the superclass 47

Error Handling with Classes Before implementing error handling code, must determine error conditions for a class Preconditions: Describe what must be true before a particular method is called Values for parameters/instance variables Postconditions: Describe what must be true after a particular method has been executed Return values and altered instance variables 48

Error Handling with Classes (cont.) Example: 49

Exceptions JVM can throw exceptions when illegal operations are attempted. Commonly used exception classes: 50

Exceptions (cont.) Can throw exceptions in methods: To enforce pre-conditions or any other condition Syntax: Example: 51

Exceptions (cont.) Exceptions to enforce pre-conditions: 52

Exceptions (cont.) Clients who call such methods need to catch the exceptions so the program does not halt. Embed method call in a try-catch statement 53

Exceptions (cont.) A method may throw more than one type of exception. Client can handle each type explicitly. Example: 54

Reference Types, Equality, and Object Identity Aliasing: Two reference variables point to same object Comparing objects for equality: Comparing two reference variables using == indicates whether the variables point to the same object. To compare values of two distinct objects for equality, use the equals method. 55

Reference Types, Equality, and Object Identity (cont.) equals method: Defined in Object class Uses == operator by default A class must override equals to allow for comparison of object s contents. Example: 56

Reference Types, Equality, and Object Identity (cont.) Copying objects: Aliasing is not copying. Use a copy constructor: Implement Clonable: 57

Summary Class (static) variables provide storage for data that all instances of a class can access but do not have to own separately. Class (static) methods are written primarily for class variables. An interface specifies a set of methods that implementing classes must include. Gives clients enough information to use a class 58

Summary (cont.) Polymorphism and inheritance reduce the amount of code that must be written by servers and learned by clients. Classes that extend other classes inherit their data and methods. Methods in different classes that have the same name are polymorphic. 59

Summary (cont.) Abstract classes are not instantiated. Help organize related subclasses Contain their common data and methods Error handling can be distributed among methods and classes by using preconditions, postconditions, and exceptions. 60

Summary (cont.) Because of the possibility of aliasing, the programmer should provide: equals method for comparing for equality clone method for creating a copy of an object 61