Chapter 2: Java OOP I

Similar documents
Object Oriented Programming

CS260 Intro to Java & Android 02.Java Technology

Object oriented programming Concepts

Chapter 5 Object-Oriented Programming

OVERRIDING. 7/11/2015 Budditha Hettige 82

Java Object Oriented Design. CSC207 Fall 2014

CS260 Intro to Java & Android 03.Java Language Basics

Lecture 6 Introduction to Objects and Classes

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

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

Lecture 23: Object Lifetime and Garbage Collection

Example: Fibonacci Numbers

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

G Programming Languages - Fall 2012

Inheritance and Polymorphism

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

Principles of Object Oriented Programming. Lecture 4

Exercise: Singleton 1

C10: Garbage Collection and Constructors

Object-Oriented Programming Concepts

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

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

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Index COPYRIGHTED MATERIAL

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

Lecture 2: Java & Javadoc

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)

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

Inheritance. Transitivity

CPS 506 Comparative Programming Languages. Programming Language

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

PROGRAMMING LANGUAGE 2

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

Lecture 5: Methods CS2301

COMP 110/L Lecture 24. Kyle Dewey

Introduction to Programming Using Java (98-388)

Recursion 1. Recursion is the process of defining something in terms of itself.

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

COMP 110/L Lecture 20. Kyle Dewey

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

Programming II (CS300)

CS-202 Introduction to Object Oriented Programming

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Class, Variable, Constructor, Object, Method Questions

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

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

Objects and Classes: Working with the State and Behavior of Objects

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

CSC 1214: Object-Oriented Programming

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

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

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

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

Chapter 5: Classes and Objects in Depth. Information Hiding

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Object Orientated Analysis and Design. Benjamin Kenwright

Classes, Objects, and OOP in Java. June 16, 2017

STRUCTURING OF PROGRAM

20 Most Important Java Programming Interview Questions. Powered by

ITI Introduction to Computing II

Programming overview

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

Object Oriented Programming. Solved MCQs - Part 2

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

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

Department of Networks College of Bardarash Technical Institute DUHOK Polytechnic University Subject: Programming Fundamental by JAVA Course Book

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226


1 Shyam sir JAVA Notes

CLASSES AND OBJECTS IN JAVA

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CMSC 132: Object-Oriented Programming II

Chapter 1 Introduction to Java

Java: introduction to object-oriented features

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Object-Oriented Programming

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Inheritance and Polymorphism. CS180 Fall 2007

CS263: Runtime Systems Lecture: High-level language virtual machines

Example: Count of Points

Compiler construction 2009

CO Java SE 8: Fundamentals

ITI Introduction to Computing II

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

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

Arrays Classes & Methods, Inheritance

Building custom components IAT351

Week 5-1: ADT Design

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

What is Inheritance?

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

Object Oriented Programming

Transcription:

Chapter 2: Java OOP I Yang Wang wyang AT njnet.edu.cn

Outline OO Concepts Class and Objects Package Field Method Construct and Initialization Access Control

OO Concepts

Object Oriented Methods Object An object is a software bundle of related state and behavior. (Behaviors) Software objects are often used to model the real-world objects Bicycle, Human Being, Fields (state)

Object Oriented Why Object Modularity (program divided in different objects ) Information Hiding (implementation is hided) Code Re-use?? Pluggability and debugging ease (refer to rule 1 and 2)

Object Oriented Class A class is a blueprint or prototype from which objects are created. many individual objects are all of the same kind encapsulation of behaviors and state describes a hierarchical concepts, human->primate->mammal->animal IN Java, the hierarchy of classes is a tree

Object Oriented OO Techniques: Abstraction Inheritance Polymorphism

Object Oriented

Class and Objects

A Simple Class

Class and Objects Package name/ Class name import Members Field - static / non-static Method - static / non-static Access Modifier(Class / Field / Method) abstract / final public / protected / private /default

Class and Object - Package Package is a set of Classes To avoid classes with same names To manage classes in your project vs. in internet Define a package lowercase project / subproject package javacourse; organization domain(reverse) + project + subproject package cn.edu.seu.cose.javacourse;

Class and Object - Package how to use a package referring the class in package with full qualified name import java.io.*; import java.io.file; JDK Package java.*; javax.* java.lang.* : default imported

Class and Object Field Define a Field Access Modifier Static Modifier(Optional) Type Name public String name; private int age; private Work work; first word lowercase

Class and Object Field int age 30 Type Work work 0x123 SEU Primary Type Lecture Reference

Class and Object Field Declare a reference of an object, but not create it Declare a reference of an object, and create the object

Class and Object Field Static Class Variables : static field Object Variables : non-static field LifeCycle: static field : as long as the program exist class.field non-static field : as long as object exist object.field local variables : in function public static int numofhuman; public int age;

Class and Object Method Define a Method Access Modifier Static Modifier (Optional) Return Type Name Parameter List (Type + Name) Method Body

Class and Object Method Static Method class Method

Class and Object Method Static Class Methods : static methods Object Methods : non-static methods LifeCycle: static field : as long as the program exist class.method() non-static field : after object exist object.method()

Class and Object Method public static void main(string args[]) why static : Lifecycle

Class and Object Overloading Method Overloading( 重载 ) Method Name Method Signature Method name Number of Parameters Types of Parameters Multiple methods with same name in a class: OK (Overloading) Multiple methods with same signature in a class: No! Signature does not include return type, because signature reflects the specification of behavior, not the result of behavior.

Class and Object Overloading Examples:

Class and Object Parameters Forget them: Formal Parameter? Actual Parameter? Pass by Value? Pass by Reference? In Java, the Copy of Parameter is passed. What is copied? For primary types, their value is copied For objects, the reference is copied. (What is a reference?)

Class and Object Parameter Try :

Class and Object Parameter Try Again:

Class and Object Parameter object object reference tom 0x12345 0x23456 Tom

Class and Object Parameter object object reference tom 0x12345 0x23456 Tom reference person 0x12345

Class and Object Parameter object object reference tom 0x12345 0x23456 Tom reference person 0x12345 Jerry

Class and Object Memory Method Area VM Stack Native Stack Heap Program Counter Runtime Memory Layout

Class and Object Parameter Methods with variable number of parameters

Class and Object Access Control Why Do We Need Access Control? Encapsulation Data Hiding Without Access Control: Debugging becomes difficult Data and programs become unsafe

Class and Object Access Control AC Modifier for Classes default (package) public AC Modifier for Members default(package) public private protected

Class and Object Access Control Modifier package world public Y Y default Y N

Class and Object Access Control Modifer Class Package SubClass World public Y Y Y Y protected Y Y Y N default Y Y N N private Y N N N

Class and Object Access Control

Class and Object Access Control Modifer Alpha Beta SubAlpha Gamma public Y Y Y Y protected Y Y Y N default Y Y N N private Y N N N

Class and Object Access Control Getter and Setter Methods

Construction and Initialization How to describe the construction of an object in a class? Constructor Default Constructor Constructor with parameters Initialization Block

Construction and Initialization Constructor Default With Parameters Constructor Nest this

Construction and Initialization Be careful of default Constructor default : no param JVM will give you one for free only when you don t write any constructor

Construction and Initialization

Construction and Initialization Initialization Block

Construction and Initialization Static Initialization Block

Construction and Initialization Destroying Object Java GC (Garbage Collection) Reference Counter finalize() political reason never use it

Class - Object All classes in Java inherits java.lang.object All objects in Java have following methods:

Last : How to Construct a Class

Forecast Abstraction Abstract Class Interface Inheritance Polymorphism