CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Objects and Classes (contd.) Course Lecture Slides 19 May 2010 Ganesh Viswanathan
Objects and Classes Credits: Adapted from CIS3023 lecture slides (Spring 2010) by Dr Seema Bandyopadhyay, University of Florida, Gainesville. 2
Object Instantiation Declaring and Creating object in two steps // <ClassName> <objectrefvar>; Circle mycircle; mycircle = new Circle(); Declaring and Creating object in single step // <ClassName> <objectrefvar> = new <ClassName>(); Circle mycircle = new Circle(); 3
animation Trace Code Declare mycircle Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); yourcircle.radius = 100; mycircle no value 4
animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle no value yourcircle.radius = 100; : Circle radius: 5.0 Create a circle 5
animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; Assign object reference to mycircle radius: 5.0 : Circle 6
animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle no value Declare yourcircle 7
animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle no value : Circle Create a new Circle object radius: 1.0 8
animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle( ); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 Assign object reference to yourcircle yourcircle reference value : Circle radius: 1.0 9
Accessing Objects Referencing the object s data: // objectrefvar.data mycircle.radius Invoking the object s method: //objectrefvar.methodname(arguments) mycircle.getarea() 10
animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle( ); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 Assign object reference to yourcircle yourcircle reference value : Circle radius: 1.0 11
animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle reference value Change radius in yourcircle : Circle radius: 100.0 12
Value Type vs. Reference Type Value type int i = 1 i 1 Created using new Circle() Reference type Circle c c reference c: Circle radius = 1 13
Copying Variables Primitive type assignment i = j Before: After: i 1 i 2 j 2 j 2 Object type assignment c1 = c2 Before: After: c1 c1 c2 c2 c1: Circle radius = 5 C2: Circle radius = 9 c1: Circle radius = 5 C2: Circle radius = 9 14
Information Hiding In a well designed OO application, a class publicizes what it can do i.e. its method signatures but hides the internal details both of how it performs these services (method bodies) and the data (attributes) that it maintains in order to support these services 15
Access Modifiers class Circle { private double radius; public Circle() { this(1.0); public Circle(double newradius) { radius = newradius; Typically: Attributes are declared private Methods are declared public public double getarea() { return radius * radius * 3.14159; 16
Visibility Modifiers public The class, data, or method is visible to any class private The data or methods can be accessed only by the declaring class. 17
Accessing private attributes public class Driver { public static void main(string[] args) { Circle s1, s2; s1 = new Circle(14); Illegal: because attribute radius is s2 = new Circle(7); hidden or private! System.out.println( Radius =, s1.radius); outcome = s2.getarea(); //ok! 18
Accessing private attributes How can code in any other class access them? Programmer can provide methods to get and set them. 19
Get/Set Methods Get method or Accessor A method that returns the value of an attribute e.g., getufid( ) { return studentufid; Set method or Mutator A method that changes the value of an attribute e.g., getufid( ) { return studentufid; 20
Example Class Code public class Circle { private double radius; public double getradius() { return radius; public void setradius(double radius){ this.radius = radius; public Circle() { this(1.0); public Circle(double r) { setradius(r); public getarea() { return 3.14*radius*radius; 21
Example Driver Code public class Driver { Circle c1 = new Circle(14); Circle c2 = new Circle(7); System.out.println(c1.getRadius()); //ok! c1.setradius(5); //ok! boolean outcome = s2.getarea(); //ok! 22
public class Circle { private double radius; Some more code public double getradius() { return radius; public void setradius(double r){ if (r>0) radius = r; public Circle() { this(1.0); public Circle(double r) { setradius(r); public getarea() { return 3.14*radius*radius; 23
public class Student { private String name; private String ssn; private float gpa; Still more code public Student(int i, String n) { setssn(i); setname(n); setgpa(0.0f); // other constructors and methods, not shown here public String getname () { return name; public void setname(string newname) { name = newname; public String getssn () { return ssn; public void setssn(string s) { ssn = s; public float getgpa () { return gpa; public void setgpa(float newgpa) { gpa = newgpa; 24
public class Student { private String name; private String ssn; private float gpa; private int numdsfs; Lots more code public Student(int i, String n) { setssn(i); setname(n); setgpa(0.0f); setnumdsfs(0); // other methods, not shown here public boolean isonprobation() { if(numdsfs > 3) return true; else return false; public String getname () { return name; public void setname(string newname) { name = newname; public String getssn () { return ssn; public float getgpa () { return gpa; public void setgpa(float newgpa) { gpa = newgpa; public void setnumdsfs(int n) { numdsfs = n; 25
Benefits of Information Hiding Allows Data Validation Allows control over the level of access given for an attribute Simplifies Code Maintenance 26
Class attributes Each instance of a class (called an object) has a copy of the attributes Changing an attribute in one object doesn t affect the attribute of another object 27
Class Attributes, cont. Sometimes you may want some data to be shared among all instances. Example: we want all Student objects to have a shared access to the total student enrollment count at the university. 28
Static Attributes public class Student { private String name; private String ssn; private float gpa; private static int totalnumstudents = 0; public Student(int i, String n) { setssn(i); setname(n); setgpa(0.0f); totalnumstudents++; // other constructors, accessors/mutators, and methods, not shown here public int gettotalnumstudents() { return totalnumstudents; 29
Static Attributes, cont. A static attribute is one whose value is shared by all instances of that class. It in essence belongs to the class as a whole. 30
Static Attributes, cont. // Client code: Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student(); System.out.println(s1.getTotalNumStudents()); System.out.println(s2.getTotalNumStudents()); System.out.println(s3.getTotalNumStudents()); All of these println statements will print the value 3. 31
Static Methods public class Student { private String name; private static int totalnumstudents = 0; // other attribute details omitted... public static int gettotalnumstudents() { return totalnumstudents; // Client code Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student(); System.out.println(Student.getTotalNumStudents()); System.out.println(s1.getTotalNumStudents()); 32
Static Methods may only access static attributes class Student { private String name; // NOT static private static int totalstudents; public static void print() { System.out.println(name + " is one of " + totalstudents + " students."); // ILLEGAL! 33
Get more info! Value Types and Reference Types: http://java.sun.com/docs/books/jls/second_edition/html/typesvalues.doc.html Creating and initializing objects: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_02.htm (Online) Search keywords: JAVA classes and objects JAVA pass by reference or value 34