| Previous | Home | Next |
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.
A class represent ADT(Abstract Data Type). It acts like a template using which we can create multiple objects. A class Declaration only create a template, it does not create an actual object . A class creates a new data type that can be used to create object. That a class creates a logical framework that defines the relationship between its member. when we declare an object of a class, we are creating an instance of that class. thus a class is a logical construct. An object has physically reality.
Declaration of class:
Access modifiers( public)( abstract | final) class class_name extends
MySuperClass implements YourInterface
{
// behaviors and state variables are declared between the { and } characters
}
- Modifiers declare whether the class is public, abstract, or final.
- The class declaration must contain the class keyword and the class name of the class that you are defining.
- The class name, with the initial letter capitalized by convention (Title case).
- The name of the superclass, if any, preceded by the keyword extends. A class can only extend one subclass.
- The name of the interfaces implemented by the class, if any, preceded by the keyword implements.
- A class can implement more than one interface.
- The class body, surrounded by braces, {}.
Example :
import java.util.Scanner;
class B
{
private int x;
private float y;
void getData()//for user input
{
Scanner sc;
sc=new Scanner(System.in);
System.out.print("Enter Value Of x :");
x=sc.nextInt();
System.out.print("Enter value of y :");
y=sc.nextFloat();
}
void setdata(int x1,float y1)//for testing purpose
{
x=x1;
y=y1;
}
void display()
{
System.out.println(x+","+y);
}
}
class BTest
{
public static void main(String args[])
{
B a1,a2;
a1=new B();
a2=new B();
//a1.x=5 ;//error
//a1.getdata();
//a2.gatdata();
a1.setdata(10,10.5f);
a2.setdata(2,5.4f);
a1.display();
a2.display();
}
}
output :
10,10.5 2,5.4
| Previous | Home | Next |