Professional Documents
Culture Documents
Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exception Handling Java Programs Java Project Java Collections Interview Java Intervie
class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;
Output 1
0
null
Output 2
GeeksForGeeks
Output 3
GeeksForGeeks
1. Modifiers: A class can be public or has default access (Refer this for
details).
2. Class keyword: class keyword is used to create a class.
3. Class name: The name should begin with an initial letter (capitalized by
convention).
4. Superclass(if any): The name of the class’s parent (superclass), if any,
preceded by the keyword extends. A class can only extend (subclass)
one parent.
5. Interfaces(if any): A comma-separated list of interfaces implemented
by the class, if any, preceded by the keyword implements. A class can
implement more than one interface.
6. Body: The class body is surrounded by braces, { }.
Constructors are used for initializing new objects. Fields are variables that
provide the state of the class and its objects, and methods are used to implement
the behavior of the class and its objects.
There are various types of classes that are used in real-time applications such
as nested classes, anonymous classes, and lambda expressions.
Java Objects
An object in Java is a basic unit of Object-Oriented Programming and represents
real-life entities. Objects are the instances of a class that are created to use the
attributes and methods of a class. A typical Java program creates many objects,
which as you know, interact by invoking methods. An object consists of :
Java Objects
Objects correspond to things found in the real world. For example, a graphics
program may have objects such as “circle”, “square”, and “menu”. An online
shopping system might have objects such as “shopping cart”, “customer”, and
“product”.
Note: When we create an object which is a non primitive data type, it’s
always allocated on the heap memory.
When an object of a class is created, the class is said to be instantiated. All the
instances share the attributes and the behavior of the class. But the values of
those attributes, i.e. the state are unique for each object. A single class may have
any number of instances.
Example:
As we declare variables like (type name;). This notifies the compiler that we will
use the name to refer to data whose type is type. With a primitive variable, this
declaration also reserves the proper amount of memory for the variable. So for
reference variables , the type must be strictly a concrete class name. In general,
we can’t create objects of an abstract class or an interface.
Dog tuffy;
The new operator instantiates a class by allocating memory for a new object and
returning a reference to that memory. The new operator also invokes the
class constructor.
Example:
Java
// Class Declaration
// method 1
public String getName() { return name; }
// method 2
public String getBreed() { return breed; }
// method 3
public int getAge() { return age; }
// method 4
public String getColor() { return color; }
Output
Hi my name is tuffy.
My breed,age and color are papillon,5,white
Java
Output
Note: All classes have at least one constructor. If a class does not explicitly
declare any, the Java compiler automatically provides a no-argument
constructor, also called the default constructor. This default constructor
calls the class parent’s no-argument constructor (as it contains only one
statement i.e super();), or the Object class constructor if the class has no
other parent (as the Object class is the parent of all classes either directly
or indirectly).
Example:
clone() method is present in the Object class. It creates and returns a copy of the
object.
4. Deserialization
Example:
In the inheritance system, we use a parent class reference variable to store a sub-
class object. In this case, we can switch into different subclass objects using the
same referenced variable.
Example:
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
public class Test
{
// using Dog object
Animal obj = new Dog();
// using Cat object
obj = new Cat();
}
btn.setOnAction(new EventHandler()
{
public void handle(ActionEvent event)
{
System.out.println("Hello World!");
}
});
Class Object
Previous Next
Similar Reads
Understanding Classes and Objects in
Java.util.Objects class in Java
Java
Private Constructors and Singleton Parent and Child Classes Having Same
Classes in Java Data Member in Java
Complete Tutorials
Spring Tutorial
G Gaurav Miglani
Additional Information
Company Explore Languages DSA Data Science & HTML & CSS
A-143, 9th Floor, Sovereign Corporate About Us Job-A-Thon Hiring Python Data Structures ML HTML
Tower, Sector-136, Noida, Uttar Pradesh - Challenge
Legal Java Algorithms Data Science With CSS
201305
Careers Hack-A-Thon C++ DSA for Beginners Python Bootstrap
In Media GfG Weekly Contest PHP Basic DSA Problems Data Science For Tailwind CSS
O line Classes Beginner
Contact Us GoLang DSA Roadmap SASS
(Delhi/NCR) Machine Learning
Advertise with us SQL Top 100 DSA LESS
DSA in JAVA/C++ Tutorial
GFG Corporate R Language Interview Problems Web Design
Master System ML Maths
Solution Android Tutorial DSA Roadmap by
Design Sandeep Jain Data Visualisation
Placement Training Tutorials Archive
Master CP Tutorial
Program All Cheat Sheets
GeeksforGeeks Pandas Tutorial
Apply for Mentor
Videos NumPy Tutorial
NLP Tutorial
Deep Learning
Tutorial
Grokking Modern
System Design
NCERT School Subjects Commerce Management & UPSC Study SSC/ BANKING
Solutions Mathematics Accountancy Finance Material SSC CGL Syllabus
Class 11 Chemistry Indian Economics HR Managament Geography Notes SBI Clerk Syllabus
Class 9 Social Science Microeconimics Finance Science and IBPS Clerk Syllabus
Class 8 English Grammar Statistics for Economics Technology Notes SSC CGL Practice