Class In Object Oriented Programming

Home /

Table of Contents

In object-oriented programming, a class is a blueprint for creating objects that encapsulate data and behavior. It defines a set of attributes (data members) and methods (functions) that the objects created from it will possess. A class provides a template or structure for objects, allowing us to create multiple instances of the same type of object, each with its own state and behavior. In essence, a class defines the properties and capabilities of a particular type of object, and objects created from the class are known as instances of that class.

In simple words, a class is a blueprint from which individual objects are created/constructed.

G9kNGa9iCvhPKVWIDOtzsTznI IWhaxD13AM3WCrYdvCUq6zETr8wfd oVZUFjVVRmjaFIfCMyY8x1WmlNu7uP 1ScT2ldSwymhta7G2vLQfeEcmW e fMW9QawoJzn3HUsDlWJzN7kHk6oEiFwPMs - Class In Object Oriented Programming
Things an object knows about itself are called instance variables.
Things an object can do are called methods.
A class is not an object (but it’s used to construct them).
KCChwDprURD4M8DBZgldFRUb2w7zWqJbWi3xwR7DuQ5QS3EMTX78Qm2UNpv81GvHdUAA9mTcVmGig1ACfMxpWmvA5okzvR j52c77AjvaHqih6twyiiLOjkWybEZ8QPJyNgky3SH 6ROIBFj3a0t4Dg - Class In Object Oriented Programming

A class is an object’s blueprint. It instructs the virtual machine on how to create an object of that type. Each object derived from that class can have its own values for the class’s instance variables. For example, you could use the Button class to create dozens of different buttons, each with its own color, size, shape, label, and so on.

What do Java Classes Contain?

A class in Java can contain:

  • Data Member(instance variables or fields)
  • Method
  • Constructor
  • Block
  • Class
  • Interface

How to Declare a Class in Java?

A class is declared by using the class keyword followed by a non-reserved identifier that names it. The class’s body is delimited by a pair of matching open and close brace characters ( and ). Consider the standard syntax for declaring a Java class:

class <class_name>{
data member;
method;
}

The first letter of a class’s name is always capitalized, by convention, and all subsequent characters are lowercase (for example, Button). If a name is made up of several words, the first letter of each word is capitalized (such as ButtonDemo).   This naming convention is known as camel casing.

When “designing” a class, consider the objects that will be constructed from that class type. Think about:

  • things the object knows(data that it contains)
  • things the object does(how to manipulate that data or do some actions).
- Class In Object Oriented Programming

In this lesson, we want to design a class called Button. This Button class is the blueprint, using which we can create buttons. We have already shown you the UML Diagram of the Button class. We are now going to implement this class in Java.

class Button{
    String label;//field or instance variable#1
    String color;//field or instance variable#2
   
    //method#1
    public void setColor(String color){
        this.color = color;
    }
    //method#2
    public void seLabel(String label){
        this.label = label;
    }
    //method#3
    public void doPress(){
        //instructions go here
    }
    //method#4
    public void undoPress(){
        //instructions go here
    }
}

The Java programming language syntax will be unfamiliar to you, but the design of this class is based on the previous discussion of button objects. The object’s state is represented by the fields(label and color), and its interaction with the outside world is defined by the methods (setColor, setLabel, doPress, and undoPress).

You might have noticed that the Button class lacks a main method. That's because it's not a complete application; it's simply a blueprint for buttons that could be used in an application. It is the responsibility of another class in your application to create and use new Button objects.

Write a Class

xz S24SV3Ta9VrqaPUPmNK0Xb4Cv - Class In Object Oriented Programming
class Button{
    String label;//field or instance variable#1
    String color;//field or instance variable#2
   
    //method#1
    public void setColor(String color){
        this.color = color;
    }
    //method#2
    public void seLabel(String label){
        this.label = label;
    }
    //method#3
    public void doPress(){
        //instructions go here
    }
    //method#4
    public void undoPress(){
        //instructions go here
    }
}

Write a Tester Class

dZkLDgKlvQ1Jqbn1P56wB7Aeqw5Bp0dKcbsq3Y74xYYT34mgY7pCRO6QqfsX0 L2zHWZJNXdqzYoE9MUGcNtBp37SxuWteRhdiNCkz8J6ecMNszTay0ppFbdQKctTjK38p30e5 fVcYc7AmlcHXMk Q - Class In Object Oriented Programming
class ButtonTest{
    public static void main(String[] args) {
 
    }
}

Make an Object

ZN0 h779SY7TTpnq6b89y19FchyLQnN71tZP5Gmbnbx2HeWwTxhs e6RTPyvX27IfqblUoD4XLDdkvDenXGwhonON42K74sllPQDvoZGZX54FN5AYEvKE LaHEz7dWPEpRmXCU - Class In Object Oriented Programming
class ButtonTest{
    public static void main(String[] args) {
        Button buttonOne = new Button();//creating first button
        buttonOne.setColor("red");//setting color field
        buttonOne.seLabel("RED Button");//setting label field
 
        System.out.println("The label of buttonOne is : "+buttonOne.label+" and it's color is: "+ buttonOne.color);
 
    }
}



We have written the complete class in a single source file called ButtonTest.java for simplicity. 

class Button{
    String label;//field or instance variable#1
    String color;//field or instance variable#2
   
    //method#1
    public void setColor(String color){
        this.color = color;
    }
    //method#2
    public void seLabel(String label){
        this.label = label;
    }
    //method#3
    public void doPress(){
        //instructions go here
    }
    //method#4
    public void undoPress(){
        //instructions go here
    }
}
 
class ButtonTest{
    public static void main(String[] args) {
        Button buttonOne = new Button();//creating first button
        buttonOne.setColor("red");//setting color field
        buttonOne.seLabel("RED Button");//setting label field
 
        Button buttonTwo = new Button();//creating second button
        buttonTwo.setColor("green");//setting color field
        buttonTwo.seLabel("GREEN Button");//setting label field
 
        System.out.println("The label of buttonOne is : "+buttonOne.label+" and it's color is: "+ buttonOne.color);
        System.out.println("The label of buttonTwo is : "+buttonTwo.label+" and it's color is: "+ buttonTwo.color);
    }
}

To compile: javac ButtonDemo.java
To run the program: java ButtonTest

Output:

The label of buttonOne is : RED Button, and it's color is: red
The label of buttonTwo is : GREEN Button, and it's color is: green

If you are already OO savvy, you’ll know we’re not using encapsulation. We will go there later.

Look carefully at the above program. Always remember, to compile a source file, you must give the name of the source file. In other words, the name you gave while you saved the file in the directory. On the other hand, to run the program you have to give the class name where the main() method resides. Otherwise, your JVM can not find the main() method and hence can not start the program.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article