Programming II (CS300)

Similar documents
Programming II (CS300)

Programming II (CS300)

Chapter 4 Defining Classes I

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

CLASSES AND OBJECTS IN JAVA

Programming II (CS300)

Chapter 4: Writing Classes

ENCAPSULATION AND POLYMORPHISM

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

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

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

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

Chapter 4. Defining Classes I

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

5. Defining Classes and Methods

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

CS1004: Intro to CS in Java, Spring 2005

Introduction to Programming Using Java (98-388)

Week 5-1: ADT Design

Getter and Setter Methods

CS 251 Intermediate Programming Methods and More

Programming II (CS300)

ECE 122. Engineering Problem Solving with Java

Distributed Systems Recitation 1. Tamim Jabban

Defining Classes and Methods

CSC1322 Object-Oriented Programming Concepts

Classes. Classes as Code Libraries. Classes as Data Structures

An Introduction to C++

Methods and Data (Savitch, Chapter 5)

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

CS 251 Intermediate Programming Methods and Classes

Object-Oriented Programming Concepts

Programming II (CS300)

EECS168 Exam 3 Review

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

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

Encapsulation. Mason Vail Boise State University Computer Science

Anatomy of a Class Encapsulation Anatomy of a Method

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

Notes on Chapter Three

CS 302 Week 9. Jim Williams

5. Defining Classes and Methods

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

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

CS1150 Principles of Computer Science Methods

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

Chapter 6 Introduction to Defining Classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

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

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

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Software and Programming 1

Methods. Methods. Mysteries Revealed

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Programming II (CS300)

ECOM 2324 COMPUTER PROGRAMMING II

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

by Pearson Education, Inc. All Rights Reserved. 2

1B1b Classes in Java Part I

Example: Fibonacci Numbers

Distributed Systems Recitation 1. Tamim Jabban

Chapter 5 Object-Oriented Programming

Classes and Objects. CGS 3416 Spring 2018

Objects as a programming concept

EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2

C08: Inheritance and Polymorphism

Defining Classes and Methods

CSC 1214: Object-Oriented Programming

Object Oriented Programming

COMP 401 Spring 2013 Midterm 1

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Principles of Object Oriented Programming. Lecture 4

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

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

EEE-425 Programming Languages (2013) 1

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

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

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

CS 2530 INTERMEDIATE COMPUTING

CS1150 Principles of Computer Science Methods

Updates. Office B116E Office Hours : Friday 1 p.m. 2 p.m. Also by appointment Office Hours Purpose:

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

COMP-202: Foundations of Programming. Lecture 9: Classes and Objects Sandeep Manjanna, Summer 2015

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

Basic Object-Oriented Concepts. 5-Oct-17

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

Object Oriented Programming in C#

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

Programming II (CS300)

Object-Oriented Programming

CS360 Lecture 5 Object-Oriented Concepts

CS Programming I: Classes

OBJECT ORIENTED PROGRAMMING USING C++

Chapter 3 Objects and Classes

Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

Transcription:

1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019

Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation Practice Example Introduction to Abstraction Keep in Mind

Constructors and Object Initialization 3 In Java, an object is an instance of any non-primitive type When a reference type is declared, no object is allocated. At that point, the reference is to null Objects must be explicitly constructed Objects are created with the operator new new calls a special type of subroutine called constructor When invocated, the constructor allocates memory for the object, initializes the object s instance variables, and returns a reference to the object

Constructors and Object Initialization 4 A constructor does not have any return type (not even void) The name of the constructor must be the same as the name of the class in which it is defined A constructor can t be declared static Every class has at least one constructor default constructor Does not take any argument allocate memory and initialize instance variables Overloaded constructor

Static versus non-static fields/methods 5 Static fields, variables, and constants have values that are shared across all instances (objects) of a particular class Static fields and methods belong to the Class rather than to an individual object No instance of the class is required in order to use a static field or a static method Non-static fields and non-static methods defined in a class can be used only when an object of that class has been created Heap (memory Allocation) Code Static Memory Stack (method call) console.log() Main() JVM

Creating Classes 6 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation Practice Example Introduction to Abstraction Keep in Mind

Encapsulation Concept 7 Encapsulation refers to information hiding The fact of packaging a number of features of an entity/object in a capsule to form a single unit In object-oriented programming, the single unit combining data and methods is called a Class

How to enforce Encapsulation Class 8 Class Private Fields (data) Public methods (behavior) private String field1; private int field2; To enforce encapsulation Hidden information Visible information The data is hidden : not accessed directly (private fields) The operations are visible: accessed directly (public methods) public void setfield1 (String s){ public String getfield1 (){ public void operationx( ){ Fields of the class are manipulated using public getter and setter methods (accessors and mutators)

Practice Example 9 Create a Student class with the following: A String variable named name to store the student s name An integer variable named uniqueid that contains the unique ID number for this student An integer class variable named nextuniqueid that contains the unique ID number for the next student A String variable named DoB to store the student s date of birth An integer class variable named numberofstudents that keeps track of the number of students that have been created so far A constructor Student(String name, String DoB) Several public get/set methods

Practice Example 10 public class Student { private String name; private int uniqueid; private String DoB; //Constructor public Student(String name, String DoB) { this.name = name; this.dob = DoB;

Practice Example 11 public class Student { private String name; private int uniqueid; private static int nextuniqueid = 1; private String DoB; private static int numberofstudents = 0; //Constructor public Student(String name, String DoB) { this.name = name; uniqueid = nextuniqueid; nextuniqueid++; this.dob = DoB; numberofstudents++; //Getters and setters methods for all properties public String getname() { return name; public void setname(string newname) { name = newname; public int getuniqueid() { return uniqueid; public int getnextuniqueid() { return nextuniqueid; public String getdob() { return DoB; public void setdob(string newdob) { DoB = newdob; public int getnumberofstudents() { return numberofstudents;

Encapsulation: Advantages 12 Encapsulation provides a layer of security around the manipulated data protecting it from external interference and mis-use A class can have total control over what is stored in its fields The users of a class ignore how the class stores the data The fields of a class can be made ready-only or write-only by providing getter and setter methods as the only access points to these fields from the outside of the class Encapsulation promotes the code s Maintainability Flexibility extensibility

Creating Classes 13 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation Practice Example Introduction to Abstraction Keep in Mind

Introduction to Abstraction 14 Abstraction Providing the essential features of an entity/object without including the implementation details hide unnecessary details of an object from the user In Object-Oriented Programming, a feature can be An attribute/field reflecting a property (data or state) An operation reflecting a method (or behavior or function)

Introduction to Abstraction 15 Abstraction Essential element of object-oriented programming Abstraction specifies necessary and sufficient descriptions rather than the implementation details Hiding internal implementation details of an object from the user In a real-world problem, humans manage complexity through abstraction Abstraction allows people to focus on what an object is able to do rather than on its technical implementation Car iphone

Abstraction versus Encapsulation 16

Abstraction versus Encapsulation 17 Abstraction Separates interface and implementation Provides access to a specific part of the data (Users know only how to use the visible part of the data according to the abstraction without allowing access to the implementation details) The ability to encapsulate and isolate design from execution information Encapsulation Groups related concepts into one unit Encapsulation hides data and users have only access to them through the accessors and mutators methods Encapsulation is a concept embedded in abstraction

Keep in Mind 18 A constructor defines how an object is declared and initialized. A constructor is a method that has the same name as the class and no return type If you write a constructor with return type void, you have actually written a method with the same name as the class, but this is NOT a constructor A class may define multiple constructors A constructor is invoked using the keyword new Private members cannot be accessed outside of the class.

Keep in Mind 19 Encapsulation is the process of combining data and functions into a single unit called class To enforce encapsulation, define the class fields as private and define public getter and setter methods to manipulate them Private members (fields or methods) cannot be accessed outside of the class Abstraction is a process where you show only relevant data and hide unnecessary details of an object from the user. The class consists of two parts: the specification (accessible) and the implementation (hidden)