Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising

Introduction to Class,Object and Method in Java
Previous Home Next

Class:Class is a collection of similar type of object. Class is a blueprint that are used to create objects, and to define object data types and methods. A class is a collection of data and methods that operate on that data.

Class is a collection of similar type of object. A class is a blue print for the object. That blueprint describes the each object behaviors and state variable. Collection of objects is called class. It is a logical entity.

A class represent a category of objects by abstracting their properties and behaviour. A class can be thought of as blueprint for creating objects. An object has the properties and behaviour defined by its class. The class can be thought of as a user- defined data type and object as a variable/ instance of the data type represented by the class.

The properites/attribute of an object are defined by data members/ fields in java. A data member/field in a class is a variable that can hold data. The behaviors / operations of an object are defined using methods in java. Both data members and method are refferred to as members of the class.

Object:Object are the real world entity. Entity are something which has extension in real world. An entity that has state and behavior is known as an object. An object is created based on its class When an object is created, memory is allocated to hold the object properties.

If we consider the real-world we can find many objects around us, Cars, Dogs, Humans, etc. All these objects have a state and behavior. Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.

Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

An object has three characteristics:

  1. State:
  2. represents data (value) of an object.

  3. Behavior:
  4. (Methods / Function / Operatoins)represents the behavior (functionality) of an object such as deposit, withdraw etc.

  5. Identity:
  6. Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely

    Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior.

Classes provide a way of packing together data and functions which work on the data. In Java terminology, this data items are known as fields and functions are known as methods. A class is a blue print from which we can create objects of the same type.

How to define a class?

First thing is find out a name to your class. Assume we are defining a class “Box”.

class Box
{
 
}

class is a keyword and then Box is a user defined class name. Then write the whole body of the class inside the curly brackets {}.

How to add data members to your class?

Suppose our class “Box” has three variable – length, width and height. So here we choose to create three int variables to store length, width and height. Let’s name these variables as length, width and height.

class Box
{
    int length;
    int width;
    int height;
}

You can give any type for the variables you create depending on what value the variable will hold. Here I just selected int as the type for all of our variables.

How to add methods to your class?
Syntax:

type nameOfTheMethod(parameters)
{ 
         body of the method 
} 

“nameOfTheMethod” is the name of the method that you give.

“type” specifies the type of the value that the method returns. This can be a primitive data type (int, long, etc) or class type. If your method does not return any value, you need to give the type as void.

parameters : Here we give the input parameters to our method.

body of the method : Here we give the logic of the method.

We are add the first method to our class “Box”. This method should take three input values and using those values, it should set the values of our variables length, width and height. name of the method as “setValues”.

setValues: This method should take three input values. So we will define three input parameters for our method. Here will define them as int type because we will be using their values to set the values of our int type variables length, width and height. Let’s give the names as “x,”y”,”z” for these input parameters.

setValues(int x, int y, int  z)

This method does not return anything. So we will write the return type as void.

void setValues(int x, int y, int  z)

Adding curly brackets to our method.

void setValues(int x, int y, int  z)
{
}

Now write the logic of the method.

void setValues(int x, int y, int z)
{
    length = x;
    width = y;
    height = z;
}

Our class should look like

class Box
{
    int length;
    int width;
    int height;
 
    void setValues(int x, int y, int z)
    {
        length = x;
        width = y;
        height = z;
    }
}

Adding another method to get length, this method should return the length of Box just need to return the value of the variable length. name of the method as getLength. So we will write as

getLength:There is no need of any input parameter, So we can write as:

getLength()

This method should return the value of the variable length. Remember we have already defined length as an int variable. So here the value returned will be of type int.

int getLength()

Adding curly brackets to our method

int getLength()
{
}

Now write the logic of the method. It should return length of the Box So we will write our method as

int getLength()
{
    return length;
}

So our class should be as given below

class Box
{
    int length;
    int width;
    int height;
 
    void setValues(int x, int y, int z)
    {
        length = x;
        width = y;
        height = z;
    }
 
    int getLength()
    {
        return length;
    }

Adding another Method to findVolume. This should be a very simple task for you now. This method should find out the volume of the box from its length, width and height (Volume = length * width * height) and return it. Name of this method as findVolume and there is no input parameter for this. We will give return type as “int”.

int findVolume()
{
        int volume = length * width * height;
        return volume;
}

Our class should like this

class Box
{
    int length;
    int width;
    int height;
 
    void setValues(int x, int y, int z)
    {
        length = x;
        width = y;
        height = z;
    }
 
    int getLength()
    {
        return length;
    }
 
    int findVolume()
    {
        int volume = length * width * height;
        return volume;
    }
}
How to Create Objects from class

we can create objects of same types from a class. To create objects we can use an operator “new” For example

Box testBox;
testBox = new  Box();

The first statement declares a reference variable “testBox”. This reference variable “testBox” can point to any of the objects of the class “Box”. Note that at this moment we have created only the reference variable. No memory is allocated at the moment.

In the second statement, we are actually creating the object of the class “Box” using the statement “new Box();” and then assigning this object to the reference variable “testBox”. In this object creation, memory will be allocated for our new object. Memory allocation of objects happens in the heap memory.

Note: that the above two statements can be combined to a single statement as given below.

Box testBox = new Box();

Now let’s take another example. Consider the following code snippet.

Box testBox1=  new Box();
Box testBox2=  new Box();
Box testBox3=  myBox2;

Its two objects are create here.

The statement “Box testBox1= new Box();” will create an object of the class “Box” which will be referred by the reference variable testBox1.

The statement “Box testBox2= new Box();” will create an object of the class “Box” which will be referred by the reference variable testBox2.

The third statement “Box testBox3= myBox2;” will not create another object of “Box”. It creates a reference variable “testBox3” and this reference variable also will point to the same object that testBox2 referrers to.

Previous Home Next