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

Similar documents
BBM 102 Introduction to Programming II Spring 2017

Java Object Oriented Design. CSC207 Fall 2014

Example: Fibonacci Numbers

ITI Introduction to Computing II

The Final Exam Paper. Duration: 2 hours Reading: 15 minutes Total marks: 65 Hurdle: 32.5

Comments are almost like C++

ITI Introduction to Computing II

CS18000: Programming I

Object-Oriented Programming Concepts

2. The object-oriented paradigm!

OOP: Key Concepts 09/28/2001 1

INHERITANCE AND OBJECTS. Fundamentals of Computer Science I

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

Inheritance and objects

Chapter 6 Introduction to Defining Classes

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

CS1004: Intro to CS in Java, Spring 2005


Chapter 3 Objects and Classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

ECOM 2324 COMPUTER PROGRAMMING II

Object Oriented Programming in C#

Chapter 4. Defining Classes I

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

The return Statement

Principles of Object Oriented Programming. Lecture 4

Programming in the Large II: Objects and Classes (Part 1)

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

Instance Members and Static Members

Chapter 5 Object-Oriented Programming

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

Chapter 14 Abstract Classes and Interfaces

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Programming II (CS300)

CLASSES AND OBJECTS IN JAVA

Programming II (CS300)

UFCE3T-15-M Object-oriented Design and Programming

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

An Introduction to C++

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

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

Data Abstraction. Hwansoo Han

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

int b = 2; for (int a = 0; a < 10 b < 20; a++) { a = a + b; b = b + a; }

Object Oriented Modeling

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

Chapter 4 Defining Classes I

Object-Oriented Programming Concepts

PROGRAMMING LANGUAGE 2

Chapter 2: Java OOP I

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

By: Abhishek Khare (SVIM - INDORE M.P)

Advanced Object Oriented Programming. EECS2030 Section A

G Programming Languages - Fall 2012

- Aggregation - UML diagram - Self-Referential Classes - Generisity

Inheritance, and Polymorphism.

JAVA: A Primer. By: Amrita Rajagopal

Practice for Chapter 11

Package. A package is a set of related classes Syntax to put a class into a package: Two rules: Example:

Chapter 8 Objects and Classes

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

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

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

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

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

CS1150 Principles of Computer Science Objects and Classes

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Methods Common to all Classes

ENCAPSULATION AND POLYMORPHISM

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

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

Java Programming. U Hou Lok. Java Aug., Department of Computer Science and Information Engineering, National Taiwan University

core Advanced Object-Oriented Programming in Java

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

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

Advanced Object-Oriented. Oriented Programming in Java. Agenda. Overloading (Continued)

Inheritance and Polymorphism

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

QUESTIONS FOR AVERAGE BLOOMERS

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

C++ Important Questions with Answers

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

COURSE 2 DESIGN PATTERNS

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Class, Variable, Constructor, Object, Method Questions

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Object-Oriented Programming (Java)

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

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Chapter 5: Classes and Objects in Depth. Information Hiding

CS-202 Introduction to Object Oriented Programming

Making New instances of Classes

Inheritance and Polymorphism

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Transcription:

Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes. 1 2 Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area() Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Bare bone class no fields, no methods // my circle class 3 4 Class Circle with fields Class Circle with methods Add fields public double x, y; // centre coordinate public double r; // radius of the circle Add methods to Circle class public double x, y; // centre of the circle public double r; // radius of circle The fields (data) are also called the instance varaibles. 5 public double circumference() { return 2*3.14*r; public double area() { return 3.14 * r * r; Method Body 6

Class of Circles Class of Circle cont. Defining the Circle class, have created a new data type Data Abstraction acircle, bcircle simply refers to a Circle object, not an object itself. Can declare variables of that type: acircle bcircle Circle acircle; Circle bcircle; Points to nothing (Null Reference) Points to nothing (Null Reference) 7 8 Creating objects of a class Objects are created dynamically using the new keyword. acircle and bcircle refer to Circle objects Creating objects of a class acircle = new Circle(); bcircle = new Circle() ; bcircle = acircle; acircle = new Circle() ; bcircle = new Circle() ; 9 10 Creating objects of a class Automatic garbage collection acircle = new Circle(); bcircle = new Circle() ; bcircle = acircle; Before Assignment acircle P bcircle Q Before Assignment acircle bcircle P Q Q The object does not have a reference and cannot be used in future. The object becomes a candidate for automatic garbage collection. Java automatically collects garbage periodically and releases the memory used to be used in the future. 11 12

Accessing Object/ Circle Data Executing Methods in Object/ Circle Similar to C syntax for accessing data defined in a structure. acircle.x = 2.0 // initialize center and radius acircle.y = 2.0 acircle.r = 1.0 Using Object Methods: double area; acircle.r = 1.0; area = acircle.area(); sent message to acircle 13 14 Constructors Constructors Take another look at Circle object creation: Parentheses? Calling a function? Yes, creating a new object automatically begins with an initialization function called the constructor. Constructors have the same name as the class. Constructor is a method that gets invoked at object creation time. Constructors have the same name as the class. Constructors cannot return values. Constructors are normally used for initializing objects. A class can have more than one constructor with different input arguments. 15 16 Defining a Constructor Like any other method public double x,y,r; // Constructor public Circle(double centrex, double centrey, double radius) { x = centrex; y = centrey; r = radius; Public double circumference() { return 2*3.14*r; Public double area() { return 3.14 * r * r; Constructors initialise Objects Using the default constructor acircle.x = 2.0 // initialize center and radius acircle.y = 2.0 acircle.r = 1.0 acircle = new Circle() ; At creation time the center and radius are not defined. These values are explicitly set later. 17 18

Constructors initialise Objects Multiple Constructors With defined constructor Circle acircle = new Circle(1.414,-1.0,.25); Sometimes want to initialize in a number of different ways, depending on circumstance. acircle = new Circle(1.414, -1.0, 0.25) ; acircle is created with center (1.414, -1.0) and radius 0.25 This can be supported by having multiple constructors having different input arguments. 19 20 Multiple Constructors Initializing with constructors public double x,y,r; //instance variables // Constructors public Circle(double centrex, double cenrey, double radius) { x = centrex; y = centrey; r = radius; public Circle(double radius) { x=0; y=0; r = radius; public Circle() { x=0; y=0; r=1.0; public class TestCircles { public static void main(string args[]){ Circle circlea = new Circle( 10, 12, 20); Circle circleb = new Circle(10); Circle circlec = new Circle(); Public double circumference() { return 2*3.14*r; Public double area() { return 3.14 * r * r; circlea = new Circle(10, 12, 20) circleb = new Circle(10) circlec = new Circle() 21 Centre = (10,12) Radius = 20 Centre = (0,0) Radius= 10 Centre = (0,0) Radius = 1 22 Method Overloading Polymorphism Constructors all have the same name? In Java, methods are distinguished by: name number of arguments type of position of arguments Not method overriding (coming up), method overloading: Allows a single object, method or operator associated with different meaning depending on the type of data passed to it. Defining the same method with different argument types (method overloading) - polymorphism. 23 24

Use of this keyword Data Hiding and Encapsulation this keyword can be used to refer to the object itself. public double x,y,r; // Constructor public Circle (double x, double y, double r) { this.x = x; this.y = y; this.r = r; Java provides control over the visibility of variables and methods, encapsulation, safely sealing data within the capsule of the class Prevents programmers from relying on details of class implementation, so you can update without worry Protects against accidental or willful stupidity Keeps code elegant and clean (easier to maintain) 25 26 Public, Private, Protected Public keyword applied to a class, makes it available/visible everywhere. Applied to a method or variable, completely visible. Private fields or methods for a class only visible within that class. Private members are not visible within subclasses, and are not inherited. Protected members of a class are visible within the class, subclasses and also within all classes that are in the same package as that class. private double x,y,r; Visibility // Constructor public Circle (double x, double y, double r) { this.x = x; this.y = y; this.r = r; public double circumference() { return 2*3.14*r; public double area() { return 3.14 * r * r; 27 28 Visibility Accessors Getters/ Setters Circle message area Construction time message Circle Center (x,y) Radius r circumference message private double x,y,r; public double getx() { return x; public double gety() { return y; public double getr() { return r; public double setx(double x) { this.x = x; public double sery(double y) { this.y = y; public double setr(double r) { this.r = r; 29 30

Class Variables Java doesn t allow for global variables Instead have class variables that apply to the class as a whole, and not to just a single instance Define a class variable by marking with the static keyword Define using static: Class Variables // class variable, one for the Circle class, how many circles public static int numcircles; //instance variables,one for each instance of a Circle public double x,y,r; // Constructors... Access with the class name: ncircles = Circle.numCircles; 31 32 Class Variables - Example Using static variables: // class variable, one for the Circle class, how many circles private static int numcircles = 0; private double x,y,r; // Constructors... Circle (double x, double y, double r){ this.x = x; this.y = y; this.r = r; numcircles++; Class Variables - Example Using static variables: public class CountCircles { public static void main(string args[]){ Circle circlea = new Circle( 10, 12, 20); // numcircles = 1 Circle circlea = new Circle( 5, 3, 10); // numcircles = 2 circlea = new Circle(10, 12, 20) circleb = new Circle(5, 3, 10) numcircles 33 34 Class vs Instance Variables Instance variables : One copy per object. Every object has its own instance variable. E.g. x, y, r (centre and radius in the circle) Class (static) variables : One copy per class. E.g. numcircles (total number of circle objects created) 35