Total Pageviews

Friday, 30 August 2024

Java Class and Objects

 

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:

  1. There should be a class which contains a method main(). This class is called main class.
  2. There should be only one main class.
  3. The name of the program file should be same as the name of the main class followed by .java as an extension.
  4. If there is no main class, then there should be compilation error.




Home                                                                                                                        Previous





Internships: 



Placements:




Material
Deep Learning



Java: 

Deep learning  CNN    




No comments:

Post a Comment