Introduction
Object oriented programming(OOP) is a programming methodology that is based on the concept of objects, which are components that possess an identity, a state and a behaviour.
They can mimic real-world entities. To illustrate this, I will be using the typical car example, sorry for the lack of creativity XD
Say we want to use a car in your program. To do this, we have to define a Car class. This class acts as a template for what a car looks like and what it can do. Using this template, we can create different objects of type 'Car' and describe their features and functionalities. In short, you can think of the class as a blueprint and the object as an embodiment of a class.
Terminologies
- Classes are user-defined templates used to create and define objects.
- Objects are instances(results) of a of class. All the instances of the same class share the attributes and the behavior of the class.
- Identity refers to the characteristic that uniquely identifies the object, typically implemented via a unique ID or memory addresss.
- State refers to the properties of an object.
- Behaviour refers to the behaviours(functionalities) of an object.
Creating a class
We create classes in Java by using the class
keyword.
class Car {
// Fields
String model;
String colour;
int milesDriven;
double gasInTank;
// Methods
void addGas(double amount) {
gasInTank = gasInTank + amount;
}
int getMilesDriven() {
return milesDriven;
}
}
- fields is another name for variables and methods is synonymous to functions.
- Class names are capitalised by convention.
Declaring an object
Declaring an object is also called instantiating a class. The object is known as the instance of a class. For example, we have a class Animal, and we want to declare an object Koala of type Animal. We say that Koala is an instance of the Animal class.
// Syntax
type name;
// Example
int testScore; // primitive type
Animal koala; //reference type
Simply declaring a reference variable does not create an object. Its value will be undetermined until we initialise it.
Initialising an object
Initialising an object involves assigning a value into an object. In Java, we initialise objects by using the new
keyword followed by a call to a constructor.
public class Cat {
// Constructor with one parameter, name
public Cat(String name) {
// Fields
String name;
// Methods
...
}
public static void main(String []args) {
// Creating an object named myCat of type Cat
Cat myCat = new Cat( "Bingus" );
}
}
We have used the new
keyword along with the constructor of the class to create an object. Here, Cat()
is the constructor of class Cat.
- Constructors in Java is a special method used for initialising objects.
- They have the same name as the class.
- It may or may not have paramters.
- If we do not define any constructors, Java will initialize uninitialized instance variables with default values:
Variable Type | Default Value |
---|---|
boolean | false |
int | 0 |
float double |
0.0 |
char | \u0000 |
Reference types | null |
Accessing instance variables & methods
We can access the variables & methods of an instance by using the .
notation as follows:
// Syntax
// Creating an object
className objectName = new className();
// Calling a variable
objectName.variableName;
// Calling a class method
objectName.methodName();
Exercise
Try creating a Bank Account class with these requirements:
- properties: name, account number, balance
- methods: withdraw, deposit, check balance.
Then, create a test account to deposit some money and check the balance.
Try it yourself before referring to the example solution.
public class Account {
// fields
private String name;
private int number;
private double balance;
// constructor
public Account (String accountName, int accountNumber, float accountBalance){
name = accountName;
number = accountNumber;
balance = accountBalance;
}
// method to withdraw money if balance is sufficient
public void withdraw(int amount) {
if (amount < balance) {
balance = balance - amount;
} else {
System.out.println("Insufficient balance.");
}
}
// method to deposit money into account
public void deposit(int amount) {
balance = balance + amount;
}
// method that displays the balance
public void checkBalance() {
System.out.printf("Balance is: %.2f", balance);
}
public static void main(String[] args) {
Account myAccount = new Account("Rachel", 1234, 10000000);
myAccount.deposit(10000000);
myAccount.checkBalance(); // Prints Balance is: 20000000.00
}
}
Top comments (0)