Sage-Code Laboratory
index<--

Java Classes

In Object Oriented programming we model a problem using classes and objects. A class is a template or we can say it is a group of statements that can define properties and methods. It represents a composite data type. Next we will describe this concept in details. Let's get started.

Java is a pure object-oriented language. The class is the most important aspect of Java language. We define class to encapsulate functionality and data together in a well defined blueprint. This blueprint can be used later to create objects.

Where it comes from? Well, we often use the word classification. It is the action or process of classifying something according to shared qualities or characteristics. In the real world, you'll often find many individual objects all of the same kind. This is the core idea behind classes.

class

Class Anatomy

Example:

In this example we implement the class from previous diagram.

/* define a class */
public class Account{
    // class public properties
    public int act_number;
    public String act_type;
    // class internal field
    private long act_amount;
    //class constructor (same name ass class)
    public Account(int p_number, String p_type) {
        this.act_number = p_number;
        this.act_type = p_type;
    }
    //fists class methods
    public long widraw(long p_amount) {
        long result;
        if (act_amount > p_amount) {
           this.act_amount -= p_amount;
           result = p_amount;
        } else {
           result = p_amount = this.act_amount;
           this.act_amount = 0;
           return result;
        }        
    }
    //second method
    public void deposit(long p_amount) {
        this.act_amount += p_amount;
    }
    //third method
    public long closure() {
        result = this.act_amount;
        this.act_amount = 0;
        return result;
    }  
    //suplimentary method
    public void getAmount() {
        return this.act_amount;
    }
} //end of class

Constructor

A class has several sub-programs defined inside class body that are called methods. One of these methods is very important and is distinguishing from the others. Is the constructor method that has its name identical with the class name: Account. This method is the one who instantiate objects by using "new" keyword.

Object "this"

Object called "this" represents current object. This is available insite the constructor method. You can use "this" as a prefix to all members, including private members. Also, "this" can be used inside any other method that is not static.

Class Instances

Once you define a Class you can use this class like a data type to define Objects. The Objects are like variables but in Java we do not define variables we define "objects". To "instantiate" an object you must use "new" keyword. This allocate new memory for your new object and return an object handler.

Example:

Let's create a particular account using the class defined previously.

class AccountDemo {
    public static void main(String[] args) {
        Account myCheckAccount = new Account(2021,"checking");
        
        //verify the account number
        System.out.println("New account:" + Integer.toString(myCheckAccount.act_number));
        
        //deposit some money
        myCheckAccount.deposit(100);
        
        //widrow some money
        myCheckAccount.widrow(10);
        
        //verify the amount left
        System.out.println("New account:" + Integer.toString(myCheckAccount.getAmmount()));
    }
}

Object Properties

In Java we do not declare variables like in other languages. Instead we declare properties. A property do not hold a value until an object is instantiated. That means each property is related to an object. You can have multiple objects of the same class. That means one property can have multiple values, one value for each object.

Object Methods

A method is a group of statements that have a name, a list of parameters and a result type. There aremethod that have no result. In this case we must specify type "void". So user knows the method is not a function and can't be used in expressions. Methods are declared and implemented inside class and are inherited by each object.

One method can return a result or not. If a method create a result it is also called function. Unfortunately Java is not a functional programming language, so in Java we do not have a "function" keyword to differentiate a method from a function declaration. Also a function is not an object.

Access Modifiers

Java is using "access modifiers" to hide & protect or to expose class members for external use. Public members can be accessed using dot operator notation: object_name.member_name. Here are the modifiers you can use in diverse member declarations to modify access level:

Other Modifiers

Java provide "non access modifiers" for other purposes:

Static Members

Static members belong to class and not to class instances. This is a work around to the fact that Java is "pure" Object Oriented. You can't define anything outside of a class. So we use "static" modifier to make the class more usable without the need to instantiate an object from it.

In object-oriented programming, a "class variable" is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. So if we define a property of a Class and we use "static" keyword then the property become "class variable".

If we define a "static" method this method becomes "class method" and can be used outside of the class using dot operator. There is a catch though. In a static method we can't use any other member that is not "static". However, we can use a "static" member of a class using an object prefix.


Read next: Inheritance