Introduction
Classes and objects are the two most fundamental Java notions that each programmer must know. Classes and objects are nearly connected and function together. An object has behaviours and states and is an illustration of the class. For example, a cat is an object, its colour and proportions are its states, and its meowing and clawing effects are its behaviours. A class models the object; it is a blueprint or template that defines the state or behaviour assisted by objects of that kind.
What is an Object in Java
A Java object is a component (also called an instance) of a Java class. Every object has an identity, a behaviour and a state. The state of an object is held in fields (variables), while methods (functions) depict the object's behaviour. Objects are designed at runtime from templates, which are also known as classes. They are characterized by three features:-
- Identity:- The identity is an attribute used to uniquely specify that object – such as a spontaneous ID number or an address in remembrance.
- State:- A Java object’s states are stored in domains that define the respective features of that object.
- Behaviour:- The object’s behaviour is exposed via methods that use its inner state.
Our Learners Also Read: What Is JUnit: An Overview of the Best Java Testing Framework
What is Class in Java
A Java class is a collection of objects that have shared properties. It is a template or blueprint from which objects are designed. It is an analytical entity. It cannot be physical or materialistic. Classes are varieties, and objects are items within each variety. All class objects should have fundamental class attributes. A class in Java is a rational template to design objects that share common properties and techniques. Classes are needed in OOPs because they give a template for designing objects, which can wrap code into data. It has descriptions of methods and data. It keeps the inheritance property of object-oriented programming and hence can preserve the class hierarchy. In general, class statements can contain these components, in order:
- Modifiers: A class can be public or contain default access.
- Class keyword: A class keyword is used to design a class.
- Class name: The title should start with an initial letter (capitalized by convention).
- Superclass: The name of the class parent (superclass) is foregone by the keyword extends. Only one subclass can be extended by a class.
- Interfaces: A comma-separated list of interfaces executed by the class, if any, foregone by the keyword implements. A class can execute more than one interface at a time.
- Body: The class body is enclosed in braces.
How to Create a Class
You can describe a specific class in Java using the following syntax:-
{
//Bodyoftheclass
}
Example:
public class Student
{
String Studname;
int Studrollno;
String section;
void study() {
}
void write() {
}
void play() {
}
}
Example of Java program
Here is a program to add two integers using Java:
class Main
{
public static void main(String[] args)
{
int firstnum = 40;
int secondnum = 60;
// add two numbers
int sumnum = firstnum + secondnum;
System.out.print("The sum is:" firstnum + " + " + secondnum + " = " + sumnum);
}
}
Output:
Enter two numbers
40 60
The sum is: 40+60=100
Full Stack Java Developer Course
Today, the demand for professionals who can create end-to-end applications, store data, and test codes is at a gush. Get your goal career as an experienced Full Stack Java Developer with The IoT Academy. It presents you with a comprehensive and well-developed Full Stack Java Developer course online. The course syllabus is categorised into four modules. The first module contains HTML5, CSS3 & Bootstrap5. In the second module, you will discover JavaScript. Module three pacts with Module Bundler & Toolchain. By the time, you get the fourth module, you will understand React: JavaScript Framework. It is a great selection of professions and the simplest way to evolve as a developer. Demand for full-stack developers reaches from tech and tech-enabled organisations across industries proposing a lot of scope for a long-term profession.
Conclusion
Object-oriented programming revolves around the visions of objects and classes. In Java, the classes are directed to as templates for the objects while the objects are instances of a class so, the objects can inherit all the attributes, variables, and operations of the class.