Download presentation
Presentation is loading. Please wait.
Published byWilfred McDaniel Modified over 9 years ago
1
Introduction to Java Classes and Objects
2
What is a class A class is description of a structure that contains both data and methods – Describes a set of objects that are instances of the class All instances of a class: – have values for the data described in the class – can execute methods of the class
3
What is a class Class Student public class Student { } String firstName; String lastName; double gpa; … Every student has: firstName lastName gpa … Data
4
What is a class Class Student public class Student { } Every student can tell his/her name String firstName; String lastName; double gpa; … String getName() { … } Every student can get a new gpa void setGpa(double newGpa) { … } Every student can … Methods …
5
What is an object Objects are instances of classes – have values for the data described in the class – can execute methods of the class An object is created by executing its class’ constructor method – Every class has a default constructor
6
Object Creation public class Student { String firstName; String lastName; double gpa; String getName() { … } void setGpa(double newGpa) { … } } Student s1;s1 new Student(); A student object firstName lastName gpa s1 =
7
Object methods public class Student { String firstName; String lastName; double gpa; String getName() { … } void setGpa(double newGpa) { … } } Student s1;s1 new Student(); A student object firstName lastName gpa s1 = s1.setGpa(3.9) void setGpa(double newGpa) { gpa=newGpa; } 3.9
8
Not Allowed firstName is private public/private public properties of an object are directly accessible from outside the class public class Student { private String firstName; private String lastName; private double gpa; public String getName() { … } public void setGpa(double newGpa) { … } } Student s1=new Student(); s1.firstName=“John” System.out.println(s1.getName()); s1.setGpa(3.6);
9
constructors Constructor is a method that gets executed upon object creation Student s1; new Student(); A student object firstName lastName gpa s1 = Calling the default constructor of class Student
10
constructors Constructor is a method that – has the same name as the class – has no return type – Is public (most of the time) public class Student { private String firstName; private String lastName; private double gpa; public Student(String fn, String ln) { … } public void getName() { … } public void setGpa(double newGpa) { … } } Constructor
11
Example public class Student { private String fName; private String lName; private double gpa; public Student(String fn, String ln) { fName = fn; lName = ln; gpa=0.0; } public void setGpa(double newGpa) { gpa=newGpa; } public String toString() { return String.format("%-10s %-10s %4.2f\n", fName, lName, gpa); } public static void main(String[] args) { Student s1 = new Student("John", "Jones"); System.out.print(s1.toString()); s1.setGpa(3.9); Student s2 = new Student("Ted", "Gonzales"); s2.setGpa(4.0); System.out.print(s1.toString() + s2.toString()); } Output: John Jones 0.00 John Jones 3.90 Ted Gonzales 4.00
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.