Classes and Objects


Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. A class is like an object constructor, or a “blueprint” for creating objects.

Class Components
A class in Java can contain:
  • Fields,
  • Methods,
  • Constructors,
  • Blocks, and
  • Nested classes and interfaces.
Creating a Class
To create a class, use the keyword class. A class should always start with an uppercase first letter, and the name of the Java file should match the class name.
class <class_name> {  
  field;  
  method;  
}

Objects
An entity that has state and behavior is known as an object, e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). The example of an intangible object is the banking system. An object (e.g., a pen) has three characteristics: