Declaration and Implementation
In this lesson, you will learn about the declaration and implementation details of a class.
We'll cover the following
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:
class ClassName { // Class name/* All member variablesand 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
:
class ClassName { // Class name...// Main methodpublic static void main(String args[]) {ClassName obj = new ClassName(); // className object}}
Implementation of a Car
class
Let’s implement the Car
class illustrated below:
//The Structure of a Java Classclass Car { // Class name// Class Data membersint topSpeed;int totalSeats;int fuelCapacity;String manufacturer;// Class Methodsvoid refuel(){...}void park(){...}void drive(){...}}