Introduction
In Object-Oriented Programming (OOP), two fundamental building blocks shape the architecture of our code: Classes and Interfaces. While both serve as blueprints for creating organized and maintainable code, they each bring distinct capabilities and serve different purposes in software design. For developers navigating through Java, C#, or any other OOP language, understanding the key differences between classes and interfaces is crucial for making informed design decisions. In this blog, we will explore these essential OOP concepts, helping you grasp when and why to use each one, ultimately enabling you to write more elegant and flexible code.

Check out this article - Duck Number in Java
Classes in Java
A class is a user-defined data type. It is a blueprint or template which is used for creating objects. Each object has some properties and methods. A class encapsulates the data and behavior of an object into a single entity. This property makes a class easy to use and maintain.
Properties of a Class
- Encapsulation: It is a mechanism that encapsulates related data and behavior into a single unit. It helps in maintaining the security and integrity of code
- Inheritance: A class can inherit properties and methods from another class
- Polymorphism: It can have multiple methods with the same name but different implementations and parameters. Polymorphism allows more versatility in the code
- Constructors: They are used to initialize objects of the class. A class can have one or more than one constructor
- Access modifiers: The access modifier determines the class's or its members' visibility. It can be public, protected, or private
Syntax:
<access_modifier> class <class_name>{
//fields
<access_modifer> <data_type> <field_name>;
//constructors
<access_modifer> <class_name> ([parameters]){
//constructor body
}
//methods
<access_modifer> <return_type> <method name> ([parameters]){
//method body
}
}
- The access modifier determines the visibility of the class or its members. It can be public, protected, or private
- Here <class_name> is the name of the class
- The fields hold the data of the class
- The constructor method initializes the object of the class when it is created
Example:
Output

Explanation
In this example, we have a NinjaStudent class with two properties, name, age, and two methods, Ninja() and NinjaAction(). We then created a new NinjaStudent object called Ninja() and NinjaAction() methods.
Also see, Eclipse ide for Java Developers