SlideShare a Scribd company logo
Basic Java Programming
Sasidhara Marrapu
 Classes
 Properties and Methods
 Constructors
 Objects
 Java Access Modifiers
 Lab Problem
 Home Work
Java Access Modifiers
3
Classes and Objects
• A Java program consists of one or more classes
• A class is an abstract description/Blueprint of objects
• Every class is Subclass (child) of Object
• A class is used as a template or pattern to create new
objects (instances of the class)
4
Classes and Objects
• A class defines the characteristics that are common to a
group of similar objects
• defines the data/variables/properties that hold the object’s state
• defines the methods/behaviors that describe the object’s behaviors
Lamp is an object
• It can be in on or off state.
• You can turn on and turn off lamp (behavior).
Bicycle is an object
• It has current gear, two wheels, number of gear etc. states.
• It has braking, accelerating, changing gears etc. behavior.
5
Class Structure
• A program consists of one or more
classes
• Typically, each class is in a separate
.java file
Program
File File
File
File
Class
Variables
Constructors
Methods
Variables
Variables
Statements
Statements
6
Classes and Objects
public class Person {
// data or variables that hold object state
String name;
int age;
String gender;
// Behaviors
public void canRead() {
}
public void canWrite() {
}
7
Classes and Objects
• A class is used as a template or pattern to create new
objects (instances of the class)
• Every object is an instance of a class
• each object has its own name (identity)
• each object has its own variables (state)
• all instances of the class share the methods defined by the
class (behaviors)
Methods
• A method in a class performs specific behaviors by using
statements
• A statement causes the object to do something
• Class can have many methods
• Two types of methods
• Class level – static methods – can be accessible directly
• Instance/Member/Object level methods – can only be
accessible after creating object/instance.
Methods
Constructor
• A method in a class that initialize an instance of an
object before it's used.
•The same name as the class and have no return type
• Multiple Constructors: the same name but a different
number of arguments or different typed arguments
• Java Provides default constructors
• The special variable, this, can be used inside a method
to refer to the object instance.
11
Constructors
public class Person {
// data or variables that hold
object state
String name;
int age;
String gender;
Person(){
}
Person(String personName){
this.name = personName;
}
12
Creating objects of a class
• Objects are created dynamically using the new keyword.
• student and employee refer to Person objects
employee = new Person(“John”) ;
student = new Person() ;
13
Creating objects of a class
student = new Person();
employee = new Person(“John”) ;
employee = student;
P
student
Q
employee
Before Assignment
P
student
Q
employee
After Assignment
14
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
15
Accessing Object Data
Person person = new Person();
person.age = 17
person.name = “Mary”
person.gender = “Female”
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
16
Executing Methods in Object
• Using Object Methods:
sent ‘message’ to aCircle
Person person = new Person();
person.canRead()
Passing Information into a Method
• Argument types
•primitive and reference data type: Yes
•method: No
• Argument Names
•Can have the same name as one of the class's member
variable
• Use this to refer to the member variable
• Primitive arguments are passed by value.
• Reference arguments are passed by reference.
Using
package,
import, and
CLASSPATH
Java
Access
Specifiers
class DoFoo {
public static void main(String[] args) {
Foo f = new Foo();
f.fun();
f.i = f.i + 1;
}
}
public class Foo {
// . . .
int i;
// . . .
fun(){
i = i + 1;
}
}
new
Foo.class
javac
Access
to
What?
Access
to
Everything
Java Access Specifiers
• public
• protected
• "friendly“/default
• private
Packages
• Assume the files are class
files
• Assume all the files in the
same folder are declared to
be in the appropriate
package relative to the
CLASSPATH
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
Can this
reference
access this
member?
Where Can We Access
"i" in Foo From?
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
Can this
reference
access this
member?
Where Can We Access
"i" in Foo From?
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
Y
Y
Y
Y
Y
Y
Y
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
public int i;
public class Foo {
// . . .
int i;
// . . .
fun(){
i++;
}
}
N
Y
Y
N
N
N
N
public class Foo {
// . . .
int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
int i;
public class Foo {
// . . .
private int i;
// . . .
fun(){
i++;
}
}
N
N
N
N
N
N
N
public class Foo {
// . . .
private int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
private int i;
Inheritance &
Access
obj
Inheritance &
Access
obj
Class Bar extends Foo {
// Stuff
}
Class Foo . . . {
// Stuff
}
Inheritance &
Access
public class Foo ext. . . {
// . . .
protected int i;
// . . .
fun(){
i++;
}
}
obj
Foo f = new Foo();
f.i++;
Inheritance &
Access
public class Foo ext. . . {
// . . .
protected int i;
// . . .
fun(){
i++;
}
}
obj
N
Y
Y
Y
N
N
N
public class Foo ext. . . {
// . . .
protected int i;
// . . .
fun(){
i++;
}
}
obj
Foo f = new Foo();
f.i++;
Inheritance &
Access
The Set View
The Member Accessed
from anywhere
from same package
from same class
public "friendly" private
public "friendly" private
The Member Accessed
from anywhere
from same package
from a child class
from same class
protected
public "friendly" private
You May Wish to Order Declarations
• public
• protected
• "friendly"
• private
Summary
• Access is based on library structure (packages) and inheritance.
• The less you expose the more flexibility is preserved
Home Work

More Related Content

What's hot

Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspection
adil raja
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
Syed Afaq Shah MACS CP
 
Aman kingrubyoo pnew
Aman kingrubyoo pnew Aman kingrubyoo pnew
Aman kingrubyoo pnew ThoughtWorks
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Introduction what is java
Introduction what is javaIntroduction what is java
Introduction what is java
sanjeeviniindia1186
 
Introducing classes
Introducing classesIntroducing classes
Introducing classes
Riaz Ahmed
 
Semantic Web - Ontology 101
Semantic Web - Ontology 101Semantic Web - Ontology 101
Semantic Web - Ontology 101
Luigi De Russis
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++Jeff TUYISHIME
 
OOP Basic Concepts
OOP Basic ConceptsOOP Basic Concepts
OOP Basic Concepts
AmeerHamza237
 
RIBBUN SOFTWARE
RIBBUN SOFTWARERIBBUN SOFTWARE
RIBBUN SOFTWARE
mosewoodward24
 
Java01
Java01Java01
Java01
Remon Hanna
 

What's hot (16)

Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspection
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 
Aman kingrubyoo pnew
Aman kingrubyoo pnew Aman kingrubyoo pnew
Aman kingrubyoo pnew
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Introduction what is java
Introduction what is javaIntroduction what is java
Introduction what is java
 
Introducing classes
Introducing classesIntroducing classes
Introducing classes
 
Semantic Web - Ontology 101
Semantic Web - Ontology 101Semantic Web - Ontology 101
Semantic Web - Ontology 101
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
OOP Basic Concepts
OOP Basic ConceptsOOP Basic Concepts
OOP Basic Concepts
 
Java intro
Java introJava intro
Java intro
 
RIBBUN SOFTWARE
RIBBUN SOFTWARERIBBUN SOFTWARE
RIBBUN SOFTWARE
 
Java01
Java01Java01
Java01
 
Java
JavaJava
Java
 

Similar to Pj01 x-classes and objects

UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
akila m
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Object concepts
Object conceptsObject concepts
Object concepts
Aashima Wadhwa
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
ayaankim007
 
3.Classes&Objects.pptx
3.Classes&Objects.pptx3.Classes&Objects.pptx
3.Classes&Objects.pptx
PRABHUSOLOMON1
 
Lecture 4 part 2.pdf
Lecture 4 part 2.pdfLecture 4 part 2.pdf
Lecture 4 part 2.pdf
SakhilejasonMsibi
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
mcollison
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part III
Hari Christian
 
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
NithyaN19
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
Abdii Rashid
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
Ferdin Joe John Joseph PhD
 

Similar to Pj01 x-classes and objects (20)

Lecture 4
Lecture 4Lecture 4
Lecture 4
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Object concepts
Object conceptsObject concepts
Object concepts
 
Object concepts
Object conceptsObject concepts
Object concepts
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
 
3.Classes&Objects.pptx
3.Classes&Objects.pptx3.Classes&Objects.pptx
3.Classes&Objects.pptx
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Lecture 4 part 2.pdf
Lecture 4 part 2.pdfLecture 4 part 2.pdf
Lecture 4 part 2.pdf
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part III
 
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
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 

Recently uploaded

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 

Recently uploaded (20)

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 

Pj01 x-classes and objects

  • 2.  Classes  Properties and Methods  Constructors  Objects  Java Access Modifiers  Lab Problem  Home Work Java Access Modifiers
  • 3. 3 Classes and Objects • A Java program consists of one or more classes • A class is an abstract description/Blueprint of objects • Every class is Subclass (child) of Object • A class is used as a template or pattern to create new objects (instances of the class)
  • 4. 4 Classes and Objects • A class defines the characteristics that are common to a group of similar objects • defines the data/variables/properties that hold the object’s state • defines the methods/behaviors that describe the object’s behaviors Lamp is an object • It can be in on or off state. • You can turn on and turn off lamp (behavior). Bicycle is an object • It has current gear, two wheels, number of gear etc. states. • It has braking, accelerating, changing gears etc. behavior.
  • 5. 5 Class Structure • A program consists of one or more classes • Typically, each class is in a separate .java file Program File File File File Class Variables Constructors Methods Variables Variables Statements Statements
  • 6. 6 Classes and Objects public class Person { // data or variables that hold object state String name; int age; String gender; // Behaviors public void canRead() { } public void canWrite() { }
  • 7. 7 Classes and Objects • A class is used as a template or pattern to create new objects (instances of the class) • Every object is an instance of a class • each object has its own name (identity) • each object has its own variables (state) • all instances of the class share the methods defined by the class (behaviors)
  • 8. Methods • A method in a class performs specific behaviors by using statements • A statement causes the object to do something • Class can have many methods • Two types of methods • Class level – static methods – can be accessible directly • Instance/Member/Object level methods – can only be accessible after creating object/instance.
  • 10. Constructor • A method in a class that initialize an instance of an object before it's used. •The same name as the class and have no return type • Multiple Constructors: the same name but a different number of arguments or different typed arguments • Java Provides default constructors • The special variable, this, can be used inside a method to refer to the object instance.
  • 11. 11 Constructors public class Person { // data or variables that hold object state String name; int age; String gender; Person(){ } Person(String personName){ this.name = personName; }
  • 12. 12 Creating objects of a class • Objects are created dynamically using the new keyword. • student and employee refer to Person objects employee = new Person(“John”) ; student = new Person() ;
  • 13. 13 Creating objects of a class student = new Person(); employee = new Person(“John”) ; employee = student; P student Q employee Before Assignment P student Q employee After Assignment
  • 14. 14 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
  • 15. 15 Accessing Object Data Person person = new Person(); person.age = 17 person.name = “Mary” person.gender = “Female” ObjectName.VariableName ObjectName.MethodName(parameter-list)
  • 16. 16 Executing Methods in Object • Using Object Methods: sent ‘message’ to aCircle Person person = new Person(); person.canRead()
  • 17. Passing Information into a Method • Argument types •primitive and reference data type: Yes •method: No • Argument Names •Can have the same name as one of the class's member variable • Use this to refer to the member variable • Primitive arguments are passed by value. • Reference arguments are passed by reference.
  • 19. class DoFoo { public static void main(String[] args) { Foo f = new Foo(); f.fun(); f.i = f.i + 1; } } public class Foo { // . . . int i; // . . . fun(){ i = i + 1; } } new Foo.class javac Access to What? Access to Everything
  • 20. Java Access Specifiers • public • protected • "friendly“/default • private
  • 21. Packages • Assume the files are class files • Assume all the files in the same folder are declared to be in the appropriate package relative to the CLASSPATH
  • 22. public class Foo { // . . . public int i; // . . . fun(){ i++; } } public class Foo { // . . . public int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; Can this reference access this member? Where Can We Access "i" in Foo From?
  • 23. public class Foo { // . . . public int i; // . . . fun(){ i++; } } public class Foo { // . . . public int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; Can this reference access this member? Where Can We Access "i" in Foo From?
  • 24. public class Foo { // . . . public int i; // . . . fun(){ i++; } } Y Y Y Y Y Y Y public class Foo { // . . . public int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; public int i;
  • 25. public class Foo { // . . . int i; // . . . fun(){ i++; } } N Y Y N N N N public class Foo { // . . . int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; int i;
  • 26. public class Foo { // . . . private int i; // . . . fun(){ i++; } } N N N N N N N public class Foo { // . . . private int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; private int i;
  • 29. obj Class Bar extends Foo { // Stuff } Class Foo . . . { // Stuff } Inheritance & Access
  • 30. public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; } } obj Foo f = new Foo(); f.i++; Inheritance & Access
  • 31. public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; } } obj N Y Y Y N N N public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; } } obj Foo f = new Foo(); f.i++; Inheritance & Access
  • 33. The Member Accessed from anywhere from same package from same class public "friendly" private
  • 34. public "friendly" private The Member Accessed from anywhere from same package from a child class from same class protected public "friendly" private
  • 35. You May Wish to Order Declarations • public • protected • "friendly" • private
  • 36. Summary • Access is based on library structure (packages) and inheritance. • The less you expose the more flexibility is preserved