-
Notifications
You must be signed in to change notification settings - Fork 0
Classes
Object-oriented programming (OOP) involves programming using objects.
An object represents an entity in the real world that can be distinctly identified. A student, a desk, a circle, a button, a television or a Netflix account can be viewed as objects and can be represented as objects.
An object has a unique identity, state, and behavior. Using these, we can model the entities in real life.
- The state of an object is represented by data fields. These are the properties of said object. For example a Student object may have "name", "GPA", "age", "class" as its data fields.
- The behavior of an object is defined by methods. To invoke a method on an object is to ask the object to perform an action. For example a TV object may have methods like "turnOn", "turnOff", "changeChannel" etc.
Remember that objects are reference types, they are different than primitive types.
- For a variable of a primitive type, the value is the primitive type.
- For a variable of a reference type, the value is a reference to where an object is located.
Java provides a special method, toString(), which returns a string that can be used to print information about an object. We can override that method to print the information of the object that we want.
How do we model a student as an object?
A student class may have the following properties:
- Name -> String ("Harry")
- Age -> int (21)
- Department -> String ("Magical Studies")
- Class -> int (2)
- Graduated -> boolean (false)
- Grades -> int[] ({96, 94, 84, 76})
- GPA -> double (3.08)
And may have methods like the following:
- calculateGPA -> do something with grades
- addGrade -> add a new grade to grades list
- changeClass -> change their class, if it is "4" make graduated status true, etc.
Also, it needs to have a special class method, a constructor, that will create a new student.
The student class will look like this:
// Class name
public class Student {
// Class data fields, class instance variables
public String name;
public int age;
public String department;
public int class;
public boolean graduated;
public int[] grades;
public double gpa;
// constructor: constructor is a special class method
Student(){
System.out.println("Creating a new student.");
}
// Class method(s)
public void calculateGPA() { ... }
public void addGrade(int grade) { ... }
public void changeClass(int class) { ... }
}
Each class is defined in its own file, named as the class itself ("Student.java")
Main application can use the Student or other classes, if they are in the same directory.
We can create objects using a special class method, called constructor. These methods do not have a return type (not even void), and their name should be the same as the class name. They create the objects and initialize them. We can set the data fields of the object inside the constructor, and can change and reach them later using getter and setter methods.
You can have multiple constructors with the same name but different signatures (they can be overloaded). A no argument default constructor is provided by Java for each class (inherited from Object).
The keyword this usually is used in constructors. It refers to the object itself. You can use this to reference the object’s instance members. It can also be used to invoke another constructor of the same class.
// Constructors:
// Default constructor
Student(){
System.out.println("Creating a default student.");
}
Student(String name, int age){
this.name = name;
this.age = age;
}
// A constructor using this keyword
Student(String name){
this(name, 18);
}
If you want all the instances of a class to share data, use static variables, also known as class variables. Static variables store values for the variables in a common memory location. Because of this common location, if one object changes the value of a static variable, all objects of the same class are affected.
As an example, 'radius' of a circle should NOT be static. Because we may want to create circles with different radius. But we may have another variable like numberOfObjects
, which will hold how many circles we have created so far, and it makes sense for it to be a static variable.
static int numberOfObjects = 0;
static int getNumberObjects() {
return numberOfObjects;
}
// Constructors
/** Construct a circle object */
Circle() {
// Calling the other constructor
this(1, 2, 2);
//this.radius = 1;
numberOfObjects++;
}
/** Construct a circle object with its radius*/
Circle(double radius) {
this.radius = radius;
numberOfObjects++;
}
Java supports static methods as well as static variables. Static methods can be called without creating an instance of the class.
Constants in a class are shared by all objects of the class. Thus, constants should be declared as final static. For example, the constant PI in the Math class can be defined as final static.
final static double PI = 3.14159265358979323846;
Visibility modifiers can be used to specify the visibility of a class and its members, it specifies how data fields and methods in a class can be accessed from outside the class.
You can use the public visibility modifier for classes, methods, and data fields to denote that they can be accessed from any other classes
The private modifier makes methods and data fields accessible only from within its own class.
If you do not use a modifier for the class, attributes, methods or the constructor, they will be only accessible by classes in the same package.