What is a Class? in JAVA

 

What is a Class?


• A class is a blueprint that defines the variables and methods common to all objects of a certain kind.

• Example: ‘your dog’ is a object of the class Dog.

• An object holds values for the variables defines in the class.

• An object is called an instance of the Class.

• A basis for the Java language.

• Each concept we wish to describe in Java must be included inside a class.

• A class defines a new data type, whose values are objects:

• A class is a template for objects

• An object is an instance of a class


Class Definition


 A class contains a name, several variable declarations (instance

variables) and several method declarations. All are called

members of the class.


 General form of a class:

class classname {


type instance-variable-1;

...

type instance-variable-n;

type method-name-1(parameter-list) { ... }

type method-name-2(parameter-list) { ... }

...

type method-name-m(parameter-list) { ... }


}



Example: Class Usage


class Box {

double width;

double height;

double depth;

}

class BoxDemo {

public static void main(String args[]) {

Box mybox = new Box();

double vol;

mybox.width = 10;

mybox.height = 20;

mybox.depth = 15;

vol = mybox.width * mybox.height * mybox.depth;

System.out.println ("Volume is " + vol);

} }


Post a Comment (0)
Previous Post Next Post