SlideShare a Scribd company logo
Programming in Java
5-day workshop
OOP Classes
Matt Collison
JP Morgan Chase 2021
PiJ2.2: OOP Classes
Four principles of OOP:
1.Encapsulation – defining a class
•Hiding all but the required components
•Providing modularity
2.Abstraction – modifying the objects
3.Inheritance – extending classes
4.Polymorphism – implementing interfaces
Classes and objects
• Classes are made up of:
• Attributes – fields to describe the class
• Constructors – constrain how you can create the object
• Methods – functions to describe it’s behaviour
Defining your first class
public class Rectangle{
// Attributes
public double width;
public double height;
final static int sides =4;
// Constructors
public Rectangle(double w, double h){ ... }
// Methods
public double getArea() { ... }
}
Attributes/fields
• For the Rectangle class, each rectangle object has different values for
the following attributes.
double width;
double height;
double originX=0.0; // need to be added
double originY=0.0;
• How about the number of sides?
final int NUMBER OF SIDES = 4;
• This attribute has the same value for all rectangles.
Attributes
Attributes could be divided into:
1. Instance attributes: belongs to objects.
• Individual value for each object even if the values are the same.
2. static attributes: belongs to the class.
• common property of all objects (that is not unique for each object)
• Through static keywords: e.g. static final int NUMBER OF SIDES = 4;
Methods
• Methods are well-defined sets of instructions to perform a specific task.
public void move(double dx, double dy) {
originX += dx;
originY += dy;
}
public double getArea() {
return width*height;
}
Input data: known as method parameters or arguments
Output data: through return statement. If the return type is void, no need to
include a return statement. .
Methods
• A method can access:
• all of that class’s attributes (static and instance)
• local variables
• the parameters passed to it
• variables declared within the method itself
• Scope of local variables:
• Exists only while the method is being invoked
Overloading methods
• Java supports overloading methods:
• i.e., two/more methods share the same name, but with different method
arguments.
public void zoom(double factor){...}
public void zoom(double factorX, double factorY){...}
Static methods
Methods could be divided into:
• Instance methods: belongs to objects.
• Static methods: belongs to the class.
• Through static keywords.
• The main() method is a static method.
Q: Should the following methods instance or static methods?
1. void zoom(double factor) //instance method
2. double getArea() //instance method
3. boolean isOverlapped(Rectangle r1, Rectangle r2) //static method
4. boolean isOverlappedWith(Rectangle r2) //instance method
Constructors
A Java constructor is invoked at the time of object creation, initialisation. It
initializes data for the object within some rules:
• Constructor name must be same as its class name.
• Constructor must have no explicit return type.
public class Rectangle{
public Rectangle(){
//constructor logic
}
…
}
Default constructors
• Constructors can be overloaded.
• A class can have no constructor.
• In this case, the compiler implicitly create a default no-arg constructor as below.
public class Point {
public double x;
public double y;
}
public class Point {
public double x;
public double y;
public Point(){ }
}
Rectangle.java (file available on gh-pages)
// A Rectangle class
public class Rectangle {
// 4 instance attributes
public double width;
public double height;
public double originX = 0.0;
public double originY = 0.0;
// 1 static attributes
public static final int NUMBER_OF_SIDES = 4;
// method: move the rectangle
public void move(double dx, double dy) {
originX += dx;
originY += dy;
Learning resources
The workshop homepage
https://mcollison.github.io/JPMC-java-intro-2021/
The course materials
https://mcollison.github.io/java-programming-foundations/
• Session worksheets – updated each week
Additional resources
• Think Java: How to think like a computer scientist
• Allen B Downey (O’Reilly Press)
• Available under Creative Commons license
• https://greenteapress.com/wp/think-java-2e/
• Oracle central Java Documentation –
https://docs.oracle.com/javase/8/docs/api/
• Other sources:
• W3Schools Java - https://www.w3schools.com/java/
• stack overflow - https://stackoverflow.com/
• Coding bat - https://codingbat.com/java

More Related Content

What's hot (20)

PPTX
Classes in c++ (OOP Presentation)
Majid Saeed
 
PPTX
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
PPT
Lect 1-class and object
Fajar Baskoro
 
PPTX
Session 15 - Collections - Array List
PawanMM
 
PDF
Class and Objects in Java
Spotle.ai
 
PPT
Core Java
Khasim Saheb
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PDF
ITFT-Classes and object in java
Atul Sehdev
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PPSX
Collections - Sorting, Comparing Basics
Hitesh-Java
 
PPT
03class
Waheed Warraich
 
PPTX
Classes and objects
rajveer_Pannu
 
PPT
Best Core Java Training In Bangalore
rajkamaltibacademy
 
PPTX
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
PPT
C++ Inheritance
Jussi Pohjolainen
 
PPT
Class and object in c++
NainaKhan28
 
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
PPT
Class and object in C++
rprajat007
 
PPTX
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
PPTX
Class or Object
Rahul Bathri
 
Classes in c++ (OOP Presentation)
Majid Saeed
 
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
Lect 1-class and object
Fajar Baskoro
 
Session 15 - Collections - Array List
PawanMM
 
Class and Objects in Java
Spotle.ai
 
Core Java
Khasim Saheb
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
ITFT-Classes and object in java
Atul Sehdev
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
Collections - Sorting, Comparing Basics
Hitesh-Java
 
Classes and objects
rajveer_Pannu
 
Best Core Java Training In Bangalore
rajkamaltibacademy
 
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
C++ Inheritance
Jussi Pohjolainen
 
Class and object in c++
NainaKhan28
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
Class and object in C++
rprajat007
 
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
Class or Object
Rahul Bathri
 

Similar to Pi j2.2 classes (20)

PPTX
Chapter 6.6
sotlsoc
 
PPT
Class & Object - User Defined Method
PRN USM
 
PPTX
Pi j2.3 objects
mcollison
 
PPTX
Classes,object and methods java
Padma Kannan
 
PPT
Class & Object - Intro
PRN USM
 
PPT
4. Classes and Methods
Nilesh Dalvi
 
PDF
Introduction to java and oop
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
PPT
Defining classes-and-objects-1.0
BG Java EE Course
 
PPTX
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
PDF
OOPs & Inheritance Notes
Shalabh Chaudhary
 
DOCX
Lecture11.docx
ASADAHMAD811380
 
PPTX
Classes,object and methods jav
Padma Kannan
 
PDF
Java Basics - Part2
Vani Kandhasamy
 
PPTX
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
berihun18
 
PDF
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
PDF
Class notes(week 3) on class objects and methods
Kuntal Bhowmick
 
PPT
Core Java unit no. 1 object and class ppt
Mochi263119
 
DOCX
Class notes(week 3) on class objects and methods
Kuntal Bhowmick
 
PPT
1 1 5 Clases
UVM
 
Chapter 6.6
sotlsoc
 
Class & Object - User Defined Method
PRN USM
 
Pi j2.3 objects
mcollison
 
Classes,object and methods java
Padma Kannan
 
Class & Object - Intro
PRN USM
 
4. Classes and Methods
Nilesh Dalvi
 
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
Defining classes-and-objects-1.0
BG Java EE Course
 
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
OOPs & Inheritance Notes
Shalabh Chaudhary
 
Lecture11.docx
ASADAHMAD811380
 
Classes,object and methods jav
Padma Kannan
 
Java Basics - Part2
Vani Kandhasamy
 
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
berihun18
 
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Class notes(week 3) on class objects and methods
Kuntal Bhowmick
 
Core Java unit no. 1 object and class ppt
Mochi263119
 
Class notes(week 3) on class objects and methods
Kuntal Bhowmick
 
1 1 5 Clases
UVM
 
Ad

More from mcollison (10)

PPTX
Pi j4.2 software-reliability
mcollison
 
PPTX
Pi j4.1 packages
mcollison
 
PPTX
Pi j3.1 inheritance
mcollison
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPTX
Pi j3.4 data-structures
mcollison
 
PPTX
Pi j1.0 workshop-introduction
mcollison
 
PPTX
Pi j1.4 loops
mcollison
 
PPTX
Pi j1.3 operators
mcollison
 
PPTX
Pi j1.2 variable-assignment
mcollison
 
PPTX
Pi j1.1 what-is-java
mcollison
 
Pi j4.2 software-reliability
mcollison
 
Pi j4.1 packages
mcollison
 
Pi j3.1 inheritance
mcollison
 
Pi j3.2 polymorphism
mcollison
 
Pi j3.4 data-structures
mcollison
 
Pi j1.0 workshop-introduction
mcollison
 
Pi j1.4 loops
mcollison
 
Pi j1.3 operators
mcollison
 
Pi j1.2 variable-assignment
mcollison
 
Pi j1.1 what-is-java
mcollison
 
Ad

Recently uploaded (20)

PPTX
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 

Pi j2.2 classes

  • 1. Programming in Java 5-day workshop OOP Classes Matt Collison JP Morgan Chase 2021 PiJ2.2: OOP Classes
  • 2. Four principles of OOP: 1.Encapsulation – defining a class •Hiding all but the required components •Providing modularity 2.Abstraction – modifying the objects 3.Inheritance – extending classes 4.Polymorphism – implementing interfaces
  • 3. Classes and objects • Classes are made up of: • Attributes – fields to describe the class • Constructors – constrain how you can create the object • Methods – functions to describe it’s behaviour
  • 4. Defining your first class public class Rectangle{ // Attributes public double width; public double height; final static int sides =4; // Constructors public Rectangle(double w, double h){ ... } // Methods public double getArea() { ... } }
  • 5. Attributes/fields • For the Rectangle class, each rectangle object has different values for the following attributes. double width; double height; double originX=0.0; // need to be added double originY=0.0; • How about the number of sides? final int NUMBER OF SIDES = 4; • This attribute has the same value for all rectangles.
  • 6. Attributes Attributes could be divided into: 1. Instance attributes: belongs to objects. • Individual value for each object even if the values are the same. 2. static attributes: belongs to the class. • common property of all objects (that is not unique for each object) • Through static keywords: e.g. static final int NUMBER OF SIDES = 4;
  • 7. Methods • Methods are well-defined sets of instructions to perform a specific task. public void move(double dx, double dy) { originX += dx; originY += dy; } public double getArea() { return width*height; } Input data: known as method parameters or arguments Output data: through return statement. If the return type is void, no need to include a return statement. .
  • 8. Methods • A method can access: • all of that class’s attributes (static and instance) • local variables • the parameters passed to it • variables declared within the method itself • Scope of local variables: • Exists only while the method is being invoked
  • 9. Overloading methods • Java supports overloading methods: • i.e., two/more methods share the same name, but with different method arguments. public void zoom(double factor){...} public void zoom(double factorX, double factorY){...}
  • 10. Static methods Methods could be divided into: • Instance methods: belongs to objects. • Static methods: belongs to the class. • Through static keywords. • The main() method is a static method. Q: Should the following methods instance or static methods? 1. void zoom(double factor) //instance method 2. double getArea() //instance method 3. boolean isOverlapped(Rectangle r1, Rectangle r2) //static method 4. boolean isOverlappedWith(Rectangle r2) //instance method
  • 11. Constructors A Java constructor is invoked at the time of object creation, initialisation. It initializes data for the object within some rules: • Constructor name must be same as its class name. • Constructor must have no explicit return type. public class Rectangle{ public Rectangle(){ //constructor logic } … }
  • 12. Default constructors • Constructors can be overloaded. • A class can have no constructor. • In this case, the compiler implicitly create a default no-arg constructor as below. public class Point { public double x; public double y; } public class Point { public double x; public double y; public Point(){ } }
  • 13. Rectangle.java (file available on gh-pages) // A Rectangle class public class Rectangle { // 4 instance attributes public double width; public double height; public double originX = 0.0; public double originY = 0.0; // 1 static attributes public static final int NUMBER_OF_SIDES = 4; // method: move the rectangle public void move(double dx, double dy) { originX += dx; originY += dy;
  • 14. Learning resources The workshop homepage https://mcollison.github.io/JPMC-java-intro-2021/ The course materials https://mcollison.github.io/java-programming-foundations/ • Session worksheets – updated each week
  • 15. Additional resources • Think Java: How to think like a computer scientist • Allen B Downey (O’Reilly Press) • Available under Creative Commons license • https://greenteapress.com/wp/think-java-2e/ • Oracle central Java Documentation – https://docs.oracle.com/javase/8/docs/api/ • Other sources: • W3Schools Java - https://www.w3schools.com/java/ • stack overflow - https://stackoverflow.com/ • Coding bat - https://codingbat.com/java