Objects and Classes Creating Objects and Object Reference Variables

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Tutorial 6 February 4th/5th, 2015
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
Chapter 9 Objects and Classes
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objects and Classes Mostafa Abdallah
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
CS 112 Programming 2 Lecture 02 Objects and Classes (1)
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
Inheritance.
Chapter 7 Objects and Classes
Topic: Classes and Objects
Lecture 3: Introduction to Object and Classes
G. Pullaiah College of Engineering and Technology
Introduction to Modular Programming
Chapter 4 Intermediate (part 1)
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Corresponds with Chapter 7
Chapter 8 Objects and Classes Part 1
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes
Java Basics II Minseok Kwon
Chapter 6 Objects and Classes
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes Part 01
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 9 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

Objects and Classes Creating Objects and Object Reference Variables Differences between primitive data type and object type Constructors Modifiers (public, private and static) Instance and Class Variables and Methods Scope of Variables Use the this Keyword

Class Concept

UML: Unified Modeling Language

Class Circle Declaration { double x0 = 0.0; double y0 = 0.0; double radius = 1.0; double findArea() return radius*radius*3.14159; }

Declaring Object Reference Variables ClassName objectName; Example: Circle myCircle;

Creating Objects Example: objectName = new ClassName(); myCircle = new Circle(); The object reference is assigned to the object reference variable.

Declaring/Creating Objects in a Single Step ClassName objectName = new ClassName(); Example: Circle myCircle = new Circle();

Test Class Circle public class TestCircle { public static main(String args[]) double area1, area2; Circle myCircle1 = new Circle(); Circle myCircle2 = new Circle(); area1 = MyCircle1.findArea(); area2 = MyCircle2.findArea(); System.out.println (”S1=”+area1); System.out.println (”S2=”+area2); }

Differences between variables of primitive Data types and Object types

Object reference The object reference is a handle to the location where the object resides in memory. When a primitive value is passed into a method, a copy of the primitive is made. The copy is what is actually manipulated in the method. So, the value of the copy can be changed within the method, but the original value remains unchanged. When an object reference or array reference is passed into a method, a copy of the reference is actually manipulated by the method. So, the method can change the attributes of the object.

Accessing Objects Referencing the object’s data: objectName.data Example: r= myCircle.radius; myCircle.radius=10.0; Invoking the object’s method: objectName.method() Example: S = myCircle.findArea();

Constructors Circle() { x0 = 0.0; y0 = 0.0; radius = 1.0; } Circle(double x, double y, double r) x0 = x; y0 = y; radius = r; Constructors are a special kind of methods that are invoked to construct objects.

Constructors, cont. A constructor with no parameters is referred to as a default constructor. ·       Constructors must have the same name as the class itself. ·       Constructors do not have a return type—not even void. ·       Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

Constructors and method for Class Circle public class Circle { ... Circle() // Default constructor x0 = 0.0; y0 = 0.0; radius = 1.0; } Circle(double x, double y, double r) x0 = x; y0 = y; radius = r; findArea() return radius*radius*3.14159;

Default Constructor If a class does not define any constructor explicitly, a default constructor is defined implicitly. If a class defines constructor implicitly, a default constructor does not exist unless it is defined explicitly.

Using Constructors Objective: Demonstrate the role of constructors and use them to create objects. myCircle = new Circle(); myCircle = new Circle(2,.0,3.0,5.0);

Visibility Modifiers and Accessor Methods By default, the class, variable, or data can be accessed by any class in the same package (will be explained later ). public The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class.

Visibility modifiers and accessor methods public class Circle { private double x0 = 0.0; private double y0 = 0.0; private double radius = 1.0; public double getRadius() return radius; } public void setRadius(double newRadius) radius = newRadius; public void setRadius(int newRadius) …

Instance Variables and Methods Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.

Class Variables, Constants, and Methods Class variables are shared by all the instances of the class. Class methods are not tied to a specific object. Class constants are final variables shared by all the instances of the class.

Class Variables, Constants, and Methods, cont. To declare class variables, constants, and methods, use the static modifier. private static int numberOfObjects=0; public final static double PI = 3.1415926;

Class Variables, Constants, and Methods, example. public class Circle { private static int numberOfObjects=0; public final static double PI = 3.1415926; Circle(double x, double y, double r) { x0=x; y0=y; radius=r; numberOfObjects++; } public static getNumberOfObjects() { return numberOfObjects; …

Class Variables, Constants, and Methods, cont.

Math is class with private constructor Class Math contains only public static methods: Math.sin() Math.asin () Math.ceil() Math.round() Math.abs() Math.min() Math.pow() Math.exp() Math.sqrt() ...

Scope of Variables The scope of instance and class variables is the entire class. They can be declared anywhere inside a class. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.

The Keyword this Use this to refer to the current object. Use this to invoke other constructors of the object. public void setRadius(double radius) { this.radius = radius; } public Circle() this(0.0, 0.0, 1.0);