9953330565 Low Rate Call Girls In Rohini Delhi NCR
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