Objects and Classes II Chapter 6, continued. Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects.

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

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Objects and Classes Writing Your Own Classes A review.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Liang Chapter 6 Variable scope and the keyword this.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Unit 6: Object-oriented Programming: 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.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
UML Basics & Access Modifier
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
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.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
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.
A Revamping Of our skills so far. Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold.
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.
Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
More on Objects Mehdi Einali Advanced Programming in Java 1.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Non-Static Classes What is the Object of this lecture?
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Classes and Objects Introduced
Examples of Classes & Objects
Class Definitions and Writing Methods
Pass by Reference, const, readonly, struct
Classes & Objects: Examples
Objects and Classes Creating Objects and Object Reference Variables
class PrintOnetoTen { public static void main(String args[]) {
Java Basics II Minseok Kwon
Chapter 6 Objects and Classes
Chapter 6 Objects and Classes
Presentation transcript:

Objects and Classes II Chapter 6, continued

Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects can be used as parameters too: public static void printCircle(Circle c) { System.out.println("Circle area=" + c.findArea() + " "); } Circle c = new Circle(2.0); printCircle(c);

Case study: Example 6.5, p221 public class TestPassingObject { public static void main(String[] args) { Circle myCircle = new Circle(1.0); printAreas(myCircle, n); } public static void printAreas(Circle c, int times) { // repeatedly prints a circle and changes its radius } }

pass-by-value/pass-by-reference When a basic datatype (e.g. int) is passed as a parameter, a copy is made of it and the copy is used within the method: int i = 5; addOne(i); System.out.println("i is still " + i); This is different when we pass a reference to an object: a copy is made of the reference, but the object it refers to is still the same one!

pass-by-reference example Circle c = new Circle(2); doMonkeyBusiness(c); System.out.println("c's radius is " + c.radius); public static void doMonkeyBusiness(Circle circ) { circ.radius++; }

Previous example illustrated c: Circle radius=2 c main method doMonkeyBusiness circ radius=3

Reminder: basic datatypes i main method 2 addOne method num 2 3

A little privacy please..... doMonkeyBusiness altered the value of the radius directly. Usually, a class like Circle should not allow other code to alter its instance variables Instead, we make radius private, and provide public methods for accessing and changing the radius

public and private parts public class Circle { private double radius; // “private” means radius not accessible outside a Circle // object: myCircle.radius won’t work! public Circle() {.....} public Circle(double r) {.....} public double findArea() {......) public double getRadius() { return radius; } // since radius is private (myCircle.radius won’t work), // we have to use the getRadius method to get its value public void setRadius(double r) { radius = r; } // since radius is private (myCircle.radius=5 won’t work), // we have to use setRadius method to change its value }

getter and setter methods For each private instance variable x, we need to provide: –a getter method, which returns the value of x –a setter method, which sets the value of x Other code can access and alter x only by using these two methods We use methods, rather than allowing direct access to variables, because the methods let us check values are correct

Sample getter and setter private int width; // getter public int getWidth() { return width; } // setter public void setWidth(int newWidth) { width = newWidth; }

Use of getter and setter methods Circle c = new Circle(); c.setRadius(3.0); System.out.println("The radius of c is " + c.getRadius());

Summarizing Circle so far Circle private double radius; public Circle() public Circle(double r) public double getRadius() public void setRadius(double newRadius) public double findArea()

public and private modifiers Both methods and instance variables can be declared as either public or private If they are public, all parts of your code can use them if they are private, they can only be used within the declaring class, but not by other classes Instance variables are almost always private and are accessed (read or modified) through Getter and Setter methods

Static variables Each object of type Circle has its own radius....there is a separate copy of the variable radius for each object created. If instead we want all objects of a class to share data, we use a static variable Only one copy of a static variable is made, and all objects can access it static int numOfCircles; Each constructor can increment numOfCircles

Example: use of static variables public class Circle { private static int numOfCircles = 0; private double radius; Public Circle() { radius = 0.0; numOfCircles++; }...etc }

static constants Just like variables, we can declare a constant which is available for all members of a class. We do not need each object to have its own copy public final static double PI = ; Now other code can use this handy constant too: System.out.println("Pi is " + Circle.PI); Note that we use the classname to refer to a class constant

Class methods Some methods need to be associated with specific objects (e.g. findArea()) Others do not. These typically accept some value(s) as a parameter, and return some other value e.g. Math.sqrt(double d) public static double sqrt(double d) Class methods are associated with classes, and not with specific objects Class methods are therefore declared static: because we only want one of each class method. The main method is declared static because we only want one main method in our program.

Classes and Instances double d = Math.sqrt(3.0); // static Circle c = new Circle(3.0); d = c.findArea(); // not static d = c.getRadius(); // not static d = 2 * Circle.PI; // class constant d = MyInput.readDouble(); // static in general: Classname.Method(); // call to a static method Objectname.Method(); // call to a non-static method

When to use which? Constants are usually static Methods that do not read or modify instance variables are usually static Methods which use instance variables are not static Variables which describe common properties of an object can be made static Study Example 6.6 carefully (pp 225++)