Java and OO Concepts : Instantiation, Inheritance, Abstraction
package fr.tbr.exercises;
/**
* Example of a car, constructor with
* a "color" parameter
* @author Tom
*/
public class Car {
private String color;
public Car(String color) {
this.color = color;
}
}
this keyword.this allows to access to the current instance valuesthis keyword can be used in any
non-static methods
/**
* return the calculated interest on one year
* warning, this method updates the balance with the result of that computation
* @return the interest
*/
public double calculateInterest(){
double result = this.interestRate * this.balance;
this.balance += result; //equivalent to this.balance = this.balance + result
return result;
}
this keyword is
strongly recommended
IS-A relationship
public class Quadrilateral {
private double sideA;
private double sideB;
private double sideC;
private double sideD;
/*...*/
public double calculatePerimeter(){
return this.sideA + this.sideB
+ this.sideC + this.sideD;
}
}
public class Rectangle extends Quadrilateral{
public Rectangle(double height, double width)
{
super (height, height, width, width);
}
}
super keyword: it allows the access of the inherited Class (here the
Quadrilateral Class)
calculatePerimeter()
method again
In the geometry example found here,
add the Ellipse object and try to generalize the Circle class methods
super object methods