CS1150 Principles of Computer Science Objects and Classes

Similar documents
CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

Inheritance and Polymorphism

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Chapter 11 Inheritance and Polymorphism

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

CS-202 Introduction to Object Oriented Programming

We are on the GUI fast track path

Chapter 11 Inheritance and Polymorphism

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

CS171:Introduction to Computer Science II. Li Xiong

Introduction Programming Using Python Lecture 8. Dr. Zhang COSC 1437 Fall 2017 Nov 30, 2017

Chapter 11 Inheritance and Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Inheritance and Polymorphism

Chapter 14 Abstract Classes and Interfaces

CH. 2 OBJECT-ORIENTED PROGRAMMING

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

ITI Introduction to Computing II

ITI Introduction to Computing II

Chapter 5 Object-Oriented Programming

Inheritance (continued) Inheritance

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

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

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

Inheritance (Deitel chapter 9)

Programming 2. Inheritance & Polymorphism

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

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

CS 251 Intermediate Programming Inheritance

Chapter 13 Abstract Classes and Interfaces

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Practice for Chapter 11

EKT472: Object Oriented Programming. Overloading and Overriding Method

Object Oriented Programming

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

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

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

Chapter 6: Inheritance

What is Inheritance?

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

Object Oriented Programming

Questions Answer Key Questions Answer Key Questions Answer Key

Inheritance and Polymorphism

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

AP CS Unit 6: Inheritance Notes

Inheritance, and Polymorphism.

Inheritance (Part 5) Odds and ends

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

Lecture 18 CSE11 Fall 2013 Inheritance

Reusing Classes. Hendrik Speleers

CS 113 PRACTICE FINAL

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

Inheritance and Encapsulation. Amit Gupta

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Inheritance. Transitivity

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

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Object Oriented Programming

COP 3330 Final Exam Review

Inheritance (IS A Relationship)

Lecture 2: Java & Javadoc

Motivations. Chapter 13 Abstract Classes and Interfaces

INHERITANCE. Spring 2019

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

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Making New instances of Classes

Inheritance -- Introduction

Comments are almost like C++

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Inheritance Motivation

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Abstract Classes Interfaces

COMP 249: Object Oriented Programming II. Tutorial 2: Intro to Inheritance

Java Fundamentals (II)

25. Generic Programming

CIS 110: Introduction to computer programming

PROGRAMMING LANGUAGE 2

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

Chapter 13 Abstract Classes and Interfaces

SSE3052: Embedded Systems Practice

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Computer Science 210: Data Structures

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

Chapter 13 Abstract Classes and Interfaces

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Transcription:

CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs

Object-Oriented Thinking Chapters 1-8 introduced fundamental programming techniques for problem solving using loops, methods, and arrays. Classes provide more flexibility and modularity for building reusable software, and data encapsulation. 2

Encapsulation This means to encase information and behavior within an object o Aka data/information hiding - structure and implementation are hidden to other objects that interact with it Accessing data directly is bad software engineering o o Student.noOfStudents++; No protection: any code could set an instance/static variable to something inappropriate UC. Colorado Springs

Encapsulation First, add private modifier to important variables Second, create getters and setters to retrieve and set private variables o o getter - method that returns the value of an instance variable setter - method that sets the value of an instance variable StudentApp7-8 UC. Colorado Springs

Inheritance Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The answer is to use inheritance. 5

Superclasses and Subclasses -color: String -filled: boolean GeometricObject -datecreated: java.util.date +GeometricObject() +GeometricObject(color: String, filled: boolean) +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +getdatecreated(): java.util.date +tostring(): String The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created. Creates a GeometricObject. Creates a GeometricObject with the specified color and filled values. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns the datecreated. Returns a string representation of this object. -radius: double +Circle() Circle +Circle(radius: double) +Circle(radius: double, color: String, filled: boolean) +getradius(): double +setradius(radius: double): void +getarea(): double +getperimeter(): double +getdiameter(): double +printcircle(): void -width: double -height: double +Rectangle() Rectangle +Rectangle(width: double, height: double) +Rectangle(width: double, height: double color: String, filled: boolean) +getwidth(): double +setwidth(width: double): void +getheight(): double +setheight(height: double): void +getarea(): double +getperimeter(): double 6

Inheritance When the definition of a class is based on an existing class (called the superclass) The class that is inheriting (subclass) can use accessible date fields and methods from superclass UC. Colorado Springs

Superclasses and Subclasses Example: StudentApp9.java StudentI -lastname/firstname: String -age: int -academiclevel: int +StudentI() +StudentI(last: String, first: String) +tostring(): String Properties and methods are inherited -campus: String CCStudentI CTechStudentI -campus: String UCCSStudentI -campus: String -UCCSID: int -lastuccsid: int +UCCSStudentI() +UCCSStudentI (last:string,first:string) +tostring(): String +getuccsid(): int +getcampus(): String 8

Are superclass s Constructor Inherited? No. They are not inherited. They are invoked explicitly or implicitly. Explicitly using the super keyword. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. But superclass constructor can be invoked explicitly or implicitly. Explicitly: Invoked from the subclasses' constructors, using super(). Implicitly (automatically): If super is not explicitly used, the superclass's no-arg constructor is automatically invoked. 9

Superclass s Constructor Is Always Invoked A constructor may invoke an overloaded constructor, or its superclass s constructor. If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example, public A() { is equivalent to public A() { super(); public A(double d) { // some statements is equivalent to public A(double d) { super(); // some statements When a constructor in a subclass (child) is called it actually invokes the constructors of all the super classes (parents) in its inheritance chain 10

CAUTION You must use super() to call the superclass constructor. Invoking a superclass constructor s name in a subclass (e.g., calling Student() in UCCSStudent) causes a syntax error. 11

Using the Keyword super The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: To call a superclass constructor: super() To call a superclass method: super.method() 12

Defining a Subclass A subclass inherits from a superclass. You can also: Add new properties Add new methods Override the methods of the superclass 13

Overriding Methods in the Superclass Student class: public String tostring (){ return String.format ("%-12s%-10s\t%d\t%d", this.lastname, this.firstname, this.age, this.academiclevel); UCCSStudent class: public String tostring (){ return String.format ("%-12s%-10s\t%d\t%d\t%d\t%s", this.lastname, this.firstname, this.age, this.academiclevel, this.uccssid, campus); 14

Overriding Methods in the Superclass A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding. class UCCSStudent extends Student{ // Other methods are omitted /** Override the tostring method defined in Student */ public String tostring() { return super.tostring() + String.format( \t%d\t%s, this.uccssid, campus); Note: The method defined in a subclass (child) matches a method defined in superclass (parent) The subclass (child) method must match signature and return type of method in superclass (parent) 15

NOTE An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. 16

NOTE Like an instance method, a static method can be inherited. However, a static method cannot be overridden (belong to the class). If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. 17

Overriding vs. Overloading Overloading o o o We discussed overloading in methods chapter Two or more methods with the same name but different formal parameters The methods could be in the same class or in different classes related by inheritance Overriding o o Occurs when dealing with inheritance A method defined in the subclass that matches the signature and return type of the method defined in superclass UC. Colorado Springs

Overriding vs. Overloading public class Test { public static void main(string[] args) { A a = new A(); a.p(10); a.p(10.0); class B { public void p(double i) { System.out.println(i * 2); class A extends B { // This method overrides the method in B public void p(double i) { System.out.println(i); A inherits from B and method "p" in A matches in signature and return type method "p" in B public class Test { public static void main(string[] args) { A a = new A(); a.p(10); a.p(10.0); class B { public void p(double i) { System.out.println(i * 2); class A extends B { // This method overloads the method in B public void p(int i) { System.out.println(i); Method "p" in A has different formal parameters than method "p" in B: Class A has 2 methods called "p" 19

Summary Build your own classes and objects Constructors Static variables and methods Encapsulation, inheritance UC. Colorado Springs