SlideShare a Scribd company logo
CLASSES AND OBJECTS
Classes and Objects
• In Java, classes and objects are basic concepts of
Object Oriented Programming (OOPs) that are used to
represent real-world concepts and entities. The class
represents a group of objects having similar properties
and behavior.
For example, the animal type Dog is a class while a
particular dog named Tommy is an object of
the Dog class.
Classes
A class in Java is a set of objects which shares common
characteristics/ behavior and common properties/
attributes. It is a user-defined blueprint or prototype
from which objects are created. For example, Student is a
class while a particular student named Ravi is an object.
Properties of Java Classes
1.Class is not a real-world entity. It is just a template or blueprint
or prototype from which objects are created.
2.Class does not occupy memory.
3.Class is a group of variables of different data types and a group
of methods.
• Data member
• Method
• Constructor
• Nested Class
• Interface
Class Declaration
Components of Java Classes
1.Modifiers: A class can be public or has default access
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, { }.
Access modifiers for classes or
interfaces
But, the classes and interfaces themselves can have only
two access modifiers when declared outside any other
class.
1) public
2) default (when no access modifier is specified)
Note: Nested interfaces and classes can have all access
modifiers.
Note: We cannot declare class/interface with private or
protected access modifiers.
Access modifiers for classes or
interfaces
In Java, methods and data members can be encapsulated by
the following four access modifiers. The access modifiers are
listed according to their restrictiveness order.
1) private (accessible within the class where defined)
2) default or package-private (when no access modifier is
specified)
3) protected (accessible only to classes that subclass your
class directly within the current or different package)
4) public (accessible from any class)
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.
Inner Class
Inner class refers to the class that is declared inside class
or interface which were mainly introduced, to sum up,
same logically relatable classes as Java is purely object-
oriented so bringing it closer to the real world. Now
geeks you must be wondering why they were
introduced?
There are certain advantages associated
with inner classes are as follows:
• Making code clean and readable.
• Private methods of the outer class can be accessed, so
bringing a new dimension and making it closer to the
real world.
• Optimizing the code module.
Types of Inner Classes
1.Nested Inner Class
2.Method Local Inner Classes
3.Static Nested Classes
4.Anonymous Inner Classes
Nested Inner Class
It can access any private instance variable of the outer class. Like
any other instance variable, we can have access modifier private,
protected, public, and default modifier. Like class, an interface can
also be nested and can have access specifiers.
Sample Nested Inner Class
Output:
In a nested class
method
Method Local Inner Classes
Method Local Inner Class
Inner class can be declared within a method of an outer class
which we will be illustrating in the below example where Inner is
an inner class in outerMethod().
Output:
inside outerMethod
inside innerMethod
Static Nested Classes
Static nested classes are not technically inner classes. They are like
a static member of outer class.
Static Nested Classes
Output
inside inner class Method
inside outerMethod
Anonymous Inner Classes
• Anonymous inner classes are declared without any name at all.
They are created in two ways.
• As a subclass of the specified type
• As an implementer of the specified interface
Anonymous Inner Classes
Output
i am in show method of super class
i am in Flavor1Demo class
Output:
In a nested class method
The benefits of using inner classes in Java
are:
• Encapsulation: Inner classes can access private variables and
methods of the outer class. This helps to achieve encapsulation and
improves code readability.
• Code Organization: Inner classes allow you to group related code
together in one place. This makes your code easier to understand
and maintain.
• Better Access Control: Inner classes can be declared as private,
which means that they can only be accessed within the outer class.
This provides better access control and improves code security.
• Callbacks: Inner classes are often used for implementing callbacks in
event-driven programming. They provide a convenient way to define
and implement a callback function within the context of the outer
class.
The benefits of using inner classes in
Java are:
• Polymorphism: Inner classes can be used to implement
polymorphism. You can define a class hierarchy within the outer
class and then create objects of the inner classes that implement
the different subclasses.
• Reduced Code Complexity: Inner classes can reduce the
complexity of your code by encapsulating complex logic and
data structures within the context of the outer class.
• Overall, the use of inner classes can lead to more modular,
maintainable, and flexible code.
Anonymous Inner Class
Nested Classes in Java is prerequisite required before adhering
forward to grasp about anonymous Inner class. It is an inner class
without a name and for which only a single object is created. An
anonymous inner class can be useful when making an instance of
an object with certain “extras” such as overriding methods of a
class or interface, without having to actually subclass a class.
Tip: Anonymous inner classes are useful in writing
implementation classes for listener interfaces in graphics
programming.
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 :
1.State: It is represented by attributes of an object.
It also reflects the properties of an object.
2.Behavior: It is represented by the methods of an
object. It also reflects the response of an object
with other objects.
3.Identity: It gives a unique name to an object and
enables one object to interact with other objects.
Example of an object: dog
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”.
Declaring Objects (Also called instantiating
a class)
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.
Initializing a Java object
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.
Initializing a Java object
Initializing a Java object
Output:
Hi my name is tuffy.
My breed, age and color are papillon,5,white

More Related Content

PDF
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
PPTX
Presentation2.ppt java basic core ppt .
PPTX
Javasession8
PDF
Classes and Object Concept Object Oriented Programming in Java
DOCX
Nested class in java
PDF
oops-123991513147-phpapp02.pdf
PPTX
DAY_1.4.pptx
PDF
Java unit 7
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
Presentation2.ppt java basic core ppt .
Javasession8
Classes and Object Concept Object Oriented Programming in Java
Nested class in java
oops-123991513147-phpapp02.pdf
DAY_1.4.pptx
Java unit 7

Similar to Object oriented programming CLASSES-AND-OBJECTS.pptx (20)

PDF
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
PPTX
) Function overriding, Member Classes Nesting of Classes.pptx
PDF
Inner Classes in Java
PPTX
Nitish Chaulagai Java1.pptx
PPTX
object oriented programming unit two ppt
PPTX
Object Oriented Programming Tutorial.pptx
PPT
Object and Classes in Java
PPTX
Unit II Inheritance ,Interface and Packages.pptx
PPTX
classes-objects in oops java-201023154255.pptx
PDF
java-2marks-qzcvhsvnmdjdvjndcsdcuestions-with-answer.pdf
PDF
Abap object-oriented-programming-tutorials
PPTX
OBJECT ORIENTED PROGRAMMING_Unit2_firsthalf Updated.pptx
PPT
4 Classes & Objects
PPTX
python.pptx
PPT
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
PDF
Java questions for interview
PPTX
Inner Classes & Multi Threading in JAVA
DOCX
Object oriented basics
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
) Function overriding, Member Classes Nesting of Classes.pptx
Inner Classes in Java
Nitish Chaulagai Java1.pptx
object oriented programming unit two ppt
Object Oriented Programming Tutorial.pptx
Object and Classes in Java
Unit II Inheritance ,Interface and Packages.pptx
classes-objects in oops java-201023154255.pptx
java-2marks-qzcvhsvnmdjdvjndcsdcuestions-with-answer.pdf
Abap object-oriented-programming-tutorials
OBJECT ORIENTED PROGRAMMING_Unit2_firsthalf Updated.pptx
4 Classes & Objects
python.pptx
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
Java questions for interview
Inner Classes & Multi Threading in JAVA
Object oriented basics
Ad

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
history of c programming in notes for students .pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
AI in Product Development-omnex systems
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Essential Infomation Tech presentation.pptx
PDF
Understanding Forklifts - TECH EHS Solution
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Nekopoi APK 2025 free lastest update
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
System and Network Administraation Chapter 3
Introduction to Artificial Intelligence
wealthsignaloriginal-com-DS-text-... (1).pdf
history of c programming in notes for students .pptx
top salesforce developer skills in 2025.pdf
Digital Strategies for Manufacturing Companies
Which alternative to Crystal Reports is best for small or large businesses.pdf
How Creative Agencies Leverage Project Management Software.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
AI in Product Development-omnex systems
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
VVF-Customer-Presentation2025-Ver1.9.pptx
Upgrade and Innovation Strategies for SAP ERP Customers
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Essential Infomation Tech presentation.pptx
Understanding Forklifts - TECH EHS Solution
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Nekopoi APK 2025 free lastest update
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
System and Network Administraation Chapter 3
Ad

Object oriented programming CLASSES-AND-OBJECTS.pptx

  • 2. Classes and Objects • In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior.
  • 3. For example, the animal type Dog is a class while a particular dog named Tommy is an object of the Dog class.
  • 4. Classes A class in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes. It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object.
  • 5. Properties of Java Classes 1.Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created. 2.Class does not occupy memory. 3.Class is a group of variables of different data types and a group of methods. • Data member • Method • Constructor • Nested Class • Interface
  • 7. Components of Java Classes 1.Modifiers: A class can be public or has default access 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, { }.
  • 8. Access modifiers for classes or interfaces But, the classes and interfaces themselves can have only two access modifiers when declared outside any other class. 1) public 2) default (when no access modifier is specified) Note: Nested interfaces and classes can have all access modifiers. Note: We cannot declare class/interface with private or protected access modifiers.
  • 9. Access modifiers for classes or interfaces In Java, methods and data members can be encapsulated by the following four access modifiers. The access modifiers are listed according to their restrictiveness order. 1) private (accessible within the class where defined) 2) default or package-private (when no access modifier is specified) 3) protected (accessible only to classes that subclass your class directly within the current or different package) 4) public (accessible from any class)
  • 10. 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.
  • 11. Inner Class Inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is purely object- oriented so bringing it closer to the real world. Now geeks you must be wondering why they were introduced?
  • 12. There are certain advantages associated with inner classes are as follows: • Making code clean and readable. • Private methods of the outer class can be accessed, so bringing a new dimension and making it closer to the real world. • Optimizing the code module.
  • 13. Types of Inner Classes 1.Nested Inner Class 2.Method Local Inner Classes 3.Static Nested Classes 4.Anonymous Inner Classes
  • 14. Nested Inner Class It can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public, and default modifier. Like class, an interface can also be nested and can have access specifiers.
  • 15. Sample Nested Inner Class Output: In a nested class method
  • 17. Method Local Inner Class Inner class can be declared within a method of an outer class which we will be illustrating in the below example where Inner is an inner class in outerMethod().
  • 19. Static Nested Classes Static nested classes are not technically inner classes. They are like a static member of outer class.
  • 21. Output inside inner class Method inside outerMethod
  • 22. Anonymous Inner Classes • Anonymous inner classes are declared without any name at all. They are created in two ways. • As a subclass of the specified type • As an implementer of the specified interface
  • 24. Output i am in show method of super class i am in Flavor1Demo class
  • 25. Output: In a nested class method
  • 26. The benefits of using inner classes in Java are: • Encapsulation: Inner classes can access private variables and methods of the outer class. This helps to achieve encapsulation and improves code readability. • Code Organization: Inner classes allow you to group related code together in one place. This makes your code easier to understand and maintain. • Better Access Control: Inner classes can be declared as private, which means that they can only be accessed within the outer class. This provides better access control and improves code security. • Callbacks: Inner classes are often used for implementing callbacks in event-driven programming. They provide a convenient way to define and implement a callback function within the context of the outer class.
  • 27. The benefits of using inner classes in Java are: • Polymorphism: Inner classes can be used to implement polymorphism. You can define a class hierarchy within the outer class and then create objects of the inner classes that implement the different subclasses. • Reduced Code Complexity: Inner classes can reduce the complexity of your code by encapsulating complex logic and data structures within the context of the outer class. • Overall, the use of inner classes can lead to more modular, maintainable, and flexible code.
  • 28. Anonymous Inner Class Nested Classes in Java is prerequisite required before adhering forward to grasp about anonymous Inner class. It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class. Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.
  • 29. 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
  • 30. An object consists of : 1.State: It is represented by attributes of an object. It also reflects the properties of an object. 2.Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects. 3.Identity: It gives a unique name to an object and enables one object to interact with other objects.
  • 31. Example of an object: dog
  • 32. 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”.
  • 33. Declaring Objects (Also called instantiating a class) 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.
  • 34. Initializing a Java object 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.
  • 36. Initializing a Java object Output: Hi my name is tuffy. My breed, age and color are papillon,5,white