Java Object Oriented Design. CSC207 Fall 2014

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

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

CS-202 Introduction to Object Oriented Programming

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 5 Object-Oriented Programming

ITI Introduction to Computing II

Chapter 14 Abstract Classes and Interfaces

CSC207 Winter Section L5101 Paul Gries

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

ITI Introduction to Computing II

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

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

Chapter 10 Classes Continued. Fundamentals of Java

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

Chapter 6 Introduction to Defining Classes

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

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

Inheritance -- Introduction

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

Programming in C# Inheritance and Polymorphism

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

OBJECT ORİENTATİON ENCAPSULATİON

Inheritance, Polymorphism, and Interfaces

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

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

Inheritance and Polymorphism

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

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

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

What is Inheritance?

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

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

C++ Important Questions with Answers

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

CSC207 Week 3. Larry Zhang

JAVA MOCK TEST JAVA MOCK TEST II

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

CS1150 Principles of Computer Science Objects and Classes

ECE 122. Engineering Problem Solving with Java

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

CMSC 132: Object-Oriented Programming II

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Lecture 18 CSE11 Fall 2013 Inheritance

Java Fundamentals (II)

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

Inheritance, and Polymorphism.

25. Generic Programming

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

INHERITANCE. Spring 2019

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

Data Abstraction. Hwansoo Han

CPS 506 Comparative Programming Languages. Programming Language

G Programming Languages - Fall 2012

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Polymorphism and Interfaces. CGS 3416 Spring 2018

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Programming II (CS300)

Inheritance and Encapsulation. Amit Gupta

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

index.pdf January 21,

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

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

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

Programming overview

CS 251 Intermediate Programming Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

ITI Introduction to Computing II

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

ITI Introduction to Computing II

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

INHERITANCE AND EXTENDING CLASSES

Arrays Classes & Methods, Inheritance

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CSC 1214: Object-Oriented Programming

Making New instances of Classes

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Practice for Chapter 11

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

CS260 Intro to Java & Android 03.Java Language Basics

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

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

PROGRAMMING LANGUAGE 2

Introduction to Programming Using Java (98-388)

More on Inheritance. Interfaces & Abstract Classes

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

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

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

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

Inheritance and Polymorphism

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

CS Programming I: Inheritance

Computer Science 210: Data Structures

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Transcription:

Java Object Oriented Design CSC207 Fall 2014

Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code What language do you plan to use for implementation?

Design Problem What did you decide about: variables you need for each shape methods you need to implement Are you planning to use an Object Oriented language (e.g. Java)?

Principles of Object Orientation 1. Encapsulation 2. Inheritance 3. Polymorphism

Class vs Object Class is more like a blueprint An object is an instance of a class

Instance and Static Variables, Constants public class Circle { private int x,y; private int radius; private static int numcircles = 0; public static final double Pi = 3.1415926535; // Methods } radius is an instance variable. Each object/instance of the Circle class has its own radius variable. numcircles is a static variable. Also called a class variable. There is one numcircles variable shared by all objects of the class. Pi is a constant (an immutable variable). Since it is declared final, its value cannot be reassigned. We made it public, since it is static and final.

Encapsulation Convention: make all non-final instance variables private, which makes them accessible only within the class. When desired, give outside access using getter and setter methods. Why make instance variables private? We aim to hide implementation details and allow access to variables via a well-defined interface (the public methods for that class).

Access Modifiers Classes can be declared public or package-private. Members of classes can be declared public, private, protected, or package-private. Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier (aka packageprivate) Y Y N N private Y N N N

Constructors A constructor has: the same name as the class no return type (not even void) A class can have multiple constructors, as long as their signatures are different. Signature is the method name + its parameters If you do not define a constructor, the compiler supplies one with no parameters and no body. Once you define a constructor for a class, the compiler will no longer supply the default constructor.

this this is an instance variable that you get without declaring it. It is like self in Python. Its value is the address of the object whose method has been called.

Method return type Defining Methods A method must have a return type declared. Use void if nothing is returned. The form of a return statement: return expression If the expression is omitted or if the end of the method is reached without executing a return statement, nothing is returned.

For access modifiers: public - callable from anywhere protected - callable from same class, package, or subclasses no modifier - callable from same class or package private - callable only from this class Variables declared in a method are local to that method.

Parameters When passing an argument to a function, you pass what is in the variable s box: For class types, you are passing a reference. (Like Python) For primitive types, you are passing a value. This has important implications: aliasing! (we will cover this later)

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 exactly 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. For class Circle to inherit from class Shape we use the keyword extends public class Circle extends Shape {

Inheritance Inheritance allows one class to inherit the non-private data/methods of another class. In a subclass, super refers to the part of the object defined by the parent class. To call a parent s constructor: super(arguments); A subclass can add new data and methods. A subclass can also reuse a name already used in its superclass (i.e. inherited data member or method). More to come on the implications of this...

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

Polymorphism (Dynamic Binding) A variable can be assigned objects of its type or any subtype. But which method gets called depend not on the variable type, but on the type of the object it currently refers to. Overriding and dynamic binding allow a superclass to define some things that it and all of its descendants share, and yet the correct specialized method to be called.

Casting Down Object o = new String( hello ); We cannot do the following, because Object does not have a charat method: char c = o.charat(1); Instead, we need to the object that o refers to as a String: char c = ((String) o).charat(1);