- Objects
- Portable (Write once, Run anywhere)
Create a object in Java:
1. Create a class to define a new type
public class Rectangle {
int width;
int height;
}
Rectangle r = new Rectangle();
3. Access fields using the reference variable and dot operator
r.width = 5;
r.height = 3;
4. Enhancing Class - Our own constructor(s)
public class Rectangle {
int width;
int height;
// a constructor with two parameters
public Rectangle(int newWidth, int newHeight) {
width = newWidth;
height = newHeight;
}
}
Constructors are special kind of method
- The same as Class name
- No return type (Not even "void")
- A class can have more than one constructor
- A constructor can take zero, one, or more parameters
- If constructors are provided, you must use one
e.g. Rectangle r = new Rectangle(5, 3);
- If you write your own constructor(s) in a class, Java does not generates the no-argument constructor
Constructor Gotcha
- If a class supplies at least one constructor but does not have a no-arg constructor, it is illegal to instantiate without supplying arguments
- If you want to create an instance by a call to "new ClassName()", you must provide a no-arg constructor along with the other constructor(s)
Advantages of Constructors
Users' perspective:
- You don't have to explain to users details of how to initialize the object instance
- Just call a constructor
Programmers' perspective:
- You control the initialization of the object
- If you provide a constructor (or constructors), users must call it (or one of them)
Access Modifiers
public: Everyone can access it
private: Only this class (file) can access it
Declare instance variables to be private to prevent changes by others (users of our class)
- Encapsulation
Declare some methods to be private so as not to expose the internal implementation details of our class
Inheritance
Why subclass?
Add more fields and methods, Override superclass methods
Provide different constructors!
Must Call the Super Class Constructor
The first line of a subclass's constructor must be a call to the superclass constructor
- If not there, you'd get an error if it has args
If the superclass has a no-arg constructor, Java will automatically generate the call of super()
public class Shape {
private double area;
public Shape(double newArea) {
area = newArea;
}
public double getArea() {
return area;
}
@Override
public String toString() {
return "Shape(area=" + area + ")";
}
}
public class Circle extends Shape {
private double radius;
public Circle(double newRadius) {
// notice that we are using Math.PI constant here
super(Math.PI * newRadius * new Radius);
radius = newRadius;
}
public double getRadius() {
return radius;
}
@Override
public String toString() {
return "Circle(radius=" + radius + ")";
}
}
Polymorphism
Many, much + form, shape
A superclass reference variable can reference instances of subclass
Shape s1 = c;
Shape s2 = r;
System.out.println(s1);
System.out.println(s2);
Results:
Circle(radius=3.0)
Rectangle(width=5.0, height=3.0)
Dynamic Binding
Binding: the process of matching a method call with the correct method definition
Dynamic binding means binding happens at runtime (late binding)
- Overridden methods
- It is the instance's type that determines which method is called, not the variable's type
- Goes from the bottom of the class hierarchy of the instance's type
Casting
Convert from one type of reference to another
No explicit cast when going up the class hierarchy
Square s = new Square();
Object o = s;
Explicit cast is required going down the class hierarchy
Shape shape = shapeMaker(...);
// if shape is really rectangle, it works
// if not, ClassCastException at runtime
Rectangle r = (Rectangle) shape;
// better to test first if not sure
if (shape instanceof Rectangle) {
Rectangle r = (Rectangle) shape;
}
Java implements "Garbage Collection"
- Java figures out, itself, when you are finished with the storage!
- There is no "free" function
How does this work?
- Java keeps "track" of all references to instances
- Instances with "no references" are "freed"
Sample Final Exam Question
What's the different between a primitive type and a reference type?
Primitive types use fixed storage
- Variables of primitive type contain their values
Reference types are instances that use varied amount of storage
- Amount that are different for each instance
- Variables of reference type "reference" (point to) instances of their type
What's the advantage of using constructors?
Users' perspective:
- You don't have to explain to users details of how to initialize the object instance
- Just call a constructor
Programmers' perspective:
- You control the initialization of the object
- If you provide a constructor (or constructors), users must call it (or one of them)
Constructor Chaining in Action
When constructing an object of a subclass, the subclass constructor first invokes its superclass constructor before performing its own tasks
This process continues all the way to the top of the inheritance hierarchy
* A superclass instance must exist before a subclass instance could exist!
Constructor Gotcha
If a class supplies at least one constructor but does not have a no-arg constructor, it is illegal to instantiate without supplying arguments
If you want to create an instance by a call to "new ClassName()", you must provide a no-arg constructor along with the other constructor(s)
Constructor Chaining Again!
Along with overridden methods and access modifiers
super keyword to call superclass method(s)