SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Classes Revision
CST200 – Week 4: Midterm revision

Instructor: Andreea Molnar
Outline
• Classes
• Example
• Constructor
• Encapsulation
Classes
In object oriented programming classes
represent objects with different
characteristics (attributes, data) and
functionality (operations, methods).
Example
Person
Characteristics: name, social security
number
Functionality: sleeps, walks
Example
Person
Attributes: name, social security number
Operations: sleeps, walks
Example
Person
Data: name, social security number
Methods: sleeps, walks
Example
Person
Data: define the state of the object
Methods: define the behavior of the object
Example
public class Person {

private String name;

Data declaration

private String socialSecurityNo;

public void sleeps() {
}
public void walks() {
}
}

Method declaration
Example
public class Person {
private String name;

private String socialSecurityNo;

Instance variables –
variables created at the
class level

Each instance of a class
(object) has its own instance
variables/data space
public void sleeps() {
}
public void walks() {
}
}

Each instance of a class
(object) share the method
definitions
Constructor
•
•
•

Creates (and initializes) an object

Similar to a method that has:

•
•

the same name as the class

no return type

Each class has a default constructor that
accepts no parameters (this is generated
only if no explicit constructor is provided).
Constructor
public class Test {
public static void main(String[] args) {
Person p = new Person();
}
}

The default
constructor is called
Constructor
public class Person {
private String name;
private String socialSecurityNo;
public Person(String name, String socialSecurityNo) {
this.name = name;

Constructor

this.socialSecurityNo = socialSecurityNo;
}
public void sleeps() {
}
public void walks() {
}
}

this is a reference to the
current object
Constructor
public class Test {
public static void main(String[] args) {
Person p = new Person("Mary", "078-05-1120");
}
}
Constructor
public class Test {
public static void main(String[] args) {
Person p1 = new Person("Mary", "078-05-1120");

Person p2 = new Person(“John", "078-05-1121");
}
}

name

name

Mary

John

socialSecurityNo

socialSecurityNo

078-05-1120

078-05-1121

Each instance of a
class (object) has its
own instance
variables/data space.
Encapsulation
•
•

Hides the data and implementation details
of an object
Protects the data integrity by making it
difficult to have unauthorized access
Encapsulation
•

Uses visibility modifiers (e.g. private) to
deny access
private String name;
private String socialSecurityNo;

name and
socialSecurityNo can
be referenced only
within Person class

public class Test {
public static void main(String[] args) {
Person p = new Person("Mary", "078-05-1120");
p.name = "Alice";
}

}

error
Encapsulation

Allows access to instance variables through
methods: accessor method (getter) and
mutator method (setter)
Encapsulation
Accessor method (getter) – returns the
variable value.
private String name;
public String getName() {
return name;
}
Encapsulation
Accessor method (getter) – returns the
variable value.
public class Test {
public static void main(String[] args) {
Person p = new Person("Mary", "078-05-1120");
System.out.println(p.getName());
}
}

Will print Mary

p

name
Mary

socialSecurityNo
078-05-1120
Encapsulation
Mutator method (setter) – changes the
variable value
private String name;
public void setName(String name) {
this.name = name;
}
Encapsulation
Mutator method (setter) – changes the variable value
public class Test {
public static void main(String[] args) {
Person p = new Person("Mary", "078-05-1120");
System.out.println(p.getName()); //will print Mary
p
p.setName("Alice");
System.out.println(p.getName()); //will print Alice
}
}
name
Mary Alice

socialSecurityNo
078-05-1120
Summary
• Initialize instance variables in the
constructor

• Make the instance variables private
unless there is a good reason to do
otherwise

• Allow access to instance variables
through setter and getter methods

Weitere ähnliche Inhalte

Was ist angesagt?

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
Syed Faizan Hassan
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
Tech_MX
 

Was ist angesagt? (20)

Adbms 15 object data management group
Adbms 15 object data management groupAdbms 15 object data management group
Adbms 15 object data management group
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
C++ classes
C++ classesC++ classes
C++ classes
 
Chap01
Chap01Chap01
Chap01
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Jscript part2
Jscript part2Jscript part2
Jscript part2
 
class c++
class c++class c++
class c++
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
 
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
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Session 09 - OOPS
Session 09 - OOPSSession 09 - OOPS
Session 09 - OOPS
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
 
Write First C++ class
Write First C++ classWrite First C++ class
Write First C++ class
 

Ähnlich wie Classes revision

Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
zand3rs
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
Svetlin Nakov
 

Ähnlich wie Classes revision (20)

Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in java
 
Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptx
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Pj01 x-classes and objects
Pj01 x-classes and objectsPj01 x-classes and objects
Pj01 x-classes and objects
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
javaClasses.pptx
javaClasses.pptxjavaClasses.pptx
javaClasses.pptx
 
Lecture 4 part 2.pdf
Lecture 4 part 2.pdfLecture 4 part 2.pdf
Lecture 4 part 2.pdf
 
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
 
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
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Chapter iii(oop)
Chapter iii(oop)Chapter iii(oop)
Chapter iii(oop)
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 

Mehr von ASU Online

Midterm common mistakes
Midterm common mistakesMidterm common mistakes
Midterm common mistakes
ASU Online
 
Lists, queues and stacks 1
Lists, queues and stacks 1Lists, queues and stacks 1
Lists, queues and stacks 1
ASU Online
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
ASU Online
 
For loop java 2
For loop java 2For loop java 2
For loop java 2
ASU Online
 
Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
ASU Online
 
Common errors v2
Common errors v2Common errors v2
Common errors v2
ASU Online
 
Common missunderestandings
Common missunderestandingsCommon missunderestandings
Common missunderestandings
ASU Online
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
ASU Online
 
Lecture 14 tourism in europe
Lecture 14   tourism in europeLecture 14   tourism in europe
Lecture 14 tourism in europe
ASU Online
 
Lecture 13 tourism in the south pacific
Lecture 13   tourism in the south pacificLecture 13   tourism in the south pacific
Lecture 13 tourism in the south pacific
ASU Online
 
Lecture 12 tourism in australia and new zealand
Lecture 12   tourism in australia and new zealandLecture 12   tourism in australia and new zealand
Lecture 12 tourism in australia and new zealand
ASU Online
 
Lecture 11 tourism in east asia
Lecture 11   tourism in east asiaLecture 11   tourism in east asia
Lecture 11 tourism in east asia
ASU Online
 
Lecture 9 tourism in south asia
Lecture 9   tourism in south asiaLecture 9   tourism in south asia
Lecture 9 tourism in south asia
ASU Online
 
Lecture 9 tourism in south asia-1
Lecture 9   tourism in south asia-1Lecture 9   tourism in south asia-1
Lecture 9 tourism in south asia-1
ASU Online
 
Lecture 8 tourism in central asia
Lecture 8   tourism in central asiaLecture 8   tourism in central asia
Lecture 8 tourism in central asia
ASU Online
 
Lecture 7 tourism in the middle east and north africa
Lecture 7   tourism in the middle east and north africaLecture 7   tourism in the middle east and north africa
Lecture 7 tourism in the middle east and north africa
ASU Online
 
Lecture 6 tourism in sub-saharan africa
Lecture 6   tourism in sub-saharan africaLecture 6   tourism in sub-saharan africa
Lecture 6 tourism in sub-saharan africa
ASU Online
 
Lecture 5 tourism in latin america
Lecture 5   tourism in latin americaLecture 5   tourism in latin america
Lecture 5 tourism in latin america
ASU Online
 
Lecture 4 tourism in the caribbean
Lecture 4   tourism in the caribbeanLecture 4   tourism in the caribbean
Lecture 4 tourism in the caribbean
ASU Online
 
Lecture 3 political context of international tourism
Lecture 3   political context of international tourismLecture 3   political context of international tourism
Lecture 3 political context of international tourism
ASU Online
 

Mehr von ASU Online (20)

Midterm common mistakes
Midterm common mistakesMidterm common mistakes
Midterm common mistakes
 
Lists, queues and stacks 1
Lists, queues and stacks 1Lists, queues and stacks 1
Lists, queues and stacks 1
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
 
For loop java 2
For loop java 2For loop java 2
For loop java 2
 
Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
 
Common errors v2
Common errors v2Common errors v2
Common errors v2
 
Common missunderestandings
Common missunderestandingsCommon missunderestandings
Common missunderestandings
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Lecture 14 tourism in europe
Lecture 14   tourism in europeLecture 14   tourism in europe
Lecture 14 tourism in europe
 
Lecture 13 tourism in the south pacific
Lecture 13   tourism in the south pacificLecture 13   tourism in the south pacific
Lecture 13 tourism in the south pacific
 
Lecture 12 tourism in australia and new zealand
Lecture 12   tourism in australia and new zealandLecture 12   tourism in australia and new zealand
Lecture 12 tourism in australia and new zealand
 
Lecture 11 tourism in east asia
Lecture 11   tourism in east asiaLecture 11   tourism in east asia
Lecture 11 tourism in east asia
 
Lecture 9 tourism in south asia
Lecture 9   tourism in south asiaLecture 9   tourism in south asia
Lecture 9 tourism in south asia
 
Lecture 9 tourism in south asia-1
Lecture 9   tourism in south asia-1Lecture 9   tourism in south asia-1
Lecture 9 tourism in south asia-1
 
Lecture 8 tourism in central asia
Lecture 8   tourism in central asiaLecture 8   tourism in central asia
Lecture 8 tourism in central asia
 
Lecture 7 tourism in the middle east and north africa
Lecture 7   tourism in the middle east and north africaLecture 7   tourism in the middle east and north africa
Lecture 7 tourism in the middle east and north africa
 
Lecture 6 tourism in sub-saharan africa
Lecture 6   tourism in sub-saharan africaLecture 6   tourism in sub-saharan africa
Lecture 6 tourism in sub-saharan africa
 
Lecture 5 tourism in latin america
Lecture 5   tourism in latin americaLecture 5   tourism in latin america
Lecture 5 tourism in latin america
 
Lecture 4 tourism in the caribbean
Lecture 4   tourism in the caribbeanLecture 4   tourism in the caribbean
Lecture 4 tourism in the caribbean
 
Lecture 3 political context of international tourism
Lecture 3   political context of international tourismLecture 3   political context of international tourism
Lecture 3 political context of international tourism
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Classes revision

  • 1. Classes Revision CST200 – Week 4: Midterm revision Instructor: Andreea Molnar
  • 2. Outline • Classes • Example • Constructor • Encapsulation
  • 3. Classes In object oriented programming classes represent objects with different characteristics (attributes, data) and functionality (operations, methods).
  • 4. Example Person Characteristics: name, social security number Functionality: sleeps, walks
  • 5. Example Person Attributes: name, social security number Operations: sleeps, walks
  • 6. Example Person Data: name, social security number Methods: sleeps, walks
  • 7. Example Person Data: define the state of the object Methods: define the behavior of the object
  • 8. Example public class Person { private String name; Data declaration private String socialSecurityNo; public void sleeps() { } public void walks() { } } Method declaration
  • 9. Example public class Person { private String name; private String socialSecurityNo; Instance variables – variables created at the class level Each instance of a class (object) has its own instance variables/data space public void sleeps() { } public void walks() { } } Each instance of a class (object) share the method definitions
  • 10. Constructor • • • Creates (and initializes) an object Similar to a method that has: • • the same name as the class no return type Each class has a default constructor that accepts no parameters (this is generated only if no explicit constructor is provided).
  • 11. Constructor public class Test { public static void main(String[] args) { Person p = new Person(); } } The default constructor is called
  • 12. Constructor public class Person { private String name; private String socialSecurityNo; public Person(String name, String socialSecurityNo) { this.name = name; Constructor this.socialSecurityNo = socialSecurityNo; } public void sleeps() { } public void walks() { } } this is a reference to the current object
  • 13. Constructor public class Test { public static void main(String[] args) { Person p = new Person("Mary", "078-05-1120"); } }
  • 14. Constructor public class Test { public static void main(String[] args) { Person p1 = new Person("Mary", "078-05-1120"); Person p2 = new Person(“John", "078-05-1121"); } } name name Mary John socialSecurityNo socialSecurityNo 078-05-1120 078-05-1121 Each instance of a class (object) has its own instance variables/data space.
  • 15. Encapsulation • • Hides the data and implementation details of an object Protects the data integrity by making it difficult to have unauthorized access
  • 16. Encapsulation • Uses visibility modifiers (e.g. private) to deny access private String name; private String socialSecurityNo; name and socialSecurityNo can be referenced only within Person class public class Test { public static void main(String[] args) { Person p = new Person("Mary", "078-05-1120"); p.name = "Alice"; } } error
  • 17. Encapsulation Allows access to instance variables through methods: accessor method (getter) and mutator method (setter)
  • 18. Encapsulation Accessor method (getter) – returns the variable value. private String name; public String getName() { return name; }
  • 19. Encapsulation Accessor method (getter) – returns the variable value. public class Test { public static void main(String[] args) { Person p = new Person("Mary", "078-05-1120"); System.out.println(p.getName()); } } Will print Mary p name Mary socialSecurityNo 078-05-1120
  • 20. Encapsulation Mutator method (setter) – changes the variable value private String name; public void setName(String name) { this.name = name; }
  • 21. Encapsulation Mutator method (setter) – changes the variable value public class Test { public static void main(String[] args) { Person p = new Person("Mary", "078-05-1120"); System.out.println(p.getName()); //will print Mary p p.setName("Alice"); System.out.println(p.getName()); //will print Alice } } name Mary Alice socialSecurityNo 078-05-1120
  • 22. Summary • Initialize instance variables in the constructor • Make the instance variables private unless there is a good reason to do otherwise • Allow access to instance variables through setter and getter methods