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

Similar documents
Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Design: Identifying Objects

Lecture 7: Classes and Objects CS2301

UNDERSTANDING CLASS DEFINITIONS CITS1001

Object-Oriented Programming Concepts

Objects First with Java A Practical Introduction using BlueJ

Inheritance and Polymorphism

CS 2530 INTERMEDIATE COMPUTING

CS111: PROGRAMMING LANGUAGE II

Chapter3: Introduction to Classes and Objects

Objects First with Java A Practical Introduction using BlueJ

by Pearson Education, Inc. All Rights Reserved. 2

A A B U n i v e r s i t y

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

Chapter 4 Defining Classes I

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Programming II (CS300)

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Lab: PiggyBank. Defining objects & classes

Programming II (CS300)

CS1004: Intro to CS in Java, Spring 2005

Programming II (CS300)

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

JAVA: A Primer. By: Amrita Rajagopal

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CLASSES AND OBJECTS IN JAVA

Ticket Machine Project(s)

Chapter 6 Introduction to Defining Classes

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

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Anatomy of a Class Encapsulation Anatomy of a Method

Introduction to Classes

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.

Lecture 6 Introduction to Objects and Classes

CS111: PROGRAMMING LANGUAGE II

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Dot and Scope Resolution Operator

Object-Oriented Programming

Express Yourself. Writing Your Own Classes

Introduction to Programming Using Java (98-388)

Understanding the Object Paradigm

How to engineer a class to separate its interface from its implementation and encourage reuse.

Chapter 4. Defining Classes I

ITI Introduction to Computing II

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Defining Classes and Methods

Today s Agenda. Quick Review

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal


Programming II (CS300)

And Even More and More C++ Fundamentals of Computer Science

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

Some miscellaneous concepts

Topic 10: Introduction to OO analysis and design

System.out.print(); Scanner.nextLine(); String.compareTo();

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

Chapter 5: Classes and Objects in Depth. Information Hiding

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

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

ITI Introduction to Computing II

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

Programming II (CS300)

CIS Intro to Programming in C#

CS111: PROGRAMMING LANGUAGE II

Software and Programming 1

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

CS313D: ADVANCED PROGRAMMING LANGUAGE

Objects as a programming concept

13. Java Classes. Educational Objectives. Classes - Technical. Definition: Classes

ECOM 2324 COMPUTER PROGRAMMING II

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Defining Classes and Methods

Classes and Objects. CGS 3416 Spring 2018

What is Inheritance?

Microsoft Visual Basic 2005: Reloaded

Lecture 02, Fall 2018 Friday September 7

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Software and Programming 1

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

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

10. Java Classes. Classes - Technical. Example: Earthquake catalog. Classes - Conceptual

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

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

7. C++ Class and Object

Lecture 18 Tao Wang 1

EECS168 Exam 3 Review

Lecture 7. Log into Linux New documents posted to course webpage

A A B U n i v e r s i t y

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

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

Transcription:

CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes

Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class

In Java 3 Java is an object-oriented programming language As the term implies, an object is a fundamental entity in a Java program Everything must be in a class (Even Main method).

What is a class? 4 Class: Defines a data type (blueprint) Combines data and operations in one place: data / fields/ properties / state variables Operations/ behavior / methods that modify state

What is a data type? Any data type includes Data (range of values) Operations (that can be performed on data) Example: int data type has: Data: +-32,767 Operations: +, -, *, /, % Same with classes Collection of data values together with set of basic operations to be allowed for the values 6-5

Class = Blueprint One blueprint to create several similar, but different, houses: Copyright 2012 Pearson Education, Inc.

7 Encapsulation The bundling of data and procedures(methods) into a single unit(called class). Class : various data elements and member functions are wrapped up together. main feature of object oriented programming

Objects & Classes objects represent things from the real world, or from some problem domain (example: the red car down there in the car park ) classes represent all objects of a kind (example: car ) Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling

Objects & Classes 9 variables of class type are objects Object: an instance / realization of a class same as relationship between a datatype and its variables

How to define a Class?? 10 Type definition Object declaration Member access

Basic class structure public class ClassName { Fields Constructors Methods } The contents of a class Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling

Defining Classes 12 Identifying Class Attributes Classes have attributes (data) and operations (behaviors). Class attributes are implemented in Java programs as fields, and class operations are implemented as methods.

the Unified Modeling Language (UML) 13 can be defined as a modeling language to capture the architectural, behavioral and structural aspects of a system. The UML has an important role in object oriented analysis and design. Objects are the key to this object oriented world The basic requirement of is to identify the object efficiently.

UML Class diagrams 14 Class diagrams describes the objects in a system and their relationships, so it is widely used by the developer community. The UML representation of a class is a rectangle containing three compartments stacked vertically. The top compartment shows the class's name. The middle compartment lists the class's attributes. The bottom compartment lists the class's operations.

15 Examples What about a student??

The student class - Exercise 16 Draw UML class diagram for class named Student where data is: name, UnivID, date of birth, department, level, GPA Methods are: PrintData()

A class in Java 17.java filename extension The class keyword Public keyword access modifier class is available to the public No main can t execute // Student.java public class Student { // data.. } //methods... //not static!!

Student Test 18 Another class. Why not Import student.java?? Using new object instantiation Dot separator // StudentTest.java public class Students { public static void main (String[] args) { //define object(s) //call a method }

19 Access modifiers Controlling Access to Members Access modifiers Public Methods Client s view of the services provided by the class Private data not accessible outside the class protected. Computer Science Department

Public and Private Members Data in class almost always designated private in definition! Upholds principles of OOP (Data Hiding) Hide data from user Allow manipulation only via operations Which are member methods 6-20

Mark Visibility type + Public # Protected - Private ~ Package 21 Marks for UML-supported visibility types

Example 22. Modify class efinition // Student.java public class Student { // data private //methods public }

Student Test 23 Try to access private members!! What about the public ones? // StudentTest.java public class StudentTest { public static void main (String[] args) { //define object(s) //access different members }

Set/get methods 24 Object needs to have access" to its data accessor methods Allow object to read data Also called "get methods" Simple retrieval of member data Mutator methods Allow object to change data Also called set methods" Manipulated based on application Computer Science Department

Example: set/get methods 25. Class Methods have direct access to class members (data or methods) // Student.java public class Student { // data //methods //set methods //get methods }

Student Test 26 The set method can be used to modify a data field the get method can be used to display the updated value // StudentTest.java public class StudentTest { public static void main (String[] args) { //define object(s) //modify data!! }

Constructors 27 A constructor can be used to initialize an object of a class when the object is created.

Example: constructors 28. Constructor must be public Same name as class no return type Parameters?? // Student.java public class Student { // data //an empty constructor public Student() { } //set/get methods } //other methods

Student Test 29 Keyword new calls the class s constructor to perform the initialization. // StudentTest.java public class StudentTest { public static void main (String[] args) { //define object(s)using empty //constructor } //print data

The default Constructor 30 By default, the compiler provides a default constructor with no parameters in any class that does not explicitly include a constructor. If you declare any constructors for a class, the Java compiler will not create a default constructor for that class With the default constructor, its instance variables are initialized to their default values.

Overloaded Constructors 31 Overloaded constructors enable objects of a class to be initialized in different ways. multiple constructor declarations with different signatures. the compiler differentiates signatures by : the number of parameters, the types of the parameters and the order of the parameter types in each signature.

Example: constructors 32. Define more Constructors Parameters?? // Student.java public class Student { // data //constructors public Student() { } //set/get methods } //other methods

Student Test 33 More objects Different initialization Same methods, different data // StudentTest.java public class StudentTest { public static void main (String[] args) { //define object(s)using different //constructors } //print data

Using this Reference 34 Every object can access a reference to itself with keyword this. Implicitly: refer to the object s instance variables and other methods inside member methods. Enables the class s code to know which object should be manipulated. Explicitly: Can also use keyword this in a non-static method s body.

Example: constructors 35. Use this reference to access class members // Student.java public class Student { // data //constructors public Student() { } //set/get methods } //other methods

36 Important hint Dr. Amal Khalifa, 2013

37 Be careful!! Dr. Amal Khalifa, 2013

38 That s all for today.. Text Book: Chapter 3