Class And Object

JavaJavaBeginner
Practice Now

Introduction

In this lab, you are going to learn a very important programming pattern: the object-oriented-programming. You need to know the difference between class and object. Take more practice to get a better understanding.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/method_overloading("`Method Overloading`") java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/inner_classes("`Inner Classes`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_attributes("`Class Attributes`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/constructors("`Constructors`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/encapsulation("`Encapsulation`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/method_overloading -.-> lab-178544{{"`Class And Object`"}} java/scope -.-> lab-178544{{"`Class And Object`"}} java/inner_classes -.-> lab-178544{{"`Class And Object`"}} java/classes_objects -.-> lab-178544{{"`Class And Object`"}} java/class_attributes -.-> lab-178544{{"`Class And Object`"}} java/class_methods -.-> lab-178544{{"`Class And Object`"}} java/constructors -.-> lab-178544{{"`Class And Object`"}} java/encapsulation -.-> lab-178544{{"`Class And Object`"}} java/modifiers -.-> lab-178544{{"`Class And Object`"}} java/oop -.-> lab-178544{{"`Class And Object`"}} java/identifier -.-> lab-178544{{"`Class And Object`"}} java/arrays -.-> lab-178544{{"`Class And Object`"}} java/comments -.-> lab-178544{{"`Class And Object`"}} java/data_types -.-> lab-178544{{"`Class And Object`"}} java/operators -.-> lab-178544{{"`Class And Object`"}} java/output -.-> lab-178544{{"`Class And Object`"}} java/strings -.-> lab-178544{{"`Class And Object`"}} java/system_methods -.-> lab-178544{{"`Class And Object`"}} end

Class and Object

There are many programming languages and almost as many programming styles (sometimes called paradigms). The programs we have written so far are mainly procedural, because the emphasis has been on specifying computational procedures. Most Java programs are object-oriented, which means that the focus is on objects and their interactions. Here are some of the characteristics of object-oriented programming:

Objects often represent the entities in the real world. The majority of methods are object methods (like the methods you invoke on Strings) rather than class methods (like the Math methods). The methods we have written so far have been class methods. In this chapter, we will write some object methods.

Objects are isolated from each other by limiting the ways they interact, especially by preventing them from accessing instance variables without invoking methods. Classes are organized in family trees where new classes extend existing classes, adding new methods and replacing others.

In Java, every class extends some class or the other, except the class Object. The most basic class is Object class. It contains no instance variables, but it provides the methods equals and toString, among others.

Many classes extend Object, including almost all of the classes we have written and many Java types like Integer, Double, String, array etc. Any class that does not explicitly name a parent extends Object by default. The โ€œfamily treeโ€ of classes is called class hierarchy. Object usually appears at the top, with all the child classes below.

Classes and objects usually have some attributes which are used to describe these classes/objects, and the methods are used to perform some specific functions. Here we give you an example to show you what a class is like, save the code to objectTest.java and use command line to run the code.

Example:

Write the following code in the /home/labex/project/objectTest.java file:

// use keyword 'class' to create a new class named Person
class Person
{
    /*
     * there are four properties,
     * name of string type, age of int type
     * gender of string type, weight of float type
    */
    private String name;
    private int age;
    private String gender;
    private float weight;
    // this is a constructor with no return type, the same name as the class, no parameters
    public Person(){

    }
    //this is a constructor with two parameters
    public Person(String name, int age){
        // use parameters to set object's name and age
        this.name = name;
        this.age = age;
    }
    // this is a constructor with four parameters
    public Person(String name, int age, String gender, float weight){
        // use parameters to set object's name, age, gender and weight
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.weight = weight;
    }
       // this is an object method
    public void personInfo(){
        System.out.println("Name: " + this.name + "\nAge: "+ this.age + "\nGender: " + this.gender + "\nWeight: "+this.weight);
    }
}

public class objectTest{
    public static void main(String[] args){
        // use 'new' to create an instance of class Person
        Person p = new Person("aaa",18,"male",50.5f);  // p refers to  an object
        /* you can try these two different construction methods, and see what the output is.
         * Person p  = new Person();
         * Person p = new Person("aaa",18);
        */
        // use object to invoke method personInfo()
        p.personInfo();
    }
}

Output:

Run the objectTest.java file using the following commands:

javac /home/labex/project/objectTest.java
java objectTest

See the output:

Name: aaa
Age: 18
Gender: male
Weight: 50.5

There are four attributes in the Person class. All the attributes are private, so they can't be accessed from outside (can only be used inside the class). There is one object method named personInfo used to print personal information and there are three constructors with the same name as the class name Person. The only difference among them is their parameters. This behavior is called overloading.

Summary

This part is very important. Classes are abstraction of entities with common features and objects are entities with specific features. You should take more time to understand the programming style mentioned above.

Other Java Tutorials you may like