OOP Relationships

Classes can have different relationships in java.
Programs utilize these for their code to run efficently.

The four relations of classes are subclasses, super classes, abstract classes and interfaces

SubClass vs Super Class

Going to show the difference between a subclass and super with my Software Object Project

SuperClass SubClass
More Generic Form of a class. Called the parent class. Can store children instances in the parent object. Downcasting require to run child's
non-overriding methods
Inherits the attributes and methods (More specific functionality) and can override the parent class. Its constructor needs to run parent's constructor.

Copy

import java.lang.Math;
public class TiposedLarenim {
    static int population=100;
    String name;
    double latitude=0;
    double longitude=0;
    double weight;
    boolean submerged=false;
    double energy;
    int age;
    String[] possibleOres = {"iron","gold","palladium","diamond","platinum","lithium"};
    String ores="";
    public TiposedLarenim(){
        name="greg";
        latitude=0;
        longitude=0;
        weight=200;
        energy=100;
        this.age=100;
    }  
    public TiposedLarenim(String n, double la,double lo, 
            double w, double e, int age){
        name=n;
        latitude=la;
        longitude=lo;
        weight=w;
        energy=e;
        this.age=age;
    }
    public boolean eat(String food, int[] heights){
        if((food.equals("trees") && ! submerged) || (food.equals("coral") && submerged)){
            for(int i=0;i<heights.length;i++){
                if(energy>=100){
                    generateOres();
                    energy-=50;
                    weight+=5;
                }else{
                    energy+= Math.pow(heights[i],1.1);
                    weight++;
                }
            }
            move();
            return true;
        }
        return false;
    }
    public void move(){
        double movelat = Math.random()*10-5;
        double movelong = Math.random()*10-5;
        latitude+=movelat;
        longitude+=movelong;
        if(latitude>180 || latitude<-180){
            double newLatitude= latitude>180 ? latitude-movelat*2 : latitude+movelat*2;
            latitude=newLatitude*-1;
        }if(longitude>90 || longitude<-90){
            double newLongitude= latitude>90 ? latitude-movelat*2 : latitude+movelat*2;
            longitude=newLongitude*-1;
        }
    }
    public void generateOres(){
        int index= (int) Math.floor(Math.random()*(possibleOres.length-0.1));
        String ore= possibleOres[index];
        double amount = Math.round((Math.random()+1)*weight*2)/50;
        ores+=amount+" kgs of "+ore+", ";
    }
    public void setSubmerged(boolean sub){
        submerged=sub;
    }
    public String  harvestOres(){
        String harvestedOres=ores;
        ores="";
        return harvestedOres;
    }
    public boolean breed(TiposedLarenim other){
        if(Math.ceil(this.latitude) == Math.ceil(other.latitude) 
        && Math.ceil(this.longitude) == Math.ceil(other.longitude)){
            population++;
            return true;
        }
        return false;
    }
}

Copy

public class FlyingTiposedLarenim extends TiposedLarenim{
    private boolean overWater=false;
    private double altitude;
    public FlyingTiposedLarenim(){
        super();
        altitude=1500;
        possibleOres= new String [1];
        possibleOres[0]="gold";
    }
    public FlyingTiposedLarenim(String n, double la,double lo, 
            double w, double e, int age, double a, String o){
        super(n,la,lo,w,e,age);
        altitude=a;
        possibleOres= new String [1];
        possibleOres[0]=o;
    }
    public void move(){
        double movelat = Math.random()*10-5;
        double movelong = Math.random()*10-5;
        double moveAlt = Math.random()*5000-altitude;
        latitude+=movelat;
        longitude+=movelong;
        altitude+=moveAlt;
        if(latitude>180 || latitude<-180){
            double newLatitude= latitude>180 ? latitude-movelat*2 : latitude+movelat*2;
            latitude=newLatitude*-1;
        }if(longitude>90 || longitude<-90){
            double newLongitude= latitude>90 ? latitude-movelat*2 : latitude+movelat*2;
            longitude=newLongitude*-1;
        }
    }
    public void overWater(boolean ow){
        overWater=ow;
        if(altitude<3000){
            altitude=altitude-altitude-300;
            submerged=true;
        }
    }
    public void setSubmerged(boolean sub){
        return;
    }
    public boolean breed(FlyingTiposedLarenim other){
        if(Math.ceil(this.latitude) == Math.ceil(other.latitude) 
        && Math.ceil(this.longitude) == Math.ceil(other.longitude) 
        && Math.abs(this.altitude-1000)<=other.altitude){
            population++;
            return true;
        }
        return false;
    }
}

There is much less code for the sub-class compared to the parent class. By extending a class, you allow the child class to inherit the functionality of the parent. Meaning, you don't need to rewrite methods unless you are overriding them.

Polymorphism

Did you know that you can set a super class variable to an instance of a sub class. Like, so

TiposedLarenim myCreature= new FlyingTiposedLarenim();
However, if you want to run a function, that is exclusive to FlyingTiposedLarenim: overWater() for Example, you would need to down cast it like
(FlyingTiposedLarenim myCreature).overWater()
All Overriden methods are accesible without downcasting and when called it will use the overriden method from the subclass.

Abstract Classes vs Interfaces

Abstract Class Interfaces
Acts like a normal class, but has some undefined functions for methods called abstract methods. These are to be overriden by a subclass. If all abstract methods are not overriden by child, child must be abstract. An abstract class, but it cannot define any functions. It also can't set any attributes. It is a list of methods needed for classes that implement the interface. If all methods are not implemented, class becomes abstract.

Here is an abstract class room I made for the Fantasy Assignment

Copy

public abstract class java.util.*;
public abstract class Room{
    protected String roomType;
    protected ArrayList<MythicalCreature> creaturesInside; 
    public Room(String rt){
        creaturesInside= new ArrayList<MythicalCreate>();
        roomType=rt;
    }
    public abstract void interact(MythicalCreature a);
    public void enter(MythicalCreature x){
        if(MythicalCreature instanceof goblin && creaturesInside.size()>0){
            for(MythicalCreature y : creaturesInside){
            ((goblin) x).stealGold(y);
            }
        }
        creaturesInside.add(x);
        interact(x);
    }public void exit(MythicalCreature x){
        creaturesInside.remove(x);
    }public String getRoomType(){
        return roomType;
    }public ArrayList<MythicalCreature>  getCreaturesInside(){
        return creaturesInside;
    }
}

I didn't use any interfaces for projects, but here is one from a practice test

Copy

public interface Computable{
    //Return this Object + y. 
    Object add (Object y);
    //Return this Object - y. 
    Object subtract (Object y);
    //Return this Object y. 
    Object multiply (Object y);
}

Interfaces are practically checklists for functions and why it is only used if there are a lot of functions required for a program. Graphics program would use Interfaces as there is a decent amount of methods required.