Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Similar documents
Introduction to Object-Oriented Programming

Inheritance and Interfaces

Internal Classes and Exceptions

Object Orientated Analysis and Design. Benjamin Kenwright

Object Model. Object Orientated Analysis and Design. Benjamin Kenwright

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

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

Data Abstraction. Hwansoo Han

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

JAVA: A Primer. By: Amrita Rajagopal

The University Of Michigan. EECS402 Lecture 06. Andrew M. Morgan. Savitch Ch. 6 Intro To OOP Classes Objects ADTs.

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

STRUCTURING OF PROGRAM

Lecture 6 Introduction to Objects and Classes

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

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

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

CS 231 Data Structures and Algorithms, Fall 2016

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

CS-202 Introduction to Object Oriented Programming

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

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

Classes. Classes as Code Libraries. Classes as Data Structures

About this exam review

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

COP 3330 Final Exam Review

Programming Exercise 14: Inheritance and Polymorphism

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

1. Write two major differences between Object-oriented programming and procedural programming?

Object Oriented Programming with Java

9 Working with the Java Class Library

Object Orientated Programming in Java. Benjamin Kenwright

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

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

Java and OOP. Part 2 Classes and objects

CPS 506 Comparative Programming Languages. Programming Language

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Object Oriented Programming

Chapter 5: Classes and Objects in Depth. Information Hiding

Chapter 1 Getting Started

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

Chapter 4 Defining Classes I

Polymorphism Part 1 1

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

CS 251 Intermediate Programming Java Basics

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

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

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

EL2310 Scientific Programming

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Fall 2017 CISC124 9/16/2017

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Introduction to Programming Using Java (98-388)

What are the characteristics of Object Oriented programming language?

What is Inheritance?

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


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

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

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

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Lecture 7: Classes and Objects CS2301

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

Examples. Object Orientated Analysis and Design. Benjamin Kenwright

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

Object oriented programming Concepts

Preview from Notesale.co.uk Page 3 of 36

Chapter 11. Abstract Data Types and Encapsulation Concepts

Building custom components IAT351

CSE 142 Su 04 Computer Programming 1 - Java. Objects

ITI Introduction to Computing II

write the functions inside as well as outside the class. So in C++, they are called member functions and not methods.

VALLIAMMAI ENGINEERING COLLEGE

Complexity. Object Orientated Analysis and Design. Benjamin Kenwright

CS 251 Intermediate Programming Inheritance

2 rd class Department of Programming. OOP with Java Programming

Chapter 1: Object-Oriented Programming Using C++

Names, Scopes, and Bindings II. Hwansoo Han

Object Oriented Software Development CIS Today: Object Oriented Analysis

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

Inheritance. OOP: Inheritance 1

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

STUDENT LESSON A5 Designing and Using Classes

Principles of Object Oriented Programming. Lecture 4

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

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

Lecture Notes on Programming Languages

Basics of Object Oriented Programming. Visit for more.

Lecture 5: Methods CS2301

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

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

CS304 Object Oriented Programming Final Term

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

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

Transcription:

Java Basics Object Orientated Programming in Java Benjamin Kenwright

Outline Essential Java Concepts Syntax, Grammar, Formatting, Introduce Object-Orientated Concepts Encapsulation, Abstract Data, OO Languages, Today s Practical Review/Discussion

Last Week Compile Java Programs Javac.exe/Java.exe Setup IDE Basic Programs Hello World Simple Debugging e.g., Program entry point, hello worlds, print out (println..) Read Chapters 1 & 2

Question Java is case sensitive? A. True B. False

A. True Answer

Question What will be output of x in following code? "class Test{ public static void main(string[] args) { int x = 1; if (x == 1) { x = x + 1} } }" A. 0 B. 1 C. 2 D. 3 E. Compile Error

Answer E. Compile Error C:\pro\zjnu2017.github.io\OOP\exa mples Missing semi-colon (;)

Question What will be output of x in following code? "class Test{ public static void main(string[] args) { int x = 1; if (x == 1) { x = x + 1;} } }" A. 0 B. 1 C. 2 D. 3 E. Compile Error

C. 2 Answer

Today Exercises from Chapters 2, 4, 5 and 6 Data types (boolean, int, string,..) Loops (while, for, ) Conditional Logic (if, else, switch,..) Math libraries Arrays Methods (calling and passing parameters)

Pure Object-Oriented Language Everything is an object A program is a set of objects telling each other what to do by sending messages Each object has its own memory (made up by other objects) Every object has a type All objects of a specific type can receive the same messages Java breaks some of these rules in the name of efficiency

Object Concept An object is an encapsulation of data An object has identity (a unique reference), state, also called characteristics behavior An object is an instance of an abstract data type An abstract data type is implemented via a class

Abstract Data Type (ADT) An ADT is a collection of objects (or values) and a corresponding set of methods An ADT encapsulates the data representation and makes data access possible at a higher level of abstraction Example 1: A set of vehicles with operations for starting, stopping, driving, get km/litre, etc Example 2: A time-interval, start time, end time, duration, overlapping intervals, etc

Encapsulation and Information Hiding Data can be encapsulated such that it is invisible to the "outside world" Data can only be accessed via methods

Encapsulation and Information Hiding What the "outside world" cannot see it cannot depend on! Wall" between the object and the "outside world" The hidden data and methods can be changed without affecting the "outside world"

Class vs. Object Class A description of the common properties of a set of objects A concept A class is a part of a program Example 1: Person Example 2: Album Object A representation of the properties of a single instance A phenomenon An object is part of data and a program execution Example 1: Bill Clinton, Bono, Viggo Jensen Example 2: A Hard Day's Night, Joshua Tree

Type and Interface An object has type and an interface To get an object: Account a = new Account() To send a message: a.withdraw()

Instantiating Classes An instantiation is a mechanism where objects are created from a class Always involves storage allocation for the object A mechanism where objects are given an initial state Static Instantiating In the declaration part of a program A static instance is implicitly created Dynamic Instantiating In the method part of a program A dynamic instance is created explicitly with a special command

Interaction between Objects Interaction between objects happens by messages being send A message activates a method on the calling object An object O1 interacts with another object O2 by calling a method on O2 O1 sends O2 a message The call of a method corresponds to a procedure call in a non object-oriented language such as C or Pascal

Aggregation and Decomposition A decomposition splits a single concept into a number of (sub-)concepts An aggregation consists of a number of (sub-)concepts which collectively is considered a new concept

Aggregation and Decomposition, Example Idea: make new objects by combining existing objects Reusing the implementation

Generalization and Specialization Generalization creates a concept with a broader scope Specialization creates a concept with a narrower scope Reusing the interface

Generalization and Specialization, Example Inheritance: get the interface from the general class Objects related by inheritance are all of the same type

Code Example Polymorphism: One piece of code works with all shape objects Dynamic binding: How polymorphism is implemented

Structuring by Program or Data? What are the actions of the program vs. which data does the program act on Top-down: Stepwise program refinement Bottom-up: Focus on the stable data parts then add methods Object-oriented programming is bottomup. Programs are structure with outset in the data C and Pascal programs are typically implemented in a more top-down fashion

Review Java Program Structure

Java Class Example Car

Question Is Java a `top-down or `bottom-up programming language? A. `top-down B. `bottom-up

Answer B. `bottom-up Object-oriented programming is bottom-up. Programs are structure with outset in the data In OOP, you first write a base class, and constantly derive new child classes from the existing base one (like a Car class will probably derive from a class called Vehicle). So, you start from the basic blocks and go on making it a more complex design.

Byte Code vs. Executable

Difference from C/C++ Everything resides in a class variables and methods Garbage collection Error and exception handling No global variables or methods No local static variables No separation of declaration and implementation (no header files). No explicit pointer operations (uses references) No pre-processor (but something similar) Has fewer "dark corners" Has a much larger standard library

Question What displays from the following statements? String word = "abcde"; for (int i = 0; i <4; i+=2) System.out.print(word.charAt(i)); A. ab B. ac C. ace D. bd // access characters in a String using charat(i) similar to word[i] in C language

B. ac Answer

Review Concepts Classes are "recipes" for creating objects All objects are instances of classes An ADT is implemented in a class Aggregation and decomposition has-a relationship Generalization and specialization is-a or is-like-a relationship Encapsulation Key feature of object-oriented programming Separation of interface from implementation It is not possible to access the private parts of an object

This Week Read Chapters 3, 4, 5, 6 Review Slides Complete Java Chapter Exercises Practical Exercises Submit Exercises Review `Quizzes

Summary Overview Essential Java Language Principles Hands-On/Practical Today is about becoming comfortable/familiar with Java and the Programming Syntax/Concepts

Questions/Discussion Submit Exercise Questions 2.1 to 2.12 Single.zip file with your student number Remember to comment your code, name/student number at the top of files, separate file for each exercise ch2_1.java, ch2_2.java,