Classes and Objects in Java
In Java, classes and objects are
basic concepts of Object Oriented Programming (OOPs) that are used to represent
real-world concepts and entities. The class represents a group of objects
having similar properties and behavior. For example, the animal
type Dog is a class while a particular dog named Tommy is
an object of the Dog class.
Java Classes
A class in Java is a set of objects which shares common
characteristics/ behavior and common properties/ attributes. It is a
user-defined blueprint or prototype from which objects are created. For
example, Student is a class while a particular student named Ravi is an object.
class Circle
{
double x,y;
// The coordinates of the center
double r; // The radius
// Method that
returns circumference
double circumference(){
return 2*3.14159*r;
}
// Method that
returns area double area(){
return (22/7)*r*r;
}
}
class CircleDemo1 {
public static void main(String args[]){ Circle c = new Circle();
c.x = 0.0;
c.y = 0.0;
c.r = 5.0;
System.out.println("Circumference" + c.circumference());
System.out.println("Area" + c.area());
}
}
Points to remember:
- There
should be a class which contains a method main(). This class is called main
class.
- There
should be only one main class.
- The name
of the program file should be same as the name of the main class followed by
.java as an extension.
- If there
is no main class, then there should be compilation error.
No comments:
Post a Comment