Classes and Objects in Java with examples

By | November 14, 2018

Classes and Objects in Java

classes and objects in java
classes and objects in java


To understand classes and objects in java, In order to take the example of the class so let’s consider basically
We divide people depending upon there professions. We classified
people on the base of there professions like doctors, lawyers, drivers,
teachers, engineers and all those kinds of professions.

So basically a class is a classification of a certain type of objects
In a similar way, we can also classify certain things in different groups
while using them in Java.

If you want to learn classes and objects in Java, So let’s take this example of the class.  let’s consider, basically
We divide people depending upon there professions. understand how object-oriented programming works.
let’s go ahead and create a new class.

To create a class you should type public or private keyword
and then you must type a class keyword and then type your desired
class name.

Note: You must type the first word of the class name in Capital letter.

Syntax to create a class:

If you want to create a class then you have to write a public or private keyword, class keyword, Class Name, and two middle

For Example:

If I want to create class named Doctor then I will type…..

public class Doctor{


A class contains the following two things.


Data: data means variables
Functions: Functions means that you are creating inside the class.

When you want to create a class in NetBeans then you have to click
on your project and then click on Source packages.

After clicking on source packages right click on the package if
haven’t created package yet first create your package by clicking
on the default package.

Right click on the package then click on new and then create
java Class. Type your desired class name make sure it starts
with capital letter. Click ok so your class will be created.

So if you are using IntelliJ Idea then you must go to your project
click on it and under your project you will see src folder
right click on that folder and then new and then select a new
java Class.Give any name to your class so your class will be created

Objects in Java:

Objects is created in Main class following is the syntax to create an object
for any class.

classname objectname = new classname()

e.g Doctor patient1 = new Doctor()

If you found this article useful share it with your friends to motivate me to write such type of articles

See Also:

Inheritance in Java

Follow me on facebook for more

Leave a Reply

Your email address will not be published. Required fields are marked *