🏛️Classes and Objects
🏗️ What is a Class?
A class is essentially a template or blueprint that defines what objects within a Java program will look like and be able to do. Think of it like a cookie cutter - the class is the mold that objects get "baked" from during runtime.
Within a class you define variables and methods. Variables store data, like a cookie's shape and color. Methods are actions the objects can perform, like rotating or growing.
When you create a class, it doesn't actually make any objects yet. But any objects created with "new" will be of that class type.
For example:
The Person
here is a class. It acts as a blueprint for creating Person
objects.
🏛️ Rules for Classes and Objects in Java 🏛️
Classes and objects are fundamental concepts in Java's object-oriented programming paradigm. There are certain rules around defining and using classes and objects.
📜 Rules for Classes 📜
Some key rules for defining classes in Java are:
A class must have a name that is a valid identifier
The class body must be between opening and closing braces {}
A class can contain fields, methods, constructors, nested classes etc.
Fields and methods can have any access modifier (public, private, protected, default)
Only one public class per source file is allowed
For example:
This defines a Person
class with a private field and public constructor.
🎁 Rules for Objects 🎁
Some important rules related to objects are:
Objects must be instantiated using the
new
keyword before useData fields in an object can be accessed using the
.
operatorMethods of an object can be invoked using the
.
operatorThe
this
keyword refers to the current object instanceObjects in Java are passed by reference, not value
For example:
This creates a Person
object and accesses its fields and methods.
So those are some of the key rules around defining and working with classes and objects in Java. Following these rules allows creating robust object-oriented code.
🤓 Key Notes
A class is declared using the
class
keywordIt describes what attributes and behaviors an object will have
Objects are created (instantiated) based on the class
The class is a template, objects are instances of the template
So in summary, a class provides a definition for the attributes and behaviors of objects that will be created from it. It's a blueprint describing what the object will contain.
💎 In Simple Terms
A class is like a mold for making objects - it defines what the objects made from that mold will be like.
Objects are created from a class mold that describes them. The class itself is just the template, not the actual object.
So in Java, you define classes which act as blueprints for objects!
🥽 What is an Object?
An object in Java is an instance of a class. It is created from a class blueprint and contains its own state and behaviors.
For example:
Here obj
is an object of type Person that is instantiated from the Person
class.
🍃 Key Notes
An object is an instance of a class
It is created via new keyword from a class
It has its own set of attributes and behaviors
Multiple objects can be made from a single class
So an object is basically a real world entity represented inside a program.
Last updated