Class example in Java

    
class Book
{
    private String title;
    private int numPages;
    private double price;

    // Default constructor
    public Book()
    {
        title = "";
        numPages = 0;
        price = 0;
    }

    // Parameterized constructor
    public Book(String title, int numPages, float price)
    {
        this.title = title;
        this.numPages = numPages;
        this.price = price;
    }

    public void printInformation()
    {
        System.out.println("Book information:");
        System.out.println("Title: " + title);
        System.out.println("Pages: " + numPages);
        System.out.println("Price: " + price);
    }

    // Getters
    public String getTitle()
    {
        return title;
    }

    public int getNumPages()
    {
        return numPages;
    }

    public double getPrice()
    {
        return price;
    }

    // Setters

    public void setTitle(String title)
    {
        this.title = title;
    }

    public void setNumPages(int numPages)
    {
        if (numPages > 0)
            this.numPages = numPages;
    }

    public void setPrice(double price)
    {
        if (price >= 0)
            this.price = price;
    }    
}
    

Association example in Java

    
class Book 
{    
    private String title;
    private int numPages;
    private double price;
    private Author author;

    public Book(String title, int numPages, double price, 
        Author author) 
    {   
        ...     
    

Association example in Java

    
Author a = new Author("J.R.R. Tolkien", 1892);
Book b = new Book("The lord of the Rings", 850, 13.50, a);

    

Inheritance example in Java

    
class Comic extends Book
{
    private boolean color;
    private int volumeNumber;

    public Comic(String title, int numPages, double price,
        boolean color, int volumeNumber)
    {
        super(title, numPages, price);
        this.color = color;
        this.volumeNumber = volumeNumber;
    }

    public boolean getColor()
    {
        return color;
    }

    public void setColor(boolean color)
    {
        this.color = color;
    }

    public int getVolumeNumber()
    {
        return volumeNumber;
    }

    public void setVolumeNumber(int volumeNumber)
    {
        this.volumeNumber = volumeNumber;
    }

    @Override
    public void printInformation()
    {
        super.printInformation();
        System.out.println("Color/Grayscale: " + 
            (color?"Color":"Grayscale"));
        System.out.println("Volume: " + volumeNumber);
    }
}
    

Polymorphism example in Java

    
class Vehicle
{
    ...
}

class Car extends Vehicle
{
    ...
}

class Van extends Vehicle
{
    ...
}
    

Polymorphism example in Java

We can instantiate a subtype from main type

    
Vehicle v = new Car(...);
    

Polymorphism example in Java

We can define an array of parent class

    
Vehicle[] vehicles = new Vehicle[10];

vehicles[0] = new Car(...);
vehicles[1] = new Van(...);
...     
    

Polymorphism example in Java

We can pass a parameter of any subtype to a method expecting parent class

    
public void aMethod(Vehicle v) 
{
    ...
}

Car anotherCar = new Car(...);
aMethod(anotherCar);        
    

Example of an abstract class

    
public abstract class Animal
{
    String color;
    int numberOfLegs;

    public Animal(String color, int numberOfLegs)
    {
        this.color = color;
        this.numberOfLegs = numberOfLegs;
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

    public abstract void talk();
}
    

Example of abstract class inheritance

    
public abstract class Bird extends Animal
{
    public abstract void fly();
}

public class Duck extends Bird
{
    public Duck(String color, int numberOfLegs)
    {
        super(color, numberOfLegs);
    }

    @Override
    public void talk()
    {
        System.out.println("Quack quack!!");
    }

    @Override
    public void fly()
    {
        System.out.println("I'm flying like a duck!");
    }
}
    

Example of an interface

    
public interface Shape
{
    public float calculateArea();
    public void draw();
}        
    

Example of interface implementation

    
public class Circle implements Shape
{
    float radius;

    public Circle(float radius)
    {
        this.radius = radius;
    }

    @Override
    public float calculateArea()
    {
        return Math.PI * radius * radius;
    }

    @Override
    public void draw()
    {
        System.out.println("Drawing a circle!");
    }
}        
    

Example: sorting objects

We implement Comparator interface with an anonymous class

    
Person[] people = new Person[50];
... // Fill array
Arrays.sort(people, new Comparator()
{
    @Override
    public int compare(Person p1, Person p2)
    {
        return Integer.compare(p1.getAge(), p2.getAge());
    }
});