Java Object and Classes

Java object and classes

Java is an object oriented language. It is developed by Sun Microsystems and in java everything is object. It is a high level programming language and platform independent. Object oriented language means everything is encapsulated in class.

Java supports the object oriented language features. Object oriented features are: polymorphism, inheritance, encapsulation, abstraction, classes, objects, instance, and method.

What is an Object: anything having state and behavior is called as object. Every class defines the state and the behavior of the program. Table, pen, car, chair is the examples of object. State is the data of the object and behavior is the functionality of the object. Like name color and model is the state of the object and behavior is the function like car to breaks, pen to write and chair to sit. Object has identity.

 

Creating an object: In java, we use the new keyword to create an object. A class provides the blueprints for objects. Declaring instantiation and initializton.

What is a Class: Class is a set of objects. Classes create the object and objects use the methods to communicate between them. Classes have data members and methods. Class contains the data member, method, constructor, block, class and interface.

Declaring a class (syntax)

Public class ClassName

{

    //fields

    fieldType fieldname;

 

//methods

Public returnType methodName()

{

    Statements;

}

}

Example:

Public class car

{

String name;

String model;

String color;

Void changing_gear()

{

}   

Void applying_brakes()

{

}

}

Data members and the data methods are the members of the class. Public private and protected are the access modifiers. Which means private can be accessed only within the class and public can be accessed from the outside the class also.

 

Constructor: constructor is another form of function that is used to initialize the object. Constructor is called at the time of object creation. Constructor is known as constructor because it constructs the value i.e. provides data for the object. There are some rules for the constructor which is followed when we call the constructor i.e. constructor must have the same name as the class. And it will not return anything means no explicit return type.

Constructor is of two types:

  1. Default constructor
  2. Parameterized constructor

Default constructor: constructor with no arguments is known as default constructor. It shows the default value.

Example:

class car

{

    car()

    {

        System.out.println(“ car is created using constructor”);

    }

    public static void main(string args[])

    {

        car c= new car();

    }

}

Output: car is created using constructor

Parameterized constructor: constructor is called with arguments.

Example:

class student

{

    int id;

    string name;

    student(int i,string n)

    {

        id=i;

        name=n;

    }

    void display()

    {

        system.out.println(id+” “+name);

    }

    public static void main(String args[])

    {

        student s1=new student(1,”Anant”);

        student s2=new student(2,”Anaya”);

        s1.display();

        s2.display();

    }

}

Output:

1 Anant

2 Anaya

Three ways to create an object:

  1. Object creation by reference:

 

Example:

 

class employee

{

    Int id;

    String name;

}

class employee1

{

        public static void main(string args[])

        {

            employee e1=new employee();

            e1.id=1;

            e1.name=”Anant”    ;

            system.out.println( e1.id +” “ +e1.name);

        }

}

  1. Object creation by method:

 

Example:

 

class employee

{

    Int id;

    String name;

    Void add(int i,string n)

    {

        id=I;

        name=n;

    }

    Void show()

{

    system.out.println( id +” “ +name);


}

   

}

class employee1

{

        public static void main(string args[])

        {

            employee e1=new employee();

            e1.add(1,”Anant”);

            e1.show();

        }

}

 

  1. Object creation using constructor:

 

Example:

 

class student

{

    int id;

    string name;

    student(int i,string n)

    {

        id=i;

        name=n;

    }

    void display()

    {

        system.out.println(id+” “+name);

    }

    public static void main(String args[])

    {

        student s1=new student(1,”Anant”);

        student s2=new student(2,”Anaya”);

        s1.display();

        s2.display();

    }

}

Output:

1 Anant

2 Anaya

 

 

Share this post