CSC207 Week 3. Larry Zhang

Similar documents
CSC207 Week 4. Larry Zhang

Java Object Oriented Design. CSC207 Fall 2014

CSC207 Week 2. Larry Zhang

Polymorphism and Inheritance

CS-202 Introduction to Object Oriented Programming

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

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

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

Inheritance and Interfaces

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

Inheritance, Polymorphism, and Interfaces

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

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

Inheritance, and Polymorphism.

Inheritance and Polymorphism

COMP 110/L Lecture 19. Kyle Dewey

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

PROGRAMMING LANGUAGE 2

What is Inheritance?

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Inheritance and Polymorphism

ITI Introduction to Computing II

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CSSE 220 Day 3. Check out UnitTesting and WordGames from SVN

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

Building custom components IAT351

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

ITI Introduction to Computing II

Chapter 14 Abstract Classes and Interfaces

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

Inheritance. Transitivity

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

CS 251 Intermediate Programming Inheritance

Polymorphism: Inheritance Interfaces

Design Principles: Part 2

Inheritance & Polymorphism

Design Principles: Part 2

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

ECE 122. Engineering Problem Solving with Java

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

Credit: The lecture slides are created based on previous lecture slides by Dan Zingaro.

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

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

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

core Advanced Object-Oriented Programming in Java

Binghamton University. CS-140 Fall Chapter 9. Inheritance

Chapter 10 Classes Continued. Fundamentals of Java

Advanced Object-Oriented. Oriented Programming in Java. Agenda. Overloading (Continued)

CS1004: Intro to CS in Java, Spring 2005

CSC207H: Software Design SOLID. CSC207 Winter 2018

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Introduction to Object-Oriented Programming

Programming in the large

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

CS 11 java track: lecture 3

C18a: Abstract Class and Method

Programming II (CS300)

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CST242 Object-Oriented Programming Page 1

Object-Oriented Concepts and Design Principles

CS11 Introduction to C++ Fall Lecture 7

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

C++ Important Questions with Answers

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

[ L5P1] Object-Oriented Programming: Advanced Concepts

Introduction to Computation and Problem Solving

More Relationships Between Classes

Chapter 15: Object Oriented Programming

Programming in C# Inheritance and Polymorphism

Inheritance and object compatibility

Chapter 5. Inheritance

Lecture 5: Inheritance

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

Object Oriented Programming

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

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

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

Lecture 18 CSE11 Fall 2013 Inheritance

Basics Inheritance and Instance Variables Inheritance and Methods Class Variables. Ruby Inheritance CSCI September 2017.

INHERITANCE AND EXTENDING CLASSES

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

Create a Java project named week10

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

1.00 Lecture 14. Exercise: Plants

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

CS 520 Theory and Practice of Software Engineering Fall 2018

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

Instance Members and Static Members

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

Lecture 2: Java & Javadoc

First IS-A Relationship: Inheritance

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

Transcription:

CSC207 Week 3 Larry Zhang 1

Announcements Readings will be posted before the lecture Lab 1 marks available in your repo 1 point for creating the correct project. 1 point for creating the correct classes. 1 point for creating the correct methods. If there is a notes.txt file saying that you need to use branches for Git, ignore it for now. Sololearn: an online learning-game for Java (as well as other languages) https://www.sololearn.com/play/java# 2

Today s Outline Unit Test: JUnit Javadoc More OO programming: Inheritance UML 3

Recap + A Couple More Tips 4

Recap: Reference to an Object is like Address Label Balloon b1 = new Balloon( red ); Balloon b2 = b1; b1 b2 5

When there is an Array of Balloons It s really an array of references, like Be careful with where the references are pointing to. 6

Call one constructor from another constructor 7

keyword static used on variables It is a variable which belongs to the class and not to object (instance) Static variables are initialized only once, at the start of the execution. A single copy to be shared by all instances of the class public class Person { public static int npeople = 0; // doesn t have to be public // Constructor public Person() { npeople++; // keeps track of # of Person s constructed System.out.println(nPeople + objects created ); } } // outside the class, static variables can be accessed like System.out.println(Person.nPeople); Avoid using static variables unless necessary. It is generally bad practice in OO design because it create dependencies between objects and violate encapsulation. Read more: https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil 8

NEXT TOPIC Unit Testing with JUnit 9

Why writing a unit testing suite? Misconception: It takes too much extra time to write the test suite! Wrong! Empirical research repeatedly show that test suites reduce debugging time more than the amount spent building the test suite. Unit Tests allows you to make big changes to code quickly: make the change, then click on test. If all green, then you know this module is all good. Unit Tests help you understand the design of the code you are working on. It forces you to think through the possible inputs and outputs of your code. Test cases should be written even before the implementation. Unit test is not really about finding bugs, it s about designing software components robustly. 10

Running unit test is very satisfying 11

It helps efficiently locate bugs in the software. 12

JUnit JUnit is a framework for writing tests in Java. JUnit was written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of Extreme Programming methodology) 13

JUnit structure 14

Execution flow Each method in a Unit Test can be labelled with @Before, @Test, @After Each Unit Test has many test cases, and for each test case: @Before: do some setup/prep work before running the tests, e.g., open a file, open a network connection. @Test: run the test case. @After: clean up after the tests finish, e.g., closing the file, close the connection. It s BTA, BTA, BTA,, not BTTTTTTTA 15

DEMO w02/src/balloontest.java 16

Other Tips for Unit Testing 17

Keep it SIMPLE 18

Keep it REAL 19

Read more on JUnit http://junit.org/ 20

NEXT TOPIC Javadoc a tool for automatically generating documentation from well-commented source code 21

DEMO Generate Javadoc in Eclipse: w02/doc/ How to write comments for Javadoc http://www.oracle.com/technetwork/articles/java/index-137868.html 22

NEXT TOPIC OO Continued: Inheritance 23

Basic Syntax for Inheritance Child Class IS-A (subtype of) Parent Class. E.g., Cat is a subtype of Animal. public class Cat extends Animal {... } 24

All Classes in Java form a Tree All classes form a tree called the inheritance hierarchy, with Class Object at the root. Class Object does not have a parent. All other Java classes have one parent. If a class has no parent declared (not extending any class), it is a child of class Object. A parent class can have multiple child classes. 25

Access Parent class from Child Child can access Parent s variable and method iff they are public or protected. CANNOT access those declared as private or default in the parent class. In a subclass, super refers to the parent class. super.variable super.method() super(arguments): parent s constructor 26

Constructor in Child class First thing to do: call parent s constructor by super(args). if don t call, parent s default constructor with no args will be called, i.e., super(). 27

Overriding and Shadowing Overriding: a Child class can re-implement a method that exists in Parent. child_object.method() will called the re-implemented one in Child. This is often-used technique. If you don t want a method to be overridden by any child, declare it as final. Shadowing: a Child class re-declares a variable that exists in Parent. The parent s variable gets shadowed by the child s variable This is bad and confusing, and should almost never be used. 28

Dynamic Binding class Person { public String hello() { return I am a person ; } } class Student extends Person { @Override public String hello() { return I am a student ; } } Student s1 = new Student( Alice, 22); Person p1 = s1; String x = p1.hello(); // Which hello() will be called? p1 is a reference that refers to Student s1 as a Person. Java remembers that s1 is created as a Student. When calling p1.hello(), Java knows that what p1 is referring to is a Student, and it will invoke the method hello() that is implemented in the Student class. So p1.hello() returns I am a student. Read more about dynamic binding here: https://stackoverflow.com/questions/190172 58/static-vs-dynamic-binding-in-java 29

DEMO w03/src/people/ Person.java StudentWithoutInheritance.java Student.java Doctor.java PlayWithPeople.java 30

Abstract Class An abstract class: contains abstract methods that are NOT implemented may also contain implemented methods may contain variables CANNOT be instantiated, because it is not concrete. A class can extend (inherit from) an abstract class and implement the abstract methods, then CAN be instantiated. A child class of an abstract class can still be abstract. 31

Example 32

What should be the parent/child relationships between these class? Shape Circle Rectangle Square 33

DEMO w03/src/shapes/ Shape.java Circle.java Rectangle.java Square.java PlayWithShapes.java 34

Liskov Substitution Principle (LSP) Functions that use references to parent classes must be able to use objects of child classes without knowing it. In other words, methods in the parent class must make total sense for the child class. If Square is a child of Rectangle, then Rectangle s setwidth() and setheight() methods do NOT make sense for Square. Violating LSP Square should directly inherit from Shape. Read more: https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle 35

NEXT TOPIC UML 36

UML Unified Modeling Language (UML) allows us to express the design of a program before writing any code. It is language-independent. An extremely expressive language. We ll use only a small part of the language, Class Diagrams, to represent basic OO design. 37

38

39

DEMO UML Visualization using ObjectAid http://www.objectaid.com/installation and many other UML tools are available for Eclipse. 40

41

References JUnit http://junit.org/ Javadoc http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html Inheritance http://docs.oracle.com/javase/tutorial/java/iandi/subclasses.html Arnold s notes on OO programming http://www.cs.toronto.edu/~arnold/cit/summer2007/ooprogramming.html 42