Object Oriented Programming: Based on slides from Skrien Chapter 2

Similar documents
PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

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

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

Practice for Chapter 11

Chapter 5 Object-Oriented Programming

Inheritance and Polymorphism

Object Orientated Analysis and Design. Benjamin Kenwright

Java Object Oriented Design. CSC207 Fall 2014

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance and object compatibility

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

CS-202 Introduction to Object Oriented Programming

[ L5P1] Object-Oriented Programming: Advanced Concepts

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS Programming I: Inheritance

Type Hierarchy. Lecture 6: OOP, autumn 2003

C++ Inheritance and Encapsulation

Inheritance -- Introduction

CS 251 Intermediate Programming Inheritance

Collections Algorithms

Object-Oriented Concepts and Design Principles

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

Inheritance, Polymorphism, and Interfaces

Polymorphism and Inheritance

What is Inheritance?

Introduction to Object-Oriented Programming

15CS45 : OBJECT ORIENTED CONCEPTS

25. Generic Programming

PROGRAMMING LANGUAGE 2

COMP 401 Spring 2013 Midterm 2

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Object-Oriented Programming More Inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Implements vs. Extends When Defining a Class

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

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

Week 5-1: ADT Design

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

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

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Rules and syntax for inheritance. The boring stuff

Self-review Questions

ECE 122. Engineering Problem Solving with Java

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

Classes and Inheritance Extending Classes, Chapter 5.2

COMP 250 Fall inheritance Nov. 17, 2017

8. Polymorphism and Inheritance

ITI Introduction to Computing II

Conformance. Object-Oriented Programming Spring 2015

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

COP 3330 Final Exam Review

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

JAVA MOCK TEST JAVA MOCK TEST II

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

INHERITANCE AND EXTENDING CLASSES

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

CS558 Programming Languages

Inheritance and Polymorphism

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Data Abstraction. Hwansoo Han

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

ITI Introduction to Computing II

Distributed Systems Recitation 1. Tamim Jabban

Introduction to Inheritance

Object-Oriented ProgrammingInheritance & Polymorphism

Compaq Interview Questions And Answers

CS558 Programming Languages Winter 2013 Lecture 8

CS313D: ADVANCED PROGRAMMING LANGUAGE

Subtypes and Subclasses

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Exercise: Singleton 1

More on Inheritance. Interfaces & Abstract Classes

Defining Classes and Methods

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

COMP 401 Spring 2013 Midterm 2

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Object typing and subtypes

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Distributed Systems Recitation 1. Tamim Jabban

Programming II (CS300)

Inheritance. A key OOP concept

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

Lecture 18 CSE11 Fall 2013 Inheritance

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

SEEM4570 System Design and Implementation Lecture 09 Object Oriented Programming

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Transcription:

Object Oriented Programming: A Review Based on slides from Skrien Chapter 2

Object-Oriented Programming (OOP) Solution expressed as a set of communicating objects An object encapsulates the behavior and data related to a single concept Responsibility for performing a service is distributed across a set of objects

Key OOP Concepts Class: classification of objects with similar attributes and behavior Object: instance of a class Inheritance: hierarchies of classes in which classes at lower levels inherit features defined in classes at higher levels Message: request for an object to perform an operation on behalf of another object Dynamic binding: the ability to vary the object a message is sent to at run-time; the target of a message is determined at run-time, not at compiletime Polymorphism: the ability to substitute objects that share the same interface at run-time; dynamic binding enables polymorphism

Class A class describes objects that have similar features Two types of features Data: instance variables Behavior: methods

Sample Java class public class Person { private String name; private Date birthdate; public Person(String name, Date birthdate) { this.name = name; this.birthdate = birthdate; } public String getname() { return name; } } public Date getbirthdate() { return birthdate; }

The UML class diagram for the Person class.

Class variables and methods Class (static) variables and methods should be used sparingly in OOP Good uses of static variables To define constants To define properties of a class as opposed to properties of class instances (e.g., the total number of objects in the collection) OK uses of static methods To define methods that produce outputs using only values passed in as parameters (e.g., methods in Math class) See page 16 of Skrien

The UML notation for subclass and superclass.

Implementation Inheritance Also called subclassing Indicated in Java by keyword extends Uses: Specialize a superclass by adding new behavior in a subclass Specialize a superclass by overriding existing behavior in a subclass Generalize one or more subclasses by creating a superclass and moving common features up

Specialization by adding new behavior to the subclass.

Specialization by overriding Skrien pages 21,22: FilledOval class overrides the draw method of the Oval class to draw a filled-in black oval instead of a white oval with black border

Generalization by moving common behavior to a superclass.

A Person class with two superclasses. This is not legal in Java.

Class C inherits two foo() methods.

Types and interfaces A type is a set of data values and the operations that can be performed on them. Example: The Java integer type has 2 32 values and has operations such as addition, subtraction, multiplication, and division.

Java classes as types Each Java class is a type The values of such a type are the objects of that class or any subclasses The operations are the methods of the class

Java interfaces as types Each Java interface is a type The values of such a type are the objects of all classes that implement the interface The operations are the methods declared in the interface

Subtypes A type S is a subtype of type T if the set of objects of S form a subset of the set of objects of T and the set of operations of S are a superset of the set of operations of T Example: Any subclass defines a subtype of its superclass

A SimpleRunner class that implements the Runnable interface and extends the Object class.

Subtype polymorphism Polymorphism: the ability to assume different forms You can use an object of a subtype wherever an object of a supertype is expected. Examples: List l = new ArrayList(); List l = new LinkedList();

Design for change Suppose you used LinkedLists (LLs) in your program and now want to change to some LLs to ArrayLists (ALs) Skrien page 27 Change may require significant effort Need to identify all places where a LL is declared or initialized and decide whether it should be changed or not You may need to change calls to methods that are defined only for LLs Change process repeated if sometime in the future you decide to change ALs to some other type of collection

Design for change - 2 How can we design our program to accommodate change? 1. Find an appropriate interface or super class List interface 2. Check to see if the methods provided in the super class or interface can handle all list manipulation needs 3. Declare list variables as interface or abstract class types List list = new ArrayList();

A step further factory method How can we minimize changes in how the variables are initialized? Recall that we need to find all places where LLs are declared or initialized and decide whether to replace with ALs Localize code that needs to be changed Factory method Public class Manager { Public List createnewlist() { return new LinkedList();} } List list = manager.createnewlist();

The List interface and two classes that implement it.

Abstract classes Abstract classes are denoted by the key word abstract. Objects of such classes cannot be created. Some of the methods can be declared abstract, in which case subclasses must implement those methods (or be abstract themselves).

Abstract classes vs. interfaces Abstract classes can include method implementations and non-final fields Abstract classes with no Abstract classes with no implementations are like interfaces except a class can extend only one superclass in Java.

Dynamic method invocation In a method call v.foo(), the Java runtime environment looks at the actual class of the value of v, not the declared type of v, to determine which implementation of foo to execute.

Dynamic method invocation example Suppose a class A has a method foo that prints A and a subclass B has a method foo that prints B. A v = new B(); v.foo(); The implementation of foo in class B is the one that is executed ( B is printed).

The abstract Automobile class and its subclasses.

Dynamic method invocation for Automobiles Automobile[] fleet = new Automobile[]{ new Sedan(Color.black), new Minivan(Color.blue), new SportsCar(Color.red)}; int totalcapacity = 0; for(automobile car : fleet) { totalcapacity += car.getcapacity(); } Three different getcapacity methods are executed in this loop (one in each subclass)

Dynamic invocation- how does it work? car.getcapacity(); 1. Compiler determines that car is of type Automobile and checks that getcapacity() is declared in that class 2. At runtime, the implementation (or body) of getcapacity() is determined based on the actual type of object stored in car

Overloading vs. Overriding Overloading occurs when two methods in the same class have the same name but different parameter lists. Overriding concerns two methods, one in a subclass and one in a superclass, with identical signatures (name and parameter list).

The Automobile class has two equals methods, one inherited and one defined in Automobile.

The Automobile class does not inherit the Object class equals method. Instead, it overrides that method.

Overloaded method example Object o = new Object(); Automobile auto = new Automobile(); Object autoobject = new Automobile(); auto.equals(o); auto.equals(auto); auto.equals(autoobject); Which of the two equals methods in the Automobile class is executed in each of the last 3 lines above?