Goseeko blog

What is Class and Object in java?

by Bhumika

Object

Object has states and behaviors that they exhibit. A dog, for example, has states such as color, name, and breed, as well as behaviors such as waving the tail, barking, and eating. A class’s instance is an object.

Let’s take a closer look at what objects are. Many objects, such as vehicles, pets, humans, and other living things, can be found in the actual world. There is a state and a behavior for each of these things.

If we consider a dog, its state includes its name, breed, and color, as well as its activity, which includes barking, wagging the tail, and running.

When you compare a software item to a physical object, you’ll notice that they share a lot of similarities.

Creating objects from class

​​ClassName objectName = new ClassName();

Class 

A class is a template/blueprint for the behavior/state that objects of that type support. Individual objects are formed from a blueprint called a class. It refers to the set of attributes or methods that are shared by all objects of the same type.

In general, class declarations can have the following elements in the following order:

  • Modifiers: A class can be private or public, and it can have default access.
  • Class keyword: Use the class keyword to create a class.
  • Class name:  The name of the class should start with an initial letter (capitalized by convention).
  • Superclass(if any): If there is a superclass, it is: If there is one, the name of the class’s parent (superclass), followed by the term extends. A class can only extend (subclass) one parent at a time.
  • Interfaces(if any): A comma-separated list of interfaces that the class implements, prefixed by the keyword implements. A single class can implement more than one interface.
  • Body: The body of the class is surround via braces.

Syntax to declare a class:

class <class_name>{  

    field;  

    method;  

}  

Interested in learning about similar topics? Here are a few hand-picked blogs for you!

You may also like