Java Fundamental period - Lecture 6

Java and OO Concepts : Instantiation, Inheritance, Abstraction

Summary of the last lectures

We know how to :
We saw basic (default) constructors , and how to call it through a java code to create new instances
How to construct complex objects? How to take advantages of the inheritance mechanism? What is the Abstraction concept

Constructors : recall

Constructors : schema

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;


Class vs Instance

Class vs Instance (2)

To precise whether you are using a local variable or a class field, the this keyword is strongly recommended

Taking advantage of inheritance

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);


Taking advantage of inheritance (2)

Warning: The inheritance mechanism allows natural code factoring, but if the relationship between inheriting objects is not a real IS-A relationship, the code maintenance can be very painful


In the geometry example found here, add the Ellipse object and try to generalize the Circle class methods

Inheritance problems

When you are using inheritance you have to face several problems