Object and Class in JAVA

In this blog ,we will learn  about Java Objects .In Object Oriented programming techniques ,here we are going to design a program by using Objects and Class.

  1. Object is the physical and logical entity
  2. Class is the logical entity .

objects

OBJECT IN JAVA :

It  have states and behavior is known as Object e.g chair, table, car, pen, marker, table. It can be tangible and intangible (physical or logical ).One of the intangible example is banking system.Why Learn Java?

It has three characteristics ,there are:

  • States –Represent Data value
  • Behavior-Represent the functionality of an object such as move,running,etc.
  • Identity-The identity is implemented via a unique ID

For example : Bike is an Object. It name is Honda,the color is Black .it is known as States. Its used to drive ,so driving is behavior .

They have similar characteristics when we compare the software with a real-World objects .

In software objects also have states and behavior. In software objects is stored in fields and behavior is shown via methods.

Object is an instance of class : Class is a blueprint from which  individual objects are created .So object is result of class.

Object Definition :

  • It is a real world entity.
  • It is a run time entity.
  • It is an entity which has state and behavior.
  • It is an instance of a class.

Using Objects in Java Programs

import java.util.Date;
class DateApp {
    public static void main(String args[]) {
        Date today = new Date();
        System.out.println(today);
    }
}

Different ways to create an object in Java?

There are many ways to create an object in java. They are:

  • By new keyword
  • By new Instance() method
  • By clone() method
  • By deserialization
  • By factory method etc.

Classes in Java

  1. Class is a group of objects which have common properties.
  2. It is a blueprint or template from which objects are created.
  3. It is a logical entity.
  4. It can’t be physical.

A class in Java can contain: fields, methods, constructors, blocks, nested class and interface

Syntax to declare a class:

class <class_name>

{

field;

method;

}

A class can contain any of the following variable types there are :

  • Local variables
  • Instance variables
  • Class variables

Method in Java

A method is like function Which is used to expose behavior of an object.

Advantage of Method

  • Code Reusability
  • Code Optimization

Sample of a Class :

Example

public class Dog

{

String breed;

int age;

String color;

void barking() {

}

void hungry() {

}

void sleeping() {

}

}

Learn Here :-

Introduction to Java Progarmming 

Learn More About Java j2EE course content PDF download