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

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

PDF
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
KaviShetty
Ā 
PPTX
) Function overriding, Member Classes Nesting of Classes.pptx
sachinkumar541404
Ā 
PDF
Inner Classes in Java
Dallington Asingwire
Ā 
PPTX
Nitish Chaulagai Java1.pptx
NitishChaulagai
Ā 
PPTX
object oriented programming unit two ppt
isiagnel2
Ā 
PPTX
Object Oriented Programming Tutorial.pptx
ethiouniverse
Ā 
PPT
Object and Classes in Java
backdoor
Ā 
PPTX
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
Ā 
PPTX
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
Ā 
PDF
java-2marks-qzcvhsvnmdjdvjndcsdcuestions-with-answer.pdf
vijaybabu0905
Ā 
PDF
Abap object-oriented-programming-tutorials
cesarmendez78
Ā 
PPT
4 Classes & Objects
Praveen M Jigajinni
Ā 
PPTX
python.pptx
Dhanushrajucm
Ā 
PPT
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
Ā 
PDF
Java questions for interview
Kuntal Bhowmick
Ā 
PPTX
Inner Classes & Multi Threading in JAVA
Tech_MX
Ā 
DOCX
Object oriented basics
vamshimahi
Ā 
DOCX
Object oriented programming tutorial
Ghulam Abbas Khan
Ā 
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
KaviShetty
Ā 
) Function overriding, Member Classes Nesting of Classes.pptx
sachinkumar541404
Ā 
Inner Classes in Java
Dallington Asingwire
Ā 
Nitish Chaulagai Java1.pptx
NitishChaulagai
Ā 
object oriented programming unit two ppt
isiagnel2
Ā 
Object Oriented Programming Tutorial.pptx
ethiouniverse
Ā 
Object and Classes in Java
backdoor
Ā 
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
Ā 
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
Ā 
java-2marks-qzcvhsvnmdjdvjndcsdcuestions-with-answer.pdf
vijaybabu0905
Ā 
Abap object-oriented-programming-tutorials
cesarmendez78
Ā 
4 Classes & Objects
Praveen M Jigajinni
Ā 
python.pptx
Dhanushrajucm
Ā 
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
Ā 
Java questions for interview
Kuntal Bhowmick
Ā 
Inner Classes & Multi Threading in JAVA
Tech_MX
Ā 
Object oriented basics
vamshimahi
Ā 
Object oriented programming tutorial
Ghulam Abbas Khan
Ā 

Recently uploaded (20)

PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
Ā 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
Ā 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
Ā 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
Ā 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
Ā 
PDF
ģœ ė‹ˆķ‹°ģ—ģ„œ Burst Compiler+ThreadedJobs+SIMD ģ ģš©ģ‚¬ė”€
Seongdae Kim
Ā 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
Ā 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
Ā 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
Ā 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
Ā 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
Ā 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
Ā 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
Ā 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
Ā 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
Ā 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
Ā 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
Ā 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
Ā 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
Ā 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
Ā 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
Ā 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
Ā 
Import Data Form Excel to Tally Services
Tally xperts
Ā 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
Ā 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
Ā 
ģœ ė‹ˆķ‹°ģ—ģ„œ Burst Compiler+ThreadedJobs+SIMD ģ ģš©ģ‚¬ė”€
Seongdae Kim
Ā 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
Ā 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
Ā 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
Ā 
Engineering the Java Web Application (MVC)
abhishekoza1981
Ā 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
Ā 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
Ā 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
Ā 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
Ā 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
Ā 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
Ā 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
Ā 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
Ā 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
Ā 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
Ā 
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