Declaration and Implementation

In this lesson, you will learn about the declaration and implementation details of a class.

The written code of a class and its attributes are known as the definition or implementation of the class.

Declaration

In Java, we define classes in the following way:

Press + to interact
class ClassName { // Class name
/* All member variables
and methods*/
}

The class keyword tells the compiler that we are creating our custom class. All the members of the class will be defined within the class scope.

Creating a class object

The name of the class, ClassName, will be used to create an instance of the class in our main program. We can create an object of a class by using the keyword new:

Press + to interact
class ClassName { // Class name
...
// Main method
public static void main(String args[]) {
ClassName obj = new ClassName(); // className object
}
}

Implementation of a Car class

Let’s implement the Car class illustrated below:

Press + to interact
//The Structure of a Java Class
class Car { // Class name
// Class Data members
int topSpeed;
int totalSeats;
int fuelCapacity;
String manufacturer;
// Class Methods
void refuel(){
...
}
void park(){
...
}
void drive(){
...
}
}