Download presentation
Presentation is loading. Please wait.
Published byOpal Jennings Modified over 9 years ago
1
Java - Classes JPatterson
2
What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you just didn’t know
3
What is a class? public class _Alpha { public static void main(String [] args) { } When a class file has a main you can execute the file
4
What is a class? public class _AlphaClass { } We are going to make class files without the main method.
5
What is a class? public class Student { } You still need to save the file as the class name with the.java extension – in this case it would be Student.java
6
Attributes / Fields public class Student { } What do we need to know about a student? These will be the ATTRIBUTES of the class.
7
Attributes / Fields public class Student { private String name; private int idNumber; private double gpa; } All attributes are declared to be private. Attributes are the global variables of the class.
8
Attributes / Fields public class Student { private String name; private int idNumber; private double gpa; } NOTICE – you only DECLARE the attributes you do NOT initialize them here…we will do that later.
9
Attributes / Fields public class Student { private String name; private int idNumber; private double gpa; } So far we have DECLARED the global or instance variables of the class
10
Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) { } To initialize the variables we have to write a method called a CONSTRUCTOR
11
Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) { } CONSTRUCTORS are public and have the same name as the class – ALWAYS, NO EXCEPTIONS
12
Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } This constructor is called the DEFAULT constructor. We want to initialize the attributes to generic starting values.
13
Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } Classes can have more than 1 constructor. Next we are going to make a SECONDARY constructor
14
Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } In this case the secondary constructor will allow your client to pass over values to the class and then store them in the attributes.
15
Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } FLEXIBLITY – having more than 1 constructor makes your class more flexible. There are now 2 ways to initialize your attributes.
16
Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } INITIALIZE ALL ATTRIBUTES IN EVERY CONSTRUCTOR
17
CLASS public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } So – let’s look at a client program that might use this class.
18
Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); }
19
Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); } s1 name“ idNumber0 Gpa0 s1 is a Student OBJECT that was declared with the default constructor so all the attributes are set to the default values.
20
Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0); } s1 name“ idNumber0 Gpa0 name“ Sookie “ idNumber10025 Gpa4.0 s2
21
Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0); } s1 name“ idNumber0 Gpa0 name“ Sookie “ idNumber10025 Gpa4.0 s2 “Sookie” is passed over to n
22
Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0); } s1 name“ idNumber0 Gpa0 name“ Sookie “ idNumber10025 Gpa4.0 s2 The attribute name is assigned n
23
Accessors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } Now, let’s look at accessors. Accessors are used to give information about the class. What is your name? What is your idNumber? What is your gpa? If I ask you, “what is your name?” what data type are you going to tell me?
24
Accessors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } Now, let’s look at accessors. Accessors are used to give information about the class. What is your name? What is your idNumber? What is your gpa? If I ask you, “what is your name?” what data type are you going to tell me? STRING – that is the return type of the accessor. The return type is the data type of the value returned or given by the method
25
Accessors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public String getName() { return name; } Now, let’s look at accessors. Accessors are used to give information about the class. What is your name? What is your idNumber? What is your gpa? If I ask you, “what is your name?” what data type are you going to tell me? STRING – that is the return type of the accessor. The return type is the data type of the value returned or given by the method
26
Mutators public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public void setName(String n) { name = n; } Now, let’s look at mutators. Mutators are used to change information about the class. What if on the first day of class I check my roll sheet and you are listed as Orvil but you go by your middle name? I would want to change your name to Sam. I could call a mutator method that would set your name to a new and improved value.
27
Mutators public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public void setName(String n) { name = n; } Now, let’s look at mutators. Mutators are used to change information about the class. What if on the first day of class I check my roll sheet and you are listed as Orvil but you go by your middle name? I would want to change your name to Sam. I could call a mutator method that would set your name to a new and improved value.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.