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

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

Java Object Oriented Design. CSC207 Fall 2014

CSC207 Winter Section L5101 Paul Gries

Introduction to Programming Using Java (98-388)

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

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

CS-202 Introduction to Object Oriented Programming

PIC 20A Number, Autoboxing, and Unboxing

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

1 Shyam sir JAVA Notes

WA1278 Introduction to Java Using Eclipse

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

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

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

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

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

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

COP 3330 Final Exam Review

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

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

What is Inheritance?

CS 231 Data Structures and Algorithms, Fall 2016

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Exercise: Singleton 1

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

Inheritance -- Introduction

CLASS DESIGN. Objectives MODULE 4

Programming in C# Inheritance and Polymorphism

Lecture 18 CSE11 Fall 2013 Inheritance

What are the characteristics of Object Oriented programming language?

CISC370: Inheritance

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Programming overview

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Java Fundamentals (II)

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

Lecture 5: Methods CS2301

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

More On inheritance. What you can do in subclass regarding methods:

CSC207 Week 3. Larry Zhang

CPS 506 Comparative Programming Languages. Programming Language

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

ITI Introduction to Computing II

type conversion polymorphism (intro only) Class class

IMACS: AP Computer Science A

Inheritance, Polymorphism, and Interfaces

CS260 Intro to Java & Android 03.Java Language Basics

Introduction to Inheritance

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

Chapter 9 Inheritance

Chapter 2: Java OOP I

Chapter 5 Object-Oriented Programming

ITI Introduction to Computing II

PROGRAMMING LANGUAGE 2

Classes and Inheritance Extending Classes, Chapter 5.2

Java: introduction to object-oriented features

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

CSE 431S Type Checking. Washington University Spring 2013

JAVA MOCK TEST JAVA MOCK TEST II

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

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

Object-Oriented Concepts

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

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

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

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

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

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

Inheritance and Polymorphism

Programming II (CS300)

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

Arrays Classes & Methods, Inheritance

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Chapter 6 Introduction to Defining Classes

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

A Foundation for Programming

What is inheritance. 8: Inheritance. Recall the objectives of OOP. Defining a Subclass. A cylinder is like a circle, but with an extra length

Declarations and Access Control SCJP tips

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Inheritance and Polymorphism

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

CS110D: PROGRAMMING LANGUAGE I

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

CS313D: ADVANCED PROGRAMMING LANGUAGE

Selected Java Topics

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Abstract Classes and Interfaces

Transcription:

Java + OOP CSC207 Winter 2018 1

Why OOP? Modularity: code can be written and maintained separately, and easily passed around the system Information-hiding: internal representation hidden from the outside world Code re-use: others can implement/test/debug complex code, which you can then use in your own code Pluggability and debugging ease: If a bolt breaks, you replace it, not the entire machine. All together: SOLID design principles (more later, and again and again and in A2) https://docs.oracle.com/javase/tutorial/java/concepts/object.html 2

Object-Oriented Programming What you have seen so far Java Classes Static Casting etc. This week Inheritance Overloading Overriding Shadowing Polymorphism Autoboxing 3

Inheritance Hierarchy All classes form a tree called the inheritance hierarchy, with 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, it is a child of class Object. A parent class can have multiple child classes. Class Object guarantees that every class inherits methods tostring, equals, and others. 4

Inheritance Inheritance allows one class to inherit the data and methods of another class. In a subclass, super refers to the part of the object defined by the parent class. Use super.«attribute» to refer to an attribute (data member or method) in the parent class. Use super(«arguments») to call a constructor defined in the parent class. 5

Multi-part objects Suppose class Child extends class Parent. (IS-A relationship) An instance of Child has: a Child part, with all the data members and methods of Child a Parent part, with all the data members and methods of Parent a Grandparent part, etc., all the way up to Object. An instance of Child can be used anywhere that a Parent is legal. But not the other way around. 6

To the demo Inheritance 7

Method Overloading Methods with the same name but different parameters e.g. four versions of Math.abs(): double abs(double d), float abs(float f), int abs(int i), long abs(long lng) Constructors are often overloaded 8

Back to the demo Method Overloading Constructor Overloading 9

Shadowing and Overriding Suppose class A and its subclass AChild each have an instance variable x and an instance method m. A s m is overridden by AChild s m. This is often a good idea. We often want to specialize behaviour in a subclass. A s x is shadowed by AChild s x. This is confusing and rarely a good idea. If a method must not be overridden in a descendant, declare it final. 10

Name Lookup A subclass can reuse a name already used for an inherited data member or method. Example: class Person could have a data member motto and so could class Student. Or they could both have a method with the signature sing(). When we construct x = new Student(); the object has a Student part and a Person part. If we say x.motto or x.sing(), we need to know which one we ll get! In other words, we need to know how Java will look up the name motto or sing inside a Student object. 11

Name Lookup Rules Calling a method: expression.method(arguments) Java looks for method in the most specific, or bottommost part of the object referred to by expression. If it s not defined there, Java looks upward until it s found (else it s an error). Referencing an instance variable: expression.variable Java determines the type of expression, and looks in that box. If it s not defined there, Java looks upward until it s found (else it s an error). 12

Back to the demo Shadowing Overriding 13

Casting for the Compiler If we could run this code, Java would find the charat method in o, since it refers to a String object: Object o = new String( hello ); char c = o.charat(1); // compilation error But the code won t compile because the compiler cannot be sure it will find the charat method in o. Remember: the compiler doesn t run the code. It can only look at the type of o. So we need to cast o as a String: char c = ((String) o).charat(1); 14

Interlude on the Memory Model 15

Sing() With the Java visualizer: https://goo.gl/xe3ty6 public class UniversityDemo { public static void main(string[] args) { Person p1 = new Student(); p1.sing(); class Person { public void sing(){ System.out.println("Caught in a bad romance!"); class Student extends Person { public void sing() { System.out.println("No more paper, no more books!"); 16

Static box: Heap: Static Space Name of class Math Object Contents: static variables and static methods double PI 3.14159... max(int, int) abs(double) Name of superclass Method frame: Name of method and currently executing line number Name of class (for static method) or address of object (for instance method) main : 1 MainClass Instance: Memory address Type of object (name of its class) Contents: parameters and local variables Stack 1010 int id 35567981 getid() setid(int) Student Contents: instance variables and methods Heap: Object Space = memory box 17

Heap: Static sing: 1 Student public class UniversityDemo { public static void main(string[] args) { Person p1 = new Student(); p1.sing(); main: 2 Stack UniDemo Person p1 0001 class Person { public void sing(){ System.out.println("Caught in a bad romance!"); class Student extends Person { public void sing() { System.out.println("No more paper, no more books!"); 0001 sing() Student Heap: Object 18

Memory Model: Extra Notes If no more memory on the stack java.lang.stackoverflowerror If no more memory on heap java.lang.outofmemoryerror Aside: JVM options: -Xms: initial Java Heap size -Xmx: maximum Java Heap size -Xmn: the size of the heap Garbage collection removes unreachable objects in the heap 19

Casting in Other Contexts Java automatically converts: byte short int long float double char int and above boolean no other types Moving in the other direction requires a cast, which is like a promise to the compiler that you know what you are doing. When you cast, information is sometimes lost. Example of casting (and info lost): double x = -57.99; int i = (int) x; // i = -57 20

Polymorphism Definition: the ability of one thing to have multiple forms (i.e. inheritance, overloading, etc.), or one form to apply to several things (i.e. interfaces) Example: if Student and Instructor both extend Person Person p; p = new Person( Lindsey ); // OK p = new Student( Jaisie ); // OK p = new Instructor( Paul ); // OK https://docs.oracle.com/javase/tutorial/java/iandi/polymorphism.html 21

Back to the demo Polymorphism 22

Autoboxing/Unboxing The automatic conversion Java makes between primitive types and their respective object wrappers It makes code clean! e.g. Integer i = 5; Autoboxing: primitive -> wrapper Unboxing: wrapper -> primitive Good for when we need object versions of things (e.g. in generics) Primitive Type Wrapper Class boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 23