Principles of Object Oriented Programming. Lecture 4

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

Chapter 5: Classes and Objects in Depth. Information Hiding

Object Oriented Programming (II)

1B1b Classes in Java Part I

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming in C#

Chapter 4 Defining Classes I

Handout 7. Defining Classes part 1. Instance variables and instance methods.

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Object Oriented Programming

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

Java and OOP. Part 2 Classes and objects

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

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

Object Oriented Programming

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

Recitation 3 Class and Objects

Classes. Classes as Code Libraries. Classes as Data Structures

Ticket Machine Project(s)

Notes on Chapter Three

Encapsulation. Mason Vail Boise State University Computer Science

Methods and Data (Savitch, Chapter 5)

G52CPP C++ Programming Lecture 9

CS-202 Introduction to Object Oriented Programming

CSC 1214: Object-Oriented Programming

Informatik II (D-ITET) Tutorial 6

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

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

CS1150 Principles of Computer Science Objects and Classes

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

CS304 Object Oriented Programming Final Term

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

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

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

The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

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

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

What will this print?

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

Object Oriented Programming

Classes, objects, methods and properties

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CMSC 132: Object-Oriented Programming II

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

Chapter 2: Java OOP I

CS 302 Week 9. Jim Williams

COMP322 - Introduction to C++

CLASSES AND OBJECTS IN JAVA

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Object-Oriented Programming in Java

CS1004: Intro to CS in Java, Spring 2005

Class and Functions. Reusable codes

JAVA MOCK TEST JAVA MOCK TEST II

JAVA: A Primer. By: Amrita Rajagopal

Basic Object-Oriented Concepts. 5-Oct-17

Chapter 4. Defining Classes I

Object-Orientation. Classes Lecture 5. Classes. State and Behaviour. Instance Variables. Object vs. Classes

EECS168 Exam 3 Review

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

CIS 110: Introduction to computer programming

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Chapter 15: Object Oriented Programming

Encapsulation in Java

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Chapter 5 Object-Oriented Programming

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

Object-Oriented Programming Concepts

Object oriented programming Concepts

Object Oriented C# Classes. The OO features of C# are very similar to Java. We ll point the exceptions along the way. 1. A simple C# class.

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

Programming Exercise 14: Inheritance and Polymorphism

Programming II (CS300)

Informatik II Tutorial 6. Subho Shankar Basu

Comments are almost like C++

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

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Distributed Systems Recitation 1. Tamim Jabban

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Classes and Methods: Classes

Programming II (CS300)

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Lecture 7 Objects and Classes

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

CS260 Intro to Java & Android 03.Java Language Basics

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

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

Classes Classes 2 / 36

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Transcription:

Principles of Object Oriented Programming Lecture 4

Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

Abstract Data Types Object-oriented languages provide a way to define abstract data types, and then create objects from them It s a template (or cookie cutter ) from which we can create new objects For example, a Car class might have attributes of speed, colour, and behaviours of accelerate, brake, etc

Encapsulation The data (state) of an object is private it cannot be accessed directly. The state can only be changed through its behaviour, otherwise known as its public interface or contract This is called encapsulation Private Data Public Interface "The Doughnut Diagram" Showing that an object has private state and public behaviour. State can only be changed by invoking some behaviour

Encapsulation Main benefit of encapsulation Internal state and processes can be changed independently of the public interface Limits the amount of large-scale changes required to a system

Modifiers Apr. 10. 2018

Modifiers Public/private are visibility modifiers Used to indicate visibility of methods and attributes

Modifiers Java has a range of other modifiers Control ownership of a method or attribute Control when and how variable can be initialised Control inheritance of methods (and whether they can be overridden by a sub-class)

Modifiers Public Protected Private Static Final

Modifiers Access Specifiers Class Package Child Class Entire Program Public O O O O Protected O O O X (omitted) O O X X Private O X X X

Example class A { private int a; int b; public int c; public class Test { public static void main(string args[]) { A obj = new A(); obj.a = 10; //? obj.b = 20; //? obj.c = 30; //?

Setters and Getters Setter / Mutator a method that sets up a value of a field format: setxxx() Getter / Accessor a method that returns a value of a field format: getxxx()

Setters and Getters These public methods are used to access private fields These methods are also used to check the attempt to change the values of private fields public void setage(int age) { if( age < 0 ) this.age = 0; else this.age = age;

LAB# 4-1 When we use an array, if we set up the index out of the size of the array, we will encounter a run-time error. Create a class for a safe array that checks if the given index is within the scope of its index public class SafeArray { private int a[]; public int length; public SafeArray(int size) { a = new int[size]; length = size; public int get(int index) {?? public void put(int index, int value) {??

Static static indicates a class variable or class method. It s not owned by an individual object This means we don t have to create an object to use it Arrays.sort and System.arrayCopy are static methods

Static -- Example public class MyClass { public static void utilitymethod() { public void othermethod() { //using the above: MyClass.utilityMethod(); MyClass objectofmyclass = new MyClass(); objectofmyclass.othermethod(); objectofmyclass.utilitymethod(); //this is illegal: MyClass.otherMethod();

Final final to make a variable that can have a single value Can be assigned to once and once only Useful to ensure a variable isn t changed once its assigned. final int count; count = 10; //the following will cause an error count = 20;

Defining Constants Unlike other languages, Java has no const keyword Must use a combination of modifiers to make a constant static to indicate its owned by the class final to make sure it can t be changed (and initialise it when its declared) Naming convention for constants is to use all capitals

Constants An Example public class MyClass { public static final int COUNT = 0; public static final boolean SWITCHED_ON = false; //example usage: if (MyClass.COUNT > 0) { if (MyClass.SWITCHED_ON) {