Java_Class_Object_Method
Java_Class_Object_Method
1. Class in Java
A class is a blueprint or template for creating objects. It defines attributes (fields) and behaviors (methods).
Syntax:
class ClassName {
// Fields
int x;
// Methods
void sayHello() {
System.out.println("Hello!");
2. Object in Java
An object is an instance of a class. You create an object using the `new` keyword.
Syntax:
3. Method in Java
A method is a block of code that performs a specific task. It belongs to a class and can be called on an
object.
Java: Class, Object, and Method
Syntax:
returnType methodName(parameters) {
// code
Example:
class Car {
String color;
int speed;
void drive() {
color = c;
speed = s;
void showInfo() {
}
Java: Class, Object, and Method
myCar.setDetails("Red", 100);
myCar.showInfo();
myCar.drive();
Output:
Color: Red
Speed: 100
Summary