More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

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

Inheritance, and Polymorphism.

CSE 143 Lecture 20. Circle

ITI Introduction to Computing II

ITI Introduction to Computing II

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

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

Java Object Oriented Design. CSC207 Fall 2014

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Inheritance Abstraction Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Introducing Java. Introducing Java. Abstract Classes, Interfaces and Enhancement of Polymorphism. Abstract Classes

Inheritance and Interfaces

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

Chapter 14 Abstract Classes and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Programming in the Large II: Objects and Classes (Part 1)

Topic 7: Algebraic Data Types

CS-202 Introduction to Object Oriented Programming

CMSC 132: Object-Oriented Programming II. Inheritance

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CS1150 Principles of Computer Science Objects and Classes

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

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

Object Oriented Programming 2015/16. Final Exam June 28, 2016

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

1.00 Lecture 13. Inheritance

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Object Oriented Relationships

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

ITI Introduction to Computing II

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

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

ITI Introduction to Computing II

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

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

Chapter 10 Classes Continued. Fundamentals of Java

Rules and syntax for inheritance. The boring stuff

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

STATIC, ABSTRACT, AND INTERFACE

Advanced Placement Computer Science. Inheritance and Polymorphism

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

Arrays Classes & Methods, Inheritance

MechEng SE3 Lecture 7 Domain Modelling

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

index.pdf January 21,

OBJECT ORİENTATİON ENCAPSULATİON

F I N A L E X A M I N A T I O N

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

Programming in the Large II: Objects and Classes (Part 2)

CSC 1214: Object-Oriented Programming

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

CS 113 PRACTICE FINAL

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

Use the scantron sheet to enter the answer to questions (pages 1-6)

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Inheritance and Polymorphism

Inheritance and Polymorphism

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Lab 9: Creating a Reusable Class

Chapter 13. Object Oriented Programming

Inheritance Motivation

JAVA MOCK TEST JAVA MOCK TEST II

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Comments are almost like C++

Inheritance and Polymorphism

Computer Science 210: Data Structures

CS 177 Week 15 Recitation Slides. Review

Polymorphism CSCI 201 Principles of Software Development

CMSC 132: Object-Oriented Programming II

Inheritance, polymorphism, interfaces

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Give one example where you might wish to use a three dimensional array

ITI Introduction to Computing II

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

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

5.6.1 The Special Variable this

CS 251 Intermediate Programming Inheritance

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

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

COMP 250. inheritance (cont.) interfaces abstract classes

Inheritance. OOP: Inheritance 1

IS-A / HAS-A. I IS-A Teacher and I HAS-A Laptop

Structured Programming

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

OOP: Key Concepts 09/28/2001 1

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Making New instances of Classes

Programming II (CS300)

The Essence of OOP using Java, Nested Top-Level Classes. Preface

CIS 110: Introduction to computer programming

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Class, Variable, Constructor, Object, Method Questions

Transcription:

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

The Story So Far... Classes as collections of fields and methods. Methods can access fields, and each other. Used to model objects. Constructors are used to allocate new objects, each of which has its own copy of the fields. class Rectangle { private int height, width; public Rectangle(int h, int w) { height = h; width = w; public int area() { return height*width; public void sayarea() { System.out.println( The area is + area());

The Story So Far... Visibility: Constructors, fields and methods may either be public or private. In the implementation of a class, fields and methods of the same object are used by giving their name. E.g. height*width, area() From outside, only public items can be used. These are accessed using object. name E.g. r1.area(), (new Rectangle(3,4)).sayArea()

Shared Fields Shared items: static A static field is used in common by all objects of a class. A field that is not static is sometimes called an instance variable. A method that uses only static fields may be declared static.

Using Shared Items From within the implementation of a class, static fields and methods are used just like any other. class Circle { public static double pi = 3.1415926535897932; private double radius; public Circle(double r) { radius = r; public double area() { return pi*radius*radius; public static void saypi(){system.out.println( Pi= + pi); From outside, these are accessed using class. name class Client { public static void main(string[] args) { System.out.println( Pi is, Circle.pi); Circle.sayPi();

Subclasses Sometimes a subset of elements have additional guaranteed properties. These additional properties may allow additional operations, or even fields, to be defined. E.g. Even Integers can have a half operation. Graduate Students can have a thesistitle method. Button Areas can have an onclick method. For this situation we can construct a subclass that extends a base class.

Subclass Example class Shape { private double area = 0; public void sayarea() { System.out.println( Area = + area); class Triangle extends Shape { private double base, height; public Triangle(double b, double h) { base = b; height = h; area = b*h/2.0; class Rectangle extends Shape { private double width, height; public Rectangle(double w, double h) { width = w; height = h; area = w*h; public double diagonal() { return Math.sqrt(width*width + height*height);

Subclass / Superclass The base class might itself be a subclass of another class. When we are thinking about a class what it extends, we talk about the class and its superclass or superclasses. Values belonging to a subclass have all the fields and methods of the superclass (plus whatever they define). These fields and methods are said to be inherited. In Java, if you all classes have Object as their ultimate superclass.

Subclass Values An object that belongs to a subclass also belongs to the superclass(es). E.g. Triangle t = new Triangle(3.0, 4.0); Rectangle r = new Rectangle(4.0, 5.0); Shape s1 = t, s2 = r; The object referred to by t is both a Triangle and a Shape. This allows us to write programs that operate on elements of the base class and then we can use them on elements of any subclass. private void sayit(shape s) { s.sayarea(); public static void main(string[] args) { sayit(t); sayit(r);

Subclass Polymorphism Being able to write programs for a common base class (like Shape) that are then used on values in particular subclasses (like Triangle or Rectangle) is the central idea of object-oriented programming. This is known as subclass polymorphism.

Null Variables of type Object or [] (array) may be assigned the value null. Using this judiciously allows you to write polymorphic programs. E.g. use as not yet initialized or value not found Using this injudiciously will lead to bugs and drive you crazy. E.g. needing to test for null before calling a method.

Protected Visibility Sometimes we want fields, methods or constructors to be available to implement subclasses, but not to be used by anyone else. For this, use protected instead of public or private.

Super Constructors You can use the constructor of the base class to construct that part of the object of the superclass. To do this, you call the superclass constructor, using the name super, as the first thing you do in the new constructor. class Rectangle extends Shape { protected double width, height; public Rectangle(double w, double h) { width = w; height = h; area = w*h;... class Square extends Rectangle { public Square(double length) { super(length, length);...

Over-Riding Sometimes there is a better way to compute a quantity in a subclass. This may be because there is more information or because of the specialized values. A subclass can over-ride an implementation of a method in a base class in this situation. The over-riding (new) method should have the same meaning as the over-ridden (old) method.

Example of Over-Riding class Rectangle extends Shape { protected double width, height, area; public Rectangle(double w, double h) { width = w; height = h; area = w*h; public double diagonal() { return Math.sqrt(width*width + height*height); class Square extends Rectangle { protected static double root2 = 1.4142135623730950; public Square(double len) { super(len, len); public double diagonal() { return root2*width;

When You Don t Want Over-Riding Over-riding has a cost: It means the compiler can not readily know at compile time which method definition will be used. An inconsistent over-riding can cause bugs. So sometimes you want to make it impossible to over-ride an item. You can do this by saying that a field or method is final.

When You Still Need the Old Method Sometimes you do want to over-ride a method, but you also want to make use of the old one. In this case, you can access it in the sub-class by calling it from super. Example: class ObjectWithBorder {... public void redraw() { drawborder(); super.redraw();...

Example of final class Rectangle extends Shape { protected double width, height, area; public Rectangle(double w, double h) { width = w; height = h; area = w*h; public double diagonal() { return Math.sqrt(width*width + height*height); class Square extends Rectangle { protected final static double root2 = 1.4142135623730950; public Square(double len) { super(len, len); public final double diagonal() { return root2*width;

Review Objects and classes. Fields, methods, constructors. Instance vs static fields and methods. Visibility: public, private, protected. Base class, subclass, superclass. Subclass polymorphism. Null. Over-riding. Super constructor and methods. Final.