The class keyword in Java is fundamental to the language's object-oriented programming model. It is used to define a new class, which can contain fields (variables) and methods to define the behavior of objects created from the class.
Usage
A class serves as a blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data.
ClassName: The name of the class, which should start with an uppercase letter by convention.
fields: Variables that hold the state of the class.
methods: Functions that define the behavior of the class.
Examples
Example 1: Basic Class Definition
classAnimal{
String name;
int age;voiddisplayInfo(){
System.out.println("Name: "+ name);
System.out.println("Age: "+ age);}}publicclassMain{publicstaticvoidmain(String[] args){
Animal dog =newAnimal();
dog.name ="Buddy";
dog.age =5;
dog.displayInfo();}}Altyapı:
In this example, the Animal class has two fields, name and age, and one method, displayInfo(). The Main class demonstrates creating an Animal object and setting its fields.
Example 2: Class with Constructor
classCar{
String model;
int year;// ConstructorCar(String model, int year){this.model = model;this.year = year;}voiddisplayDetails(){
System.out.println("Model: "+ model);
System.out.println("Year: "+ year);}}publicclassMain{publicstaticvoidmain(String[] args){
Car car =newCar("Toyota",2020);
car.displayDetails();}}Altyapı:
This example introduces a constructor in the Car class to initialize the model and year fields. The Main class creates a Car object using the constructor and displays its details.
Example 3: Inheritance
classAnimal{voidmakeSound(){
System.out.println("Animal makes a sound");}}classDogextendsAnimal{voidmakeSound(){
System.out.println("Dog barks");}}publicclassMain{publicstaticvoidmain(String[] args){
Dog dog =newDog();
dog.makeSound();}}Altyapı:
This example demonstrates inheritance. The Dog class extends the Animal class and overrides the makeSound() method. The Main class creates a Dog object and calls the overridden method.
Tips and Best Practices
Naming Conventions: Always start class names with an uppercase letter and use camel case for readability.
Encapsulation: Use private fields and provide public getter and setter methods to protect the internal state of the class.
The
classkeyword in Java is fundamental to the language's object-oriented programming model. It is used to define a new class, which can contain fields (variables) and methods to define the behavior of objects created from the class.Usage
A class serves as a blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data.
Syntax
ClassName: The name of the class, which should start with an uppercase letter by convention.fields: Variables that hold the state of the class.methods: Functions that define the behavior of the class.Examples
Example 1: Basic Class Definition
In this example, the
Animalclass has two fields,nameandage, and one method,displayInfo(). TheMainclass demonstrates creating anAnimalobject and setting its fields.Example 2: Class with Constructor
This example introduces a constructor in the
Carclass to initialize themodelandyearfields. TheMainclass creates aCarobject using the constructor and displays its details.Example 3: Inheritance
This example demonstrates inheritance. The
Dogclass extends theAnimalclass and overrides themakeSound()method. TheMainclass creates aDogobject and calls the overridden method.Tips and Best Practices