-
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.
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)
// Constructors:
Student(){
System.out.println("Creating a default student.");
}
Student(String name, int age){
this.name = name;
this.age = age;
}
Classes and objects can be modeled using a UML (Unified Modeling Language) diagram. From those diagrams, we can see the properties and methods of an object as well as the relationship between different objects.
- "+" denotes public attributes or operations
- "-" denotes private attributes or operations
- "#" denotes protected attributes or operations
We can also show relationships between different classes using different shaped lines (arrows or diamonds can denote different relationships like inheritence, dependency etc.) Those can be seen later in "Polymorphism and Inheritence"