0% found this document useful (0 votes)
11 views23 pages

Java Classes and Objects Explained

The document provides an overview of classes and objects in Java, highlighting key concepts such as encapsulation, inheritance, polymorphism, and abstraction. It explains the structure of a class, the process of creating objects, and the role of constructors, access modifiers, and the garbage collection mechanism. Overall, it emphasizes the importance of classes as blueprints for objects in object-oriented programming.

Uploaded by

sharwaribhoyar77
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views23 pages

Java Classes and Objects Explained

The document provides an overview of classes and objects in Java, highlighting key concepts such as encapsulation, inheritance, polymorphism, and abstraction. It explains the structure of a class, the process of creating objects, and the role of constructors, access modifiers, and the garbage collection mechanism. Overall, it emphasizes the importance of classes as blueprints for objects in object-oriented programming.

Uploaded by

sharwaribhoyar77
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Classes & Objects: The

Heart of OOP
Index
• Class Declaration Syntax
• Creating Objects from a Class
• Example of a Simple Class
• Access Modifiers in Java Classes
• Instance vs Static Members
• Constructor in Java
• Encapsulation in Java
• Polymorphism in Java
• Abstraction in Java
• Summary
• What is an Object?
• Creating an Object in Java
• Accessing Fields and Methods of an Object
• The this Keyword in Java
• Object Initialization
• Garbage Collection in Java
• Conclusion
What is class ?
Definition: A class is a blueprint or prototype from which
objects are created in Java.
Components:
Fields (variables)

Methods (functions)

Constructors (special methods to initialize objects)

Access modifiers (to define the visibility of the class


Class Declaration Syntax

• public class ClassName { // Fields type


fieldName; // Constructor public ClassName()
{ // Initialization code } // Methods public
returnType methodName() { // method body }}
• Explanation: The public keyword is an access modifier.
The class name should match the file name in Java.
Creating Objects from a Class
Object Definition: An instance of a class is called an
object.
• Syntax
• ClassName obj = new ClassName();
• Example:
• Car myCar = new Car();
• In this example, Car is the class, and myCar is the
object.
Example of a Simple Class
Public class Car {
// Fields
String model;
int year;
// Constructor
public Car(String model, int year) {
[Link] = model;
[Link] = year;
}
// Method
public void startEngine() {
[Link](“The engine of the “ + model + “ is starting.”);
}
}
Access Modifiers in Java Classes
• Public: Accessible from anywhere.
• Private: Accessible only within the class.
• Protected: Accessible within the same package and
subclasses.
• Default: Accessible within the same package (no
modifier).
Instance vs Static Members
Instance members: Belong to an instance of the class
(objects).
Accessed through objects.
Example: [Link]()
Static members: Belong to the class itself, not the objects.
Accessed using the class name or an object.
Public static void displayMessage() {
[Link](“Hello from static method”);
}
Constructor in Java
Purpose: A constructor initializes an object when it’s created.
Types:
Default Constructor: No arguments.
Parameterized Constructor: Accepts arguments.
Example:
Public class Car {
String model;
int year;
public Car(String model, int year) {
[Link] = model;
[Link] = year;
}
}
Inheritance and Class Hierarchy
Inheritance: One class (child) can inherit properties and behaviors (fields and methods) from another class
(parent).
Public class ChildClass extends ParentClass {
// Additional fields and methods
}
Example:
Class Vehicle {
String brand;
public void honk() {
[Link](“Honk!”);
}
}

class Car extends Vehicle {


int doors;
public void display() {
[Link](“This is a car.”);
}
}
Encapsulation in Java
Definition: Encapsulation is the concept of hiding the internal state of an object and
only allowing it to be modified through public methods (getters and setters).
Example:
Public class Person {
private String name; // private field
public String getName() { // getter
return name;
}

public void setName(String name) { // setter


[Link] = name;
}
}
Polymorphism in Java
Definition: The ability of a class to take multiple forms.
Types:
Compile-time Polymorphism (Method Overloading)
Runtime Polymorphism (Method Overriding)
Example (Method Overloading):
Class Calculator {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}
}
Abstraction in Java
• Definition: Abstraction is the concept of hiding the
implementation details and showing only the essential
features of the object.
• Implemented using:
• Abstract classes
• Interfaces
Example (Abstract class):
Abstract class Animal {
abstract void sound();
}

class Dog extends Animal {


void sound() {
[Link](“Bark”);
}
}
Summary :

A class defines the structure of objects.

Key concepts: Encapsulation, Inheritance, Polymorphism,


Abstraction.

Methods, Constructors, and Fields form the building


blocks of a class.

A class in Java is a central idea of Object-Oriented


Programming.
What is an Object?
• Definition: An object is an instance of a class in Java. It
represents a real-world entity with attributes (fields) and
behaviors (methods).
• Components of an Object:
• State: Defined by the fields (variables) of the class.
• Behavior: Defined by the methods (functions) of the
class.
• Identity: Each object has a unique identity.
Creating an Object in Java
Syntax: To create an object, you first need to instantiate
a class using the new keyword.
ClassName objectName = new ClassName();
Example:
Car myCar = new Car();
Explanation:
Car is the class.
myCar is the object (instance of the class).
New Car() initializes the object.
Accessing Fields and
Methods of an Object
Accessing Fields: Object fields can be accessed using the dot (.)
operator.
[Link] = “Tesla”; // Accessing the field
Calling Methods: Similarly, methods can be invoked on an object.
[Link](); // Calling the method
Example:
Class Car {
String model;
public void startEngine() {
[Link](“The engine of “ + model + “ is starting.”);
}
The this Keyword in Java
Definition: The this keyword refers to the current instance of the object.
Usage:
To differentiate between instance variables and parameters with the same name.
To refer to the current object from within its methods or constructors.
Example:
Class Car {
String model;

public Car(String model) {


[Link] = model; // `[Link]` refers to the field, `model` refers to the parameter
}

public void displayModel() {


[Link](“Model: “ + [Link]);
}
}
Object Initialization
Constructors: A constructor is used to initialize an object when it is created. It has the same name as the class.
Default Constructor: If no constructor is provided, Java automatically provides a default constructor.
Parameterized Constructor: A constructor that accepts parameters to initialize the object.
Example:class Car {
String model;
int year;

// Parameterized constructor
public Car(String model, int year) {
[Link] = model;
[Link] = year;
}
}

Car myCar = new Car(“Tesla”, 2023);


Garbage Collection in Java
Definition: In Java, objects are automatically managed by
the garbage collector, which frees up memory by removing
objects that are no longer referenced.
Memory Management:
Once an object is no longer referenced, it becomes
eligible for garbage collection.
Important: Developers don’t need to manually free memory
like in languages such as C++.
Example:
myCar = null; // `myCar` is no longer referencing the Car
object
Conclusion:
• In object-oriented programming, a class is a blueprint
for creating objects, defining their attributes (data) and
behaviors (methods). An object is an instance of a class,
representing a specific entity with its own values for the
attributes.
• Classes provide structure and reusability, while objects
bring them to life by storing unique data and executing
behaviors. This approach enhances code organization,
modularity, and scalability in software development.
Thank you

You might also like