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)

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

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

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

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

CLASS DESIGN. Objectives MODULE 4

Inheritance -- Introduction

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

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

CS-202 Introduction to Object Oriented Programming

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

What is Inheritance?

PIC 20A Number, Autoboxing, and Unboxing

Programming in C# Inheritance and Polymorphism

What are the characteristics of Object Oriented programming language?

WA1278 Introduction to Java Using Eclipse

Lecture 18 CSE11 Fall 2013 Inheritance

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

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

Inheritance and Polymorphism

ITI Introduction to Computing II

CSC Java Programming, Fall Java Data Types and Control Constructs

CSE 431S Type Checking. Washington University Spring 2013

CSC207 Week 3. Larry Zhang

Inheritance, Polymorphism, and Interfaces

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

Chapter 5 Object-Oriented Programming

ITI Introduction to Computing II

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

1 Shyam sir JAVA Notes

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

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

JAVA MOCK TEST JAVA MOCK TEST II

Introduction to Inheritance

type conversion polymorphism (intro only) Class class

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

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

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

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

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS260 Intro to Java & Android 03.Java Language Basics

Programming II (CS300)

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

PROGRAMMING LANGUAGE 2

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

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

CPS 506 Comparative Programming Languages. Programming Language

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

CompuScholar, Inc. 9th - 12th grades

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

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

Polymorphism and Inheritance

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

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

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

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 Java. Jaanus Pöial, PhD Tallinn, Estonia

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

TeenCoder : Java Programming (ISBN )

CS313D: ADVANCED PROGRAMMING LANGUAGE

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

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

PIC 20A Inheritance, Interface, and Polymorphism

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

Declarations and Access Control SCJP tips

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

COP 3330 Final Exam Review

8. Polymorphism and Inheritance

Exercise: Singleton 1

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

Polymorphism. Zimmer CSCI 330

C++ Programming: Polymorphism

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Polymorphism. return a.doublevalue() + b.doublevalue();

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

Super-Classes and sub-classes

Implements vs. Extends When Defining a Class

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

More on Objects in JAVA TM

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

OBJECT ORİENTATİON ENCAPSULATİON

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Data Structures (list, dictionary, tuples, sets, strings)

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

Java Fundamentals (II)

Java: introduction to object-oriented features

Programming overview

Programming Exercise 14: Inheritance and Polymorphism

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

CO Java SE 8: Fundamentals

Polymorphism. Hsuan-Tien Lin. OOP Class, April 8, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/ / 26

Answer1. Features of Java

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 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); 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

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 15

Back to the demo Polymorphism 16

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 17