Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.

Similar presentations


Presentation on theme: "CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010."— Presentation transcript:

1 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010

2 Classes and Objects Credits: Adapted from CIS-3023 lecture slides (Spring 2010) by Dr Seema Bandyopadhyay, University of Florida, Gainesville. 2

3 Object Instantiation Declaring and Creating object in two steps Declaring and Creating object in single step 3 // ; Circle myCircle; myCircle = new Circle(); // = new (); Circle myCircle = new Circle();

4 Trace Code 4 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; Declare myCircle no value myCircle animation

5 Trace Code, cont. 5 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; no value myCircle Create a circle animation

6 Trace Code, cont. 6 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; reference value myCircle Assign object reference to myCircle animation

7 Trace Code, cont. 7 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; reference value myCircle no value yourCircle Declare yourCircle animation

8 Trace Code, cont. 8 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; reference value myCircle no value yourCircle Create a new Circle object animation

9 Trace Code, cont. 9 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle( ); yourCircle.radius = 100; reference value myCircle reference value yourCircle Assign object reference to yourCircle animation

10 Accessing Objects Referencing the object’s data: Invoking the object’s method: 10 // objectRefVar.data myCircle.radius //objectRefVar.methodName(arguments) myCircle.getArea()

11 Trace Code, cont. 11 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle( ); yourCircle.radius = 100; reference value myCircle reference value yourCircle Assign object reference to yourCircle animation

12 Trace Code, cont. 12 Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); yourCircle.radius = 100; reference value myCircle reference value yourCircle Change radius in yourCircle animation

13 Value Type vs. Reference Type 13

14 Copying Variables 14

15 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

16 Access Modifiers 16 class Circle { private double radius; public Circle() { this(1.0); } public Circle(double newRadius) { radius = newRadius; } public double getArea() { return radius * radius * 3.14159; } Typically: Attributes are declared private Methods are declared public

17 Visibility Modifiers 17 F public –The class, data, or method is visible to any class F private –The data or methods can be accessed only by the declaring class.

18 Accessing private attributes public class Driver { public static void main(String[] args) { Circle s1, s2; s1 = new Circle(14); s2 = new Circle(7); System.out.println(“Radius = “, s1.radius); outcome = s2.getArea();//ok! } 18 Illegal: because attribute radius is hidden or private!

19 Accessing private attributes How can code in any other class access them? Programmer can provide methods to get and set them. 19

20 Get/Set Methods 20

21 Example Class Code 21 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; } }

22 Example Driver Code 22 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! }

23 23 public class Circle { private double radius; 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; } } Some more code

24 public class Student { private String name; private String ssn; private float gpa; 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 Still more code

25 public class Student { private String name; private String ssn; private float gpa; private int numDsFs; 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 Lots more code

26 Benefits of Information Hiding Allows Data Validation Allows control over the level of access given for an attribute Simplifies Code Maintenance 26

27 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

28 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

29 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

30 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

31 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

32 Static Methods 32 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());

33 Static Methods may only access static attributes 33 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! }

34 Get more info! 34


Download ppt "CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010."

Similar presentations


Ads by Google