SlideShare una empresa de Scribd logo
1 de 31
Descargar para leer sin conexión
Programming in Java
Lecture 5: Objects and Classes
By
Ravi Kant Sahu
Asst. Professor, LPU
Contents
• Class
• Object
• Defining and adding variables
• Nested Classes
• Abstract Class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
OBJECTS AND CLASS
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
4
Class
• A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
Circle
centre
radius
circumference()
area()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
What is a class?
• A class can be defined as a template/ blue print that
describe the behaviors/states that object of its type
support.
• A class is the blueprint from which individual objects
are created.
• A class defines a new data type which can be used to
create objects of that type. Thus, a class is a template
for an object, and an object is an instance of a class.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Classes and Objects
• A Java program consists of one or more classes.
• A class is an abstract description of objects.
• Here is an example class:
class Dog { ...description of a dog goes here... }
• Here are some objects of that class:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
7
More Objects
• Here is another example of a class:
– class Window { ... }
• Here are some examples of Windows:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• The data, or variables, defined within a class are called
instance variables because each instance of the class (that
is, each object of the class) contains its own copy of these
variables.
• The code is contained within methods.
• The methods and variables defined within a class are
called members of the class.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Defining Classes
 The basic syntax for a class definition:
 Bare bone class – no fields, no methods
public class Circle {
// my circle class
}
class ClassName
{
[fields declaration]
[methods declaration]
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Adding Fields: Class Circle with fields
• Add fields
• The fields (data) are also called the instance
variables.
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Circle Class
• aCircle, bCircle simply refers to a Circle object, It is
not an object itself.
aCircle
Points to nothing (Null Reference)
bCircle
Points to nothing (Null Reference)
null null
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Creating objects of a class
• An object is an instance of the class which has well-
defined attributes and behaviors.
• Objects are created dynamically using the new keyword.
• aCircle and bCircle refer to Circle objects.
bCircle = new Circle();aCircle = new Circle();
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P
aCircle
Q
bCircle
Before Assignment
P
aCircle
Q
bCircle
Before Assignment
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Adding Methods
• A class with only data fields has no life. Objects
created by such a class cannot respond to any
messages.
• Methods are declared inside the body of the class but
immediately after the declaration of data fields.
• The general form of a method declaration is:
type MethodName (parameter-list)
{
Method-body;
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Adding Methods to Class Circle
public class Circle {
public double x, y; // centre of the circle
public double r; // radius of circle
//Methods to return circumference and area
public double circumference() {
return 2*3.14*r;
}
public double area() {
return 3.14 * r * r;
}
}
Method Body
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Automatic garbage collection
• The object does not have a reference and
cannot be used in future.
• The object becomes a candidate for automatic
garbage collection.
• Java automatically collects garbage periodically and
releases the memory used to be used in the future.
Q
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
finalize()
• The finalize() method is declared in the java.lang.Object class.
• Before an object is garbage collected, the runtime system calls
its finalize() method.
• The intent is for finalize() to release system resources such as
open files or open sockets before getting collected.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Accessing Object/Circle Data
Circle aCircle = new Circle();
aCircle.x = 2.0 // initialize center and radius
aCircle.y = 2.0
aCircle.r = 1.0
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Executing Methods in Object/Circle
• Using Object Methods:
Circle aCircle = new Circle();
double area;
aCircle.r = 1.0;
area = aCircle.area();
sent ‘message’ to aCircle
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Nested Class
• The Java programming language allows us to define a
class within another class. Such a class is called a nested
class.
Example:
class OuterClass
{
...
class NestedClass
{
...
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Types of Nested Classes
• A nested class is a member of its enclosing class.
• Nested classes are divided into two categories:
– static
– non-static
• Nested classes that are declared static are simply
called static nested classes.
• Non-static nested classes are called inner classes.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Why Use Nested Classes?
• Logical grouping of classes—If a class is useful to only one other
class, then it is logical to embed it in that class and keep the two
together.
• Increased encapsulation—Consider two top-level classes, A and B,
where B needs access to members of A that would otherwise be
declared private. By hiding class B within class A, A's members can
be declared private and B can access them. In addition, B itself can
be hidden from the outside world.
• More readable, maintainable code—Nesting small classes within
top-level classes places the code closer to where it is used.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Static Nested Classes
• A static nested class is associated with its outer class similar to class
methods and variables.
• A static nested class cannot refer directly to instance variables or
methods defined in its enclosing class.
• It can use them only through an object reference.
• Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
• For example, to create an object for the static nested class, use this
syntax:
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Inner Classes
• An inner class is associated with an instance of its enclosing class
and has direct access to that object's methods and fields.
• Because an inner class is associated with an instance, it cannot
define any static members itself.
• Objects that are instances of an inner class exist within an instance
of the outer class.
• Consider the following classes:
class OuterClass {
...
class InnerClass { ... }
}
• An instance of InnerClass can exist only within an instance of
OuterClass and has direct access to the methods and fields of its
enclosing instance.
• To instantiate an inner class, we must first instantiate the outer class.
Then, create the inner object within the outer object.
• Syntax:
OuterClass.InnerClass innerObject =
outerObject.new InnerClass();
• Additionally, there are two special kinds of inner classes:
– local classes and
– anonymous classes (also called anonymous inner classes).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Local Classes
• Local classes are classes that are defined in a block, which is a
group of zero or more statements between balanced braces.
• For example, we can define a local class in a method body, a for
loop, or an if clause.
• A local class has access to the members of its enclosing class.
• A local class has access to local variables. However, a local class
can only access local variables that are declared final.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Anonymous Classes
• Anonymous classes enable us to declare and instantiate a class
at the same time.
• They are like local classes except that they do not have a
name.
• The anonymous class expression consists of the following:
1. The new operator
2. The name of an interface to implement or a class to extend.
3. Parentheses that contain the arguments to a constructor, just like a
normal class instance creation expression.
4. A body, which is a class declaration body. More specifically, in
the body, method declarations are allowed but statements are not.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
 Anonymous classes have the same access to local variables of the enclosing
scope as local classes:
• An anonymous class has access to the members of its enclosing class.
• An anonymous class cannot access local variables in its enclosing scope that are not
declared as final.
 Anonymous classes also have the same restrictions as local classes with
respect to their members:
• We cannot declare static initializers or member interfaces in an anonymous class.
• An anonymous class can have static members provided that they are constant
variables.
 Note that we can declare the following in anonymous classes:
• Fields
• Extra methods (even if they do not implement any methods of the supertype)
• Local classes
• we cannot declare constructors in an anonymous class.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Note:
When we compile a nested class, two different class files will
be created with names
Outerclass.class
Outerclass$Nestedclass.class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Questions

Más contenido relacionado

La actualidad más candente

Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Edureka!
 
self_refrential_structures.pptx
self_refrential_structures.pptxself_refrential_structures.pptx
self_refrential_structures.pptxAshishNayyar12
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List Hitesh-Java
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in javaJayasankarPR2
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and OperatorsMarwa Ali Eissa
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword pptVinod Kumar
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesMicheal Ogundero
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanKavita Ganesan
 

La actualidad más candente (20)

Inheritance
InheritanceInheritance
Inheritance
 
Collections in-csharp
Collections in-csharpCollections in-csharp
Collections in-csharp
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
 
self_refrential_structures.pptx
self_refrential_structures.pptxself_refrential_structures.pptx
self_refrential_structures.pptx
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 

Destacado (17)

Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Questions for Class I & II
Questions for Class I & IIQuestions for Class I & II
Questions for Class I & II
 
Jun 2012(1)
Jun 2012(1)Jun 2012(1)
Jun 2012(1)
 
Networking
NetworkingNetworking
Networking
 
Sms several papers
Sms several papersSms several papers
Sms several papers
 
Swing api
Swing apiSwing api
Swing api
 
Internationalization
InternationalizationInternationalization
Internationalization
 
Basic IO
Basic IOBasic IO
Basic IO
 
Distributed Programming (RMI)
Distributed Programming (RMI)Distributed Programming (RMI)
Distributed Programming (RMI)
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Servlets
ServletsServlets
Servlets
 
Jdbc
JdbcJdbc
Jdbc
 
Packages
PackagesPackages
Packages
 
Event handling
Event handlingEvent handling
Event handling
 
List classes
List classesList classes
List classes
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Generics
GenericsGenerics
Generics
 

Similar a Java Programming Objects and Classes Guide

Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructorsRavi_Kant_Sahu
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and MethodsNilesh Dalvi
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classteach4uin
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsHelen SagayaRaj
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Mahmoud Alfarra
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptRithwikRanjan
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introductionteach4uin
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.pptMorgan309846
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 

Similar a Java Programming Objects and Classes Guide (20)

Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Inheritance
InheritanceInheritance
Inheritance
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Lecture 2 classes i
Lecture 2 classes iLecture 2 classes i
Lecture 2 classes i
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.ppt
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.ppt
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 

Más de Ravi_Kant_Sahu

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaRavi_Kant_Sahu
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi_Kant_Sahu
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java Ravi_Kant_Sahu
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesRavi_Kant_Sahu
 

Más de Ravi_Kant_Sahu (11)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
Event handling
Event handlingEvent handling
Event handling
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Packages
PackagesPackages
Packages
 
Array
ArrayArray
Array
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 

Último

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Último (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

Java Programming Objects and Classes Guide

  • 1. Programming in Java Lecture 5: Objects and Classes By Ravi Kant Sahu Asst. Professor, LPU
  • 2. Contents • Class • Object • Defining and adding variables • Nested Classes • Abstract Class Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. OBJECTS AND CLASS Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. 4 Class • A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. What is a class? • A class can be defined as a template/ blue print that describe the behaviors/states that object of its type support. • A class is the blueprint from which individual objects are created. • A class defines a new data type which can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. Classes and Objects • A Java program consists of one or more classes. • A class is an abstract description of objects. • Here is an example class: class Dog { ...description of a dog goes here... } • Here are some objects of that class: Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. 7 More Objects • Here is another example of a class: – class Window { ... } • Here are some examples of Windows: Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. • The data, or variables, defined within a class are called instance variables because each instance of the class (that is, each object of the class) contains its own copy of these variables. • The code is contained within methods. • The methods and variables defined within a class are called members of the class. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. Defining Classes  The basic syntax for a class definition:  Bare bone class – no fields, no methods public class Circle { // my circle class } class ClassName { [fields declaration] [methods declaration] } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. Adding Fields: Class Circle with fields • Add fields • The fields (data) are also called the instance variables. public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. Circle Class • aCircle, bCircle simply refers to a Circle object, It is not an object itself. aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null null Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Creating objects of a class • An object is an instance of the class which has well- defined attributes and behaviors. • Objects are created dynamically using the new keyword. • aCircle and bCircle refer to Circle objects. bCircle = new Circle();aCircle = new Circle(); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle Before Assignment Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. Adding Methods • A class with only data fields has no life. Objects created by such a class cannot respond to any messages. • Methods are declared inside the body of the class but immediately after the declaration of data fields. • The general form of a method declaration is: type MethodName (parameter-list) { Method-body; } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. Adding Methods to Class Circle public class Circle { public double x, y; // centre of the circle public double r; // radius of circle //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; } } Method Body Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. Automatic garbage collection • The object does not have a reference and cannot be used in future. • The object becomes a candidate for automatic garbage collection. • Java automatically collects garbage periodically and releases the memory used to be used in the future. Q Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. finalize() • The finalize() method is declared in the java.lang.Object class. • Before an object is garbage collected, the runtime system calls its finalize() method. • The intent is for finalize() to release system resources such as open files or open sockets before getting collected. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 19. Accessing Object/Circle Data Circle aCircle = new Circle(); aCircle.x = 2.0 // initialize center and radius aCircle.y = 2.0 aCircle.r = 1.0 ObjectName.VariableName ObjectName.MethodName(parameter-list) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. Executing Methods in Object/Circle • Using Object Methods: Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area(); sent ‘message’ to aCircle Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. Nested Class • The Java programming language allows us to define a class within another class. Such a class is called a nested class. Example: class OuterClass { ... class NestedClass { ... } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 22. Types of Nested Classes • A nested class is a member of its enclosing class. • Nested classes are divided into two categories: – static – non-static • Nested classes that are declared static are simply called static nested classes. • Non-static nested classes are called inner classes. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. Why Use Nested Classes? • Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. • Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world. • More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 24. Static Nested Classes • A static nested class is associated with its outer class similar to class methods and variables. • A static nested class cannot refer directly to instance variables or methods defined in its enclosing class. • It can use them only through an object reference. • Static nested classes are accessed using the enclosing class name: OuterClass.StaticNestedClass • For example, to create an object for the static nested class, use this syntax: OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 25. Inner Classes • An inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. • Because an inner class is associated with an instance, it cannot define any static members itself. • Objects that are instances of an inner class exist within an instance of the outer class. • Consider the following classes: class OuterClass { ... class InnerClass { ... } }
  • 26. • An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. • To instantiate an inner class, we must first instantiate the outer class. Then, create the inner object within the outer object. • Syntax: OuterClass.InnerClass innerObject = outerObject.new InnerClass(); • Additionally, there are two special kinds of inner classes: – local classes and – anonymous classes (also called anonymous inner classes). Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 27. Local Classes • Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. • For example, we can define a local class in a method body, a for loop, or an if clause. • A local class has access to the members of its enclosing class. • A local class has access to local variables. However, a local class can only access local variables that are declared final. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 28. Anonymous Classes • Anonymous classes enable us to declare and instantiate a class at the same time. • They are like local classes except that they do not have a name. • The anonymous class expression consists of the following: 1. The new operator 2. The name of an interface to implement or a class to extend. 3. Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. 4. A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 29.  Anonymous classes have the same access to local variables of the enclosing scope as local classes: • An anonymous class has access to the members of its enclosing class. • An anonymous class cannot access local variables in its enclosing scope that are not declared as final.  Anonymous classes also have the same restrictions as local classes with respect to their members: • We cannot declare static initializers or member interfaces in an anonymous class. • An anonymous class can have static members provided that they are constant variables.  Note that we can declare the following in anonymous classes: • Fields • Extra methods (even if they do not implement any methods of the supertype) • Local classes • we cannot declare constructors in an anonymous class. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 30. Note: When we compile a nested class, two different class files will be created with names Outerclass.class Outerclass$Nestedclass.class Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)