C1CTech

Java Class and Objects

In this article, you will be introduced with the basic OOPs concept i.e. Classes and Objects and how you can create classes and objects in your Java program.

Java Objects

Object refers to an entity that has state and behavior. 

Let’s take few examples:

1 . car is an object

2 . account is an object

Java Class

A Class is a way to bind the data describing an object and its associated methods together.

Classes are needed to represent real-world entities that not only have properties (their characteristics) but also have associated operations (their behavior).

How to define a class in Java?

Syntax

class ClassName {
    //variables
    //methods
}

Example

 class Account {
    private String type;
    private int accountno;
    private float balance;

    public void deposit(float amount) {
      balance = balance + amount; 
    }

    public void withdraw(float amount) {
     balance = balance - amount;
    }    
}

Example explained

Notice two keywords private and public in above program. These are access modifiers which will be discussed in detail in later chapters. For now, just remember:

Creating Objects

To access members defined within the class, you need to create objects.

Syntax

obj_new_img

 

Here, Constructor is a new term, we will discuss in detail in later chapters. For now, just remember:

Example

class Account {

    private char type;
    private int accountno;
    private float balance;

    public void deposit(float amount) {
        balance = balance + amount;
    }

    public void withdraw(float amount) {
        balance = balance - amount;
    }
}

public class ClassMain {
    public static void main(String[] args) {

     //account1 and account2 are the objects of Account class
        Account account1 = new Account();
        Account account2 = new Account();
    }
}

How to access members?

You can access members (call methods and access instance variables) by using the dot(.) operator.

For example, methods can be accessed as :

account.deposit();

This statement calls the deposit() method inside the Account class for an account object.

When you call the method using the above statement, all statements within the body of deposit() method are executed. Then, the control of the program jumps back to the statement following account.deposit().

Similarly, the instance variable can be accessed as:

account.balance;

Example: Java Class and Objects

class Student {
   //instance variables
    int rollno;
    String name;
   //methods
    public void insertRecord(int roll_no, String student_name) {
        rollno = roll_no;
        name = student_name;
    }

    public void displayDetail() {
        System.out.println(rollno + " " + name);
    }
}

public class ClassMain {
    public static void main(String args[]) {

       //s1 and s2 are two objects of Student class
        Student s1 = new Student();
        Student s2 = new Student();
        s1.insertRecord(101, "Manish");
        s2.insertRecord(202, "Arun");
        s1.displayDetail();
        s2.displayDetail();
    }
}

Output

101 Manish
202 Arun

Example explained

In the above program,

We have mentioned the term method quite a few times. In the next chapter, you will learn about Java methods in detail.

 

 

 

Exit mobile version