Java Classes and Objects

Java is an Object-Oriented Language. Java is associated with classes and objects, along with its attributes and methods. 

A classes is collection of attributes and methods.

An object is an instance of a class.. A object is used to access property of class.

The keyword class is used to create a class.

Syntax to declare a class:

class <class_name>{  
    variable;  
    method;  
}  

We can create object using new keyword .

 Syntax to create a object:

Class_name object_name = new constructor_name;

Example :
Fruit obj = new Fruit();

 

Example :

import java.util.*;

public class Fruit {
String name ="Apple";
public static void main(String[] args) {
Fruit obj = new Fruit();
System.out.println("Fruit Name : "+obj.name);
}
}

Output :

Fruit Name : Apple

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

29417