Skip to content
Advertisement

Abstract Classes & Methods In Java

I am working on a Java exercise where a parent class is abstract, and the subclasses override the isSimilar method. This method should take a device parameter. In the Server subclass, this method should compare the Brand, Operating System, and Available Services. In the NetworkDevice subclass, this method should compare the Brand, Number of Ports, and Power Over Ethernet.

The problem with this method is that I can’t compare the devices if it only takes in one parameter. (At least I could not find out how to do so.)

— Device Class —

public abstract class Device {
    protected int id;
    protected String brand;

    // The Constructor Methods.
    public Device(){}
    public Device(int id, String brand){
        this.id = id;
        this.brand = brand;
    }

    // The Get Methods.
    public int getId() {return id;}
    public String getBrand() {return brand;}

    // The Set Methods.
    public void setId(int id) {this.id = id;}
    public void setBrand(String brand) {this.brand = brand;}

    // Returning Information.
    public String toString(){
        String s = "Id; " + brand + ", Brand: " + brand + ". ";
        return s;
    }

    // Other Abstract Classes.
    public abstract boolean isSimilar(Device d);
}

— Server Class —

public class Server extends Device{
    private String operatingSystemType;
    private String availableServices;

    // The Constructor Methods.
    public Server(){}
    public Server(int id, String brand, String operatingSystemType, String availableServices){
        super(id, brand);
        this.operatingSystemType = operatingSystemType;
        this.availableServices = availableServices;
    }

    // The Get Methods.
    public String getOperatingSystemType() {return operatingSystemType;}
    public String getAvailableServices() {return availableServices;}

    // The Set Methods.
    public void setOperatingSystemType(String operatingSystemType) {this.operatingSystemType = operatingSystemType;}
    public void setAvailableServices(String availableServices) {this.availableServices = availableServices;}

    // Server Related Methods.
    public boolean addAService(String service){
        String[] services = getAvailableServices().split(":");
        for (int i = 0; i < services.length; i++) {
            if (services[i].equalsIgnoreCase(service))
                return false;
        }
        setAvailableServices(getAvailableServices() + ":" + service);
        return true;
    }

    public boolean findAService(String service){
        String[] services = getAvailableServices().split(":");
        for (int i = 0; i < services.length; i++) {
            if (services[i].equalsIgnoreCase(service))
                return true;
        }
        return false;
    }

    @Override
    public boolean isSimilar(Device d) {
        if(d.getBrand().equalsIgnoreCase(getBrand())) {
            return true;
        }
        return false;
    }

    // Returning Information.
    @Override
    public String toString() {
        String s = super.toString() + ", Operating System: " + getOperatingSystemType() + ", Services: " + getAvailableServices() + ".";
        return s;
    }
}

— Network Device Class —

public class NetworkDevice extends Device{
    private int numberOfPorts;
    private boolean poweredByEthernet;

    // The Constructor Methods.
    public NetworkDevice(){}
    public NetworkDevice(int id, String brand, int numberOfPorts, boolean poweredByEthernet){
        super(id, brand);
        this.numberOfPorts = numberOfPorts;
        this.poweredByEthernet = poweredByEthernet;
    }

    // The Get Methods.
    public int getNumberOfPorts() {return numberOfPorts;}
    public boolean isPoweredByEthernet() {return poweredByEthernet;}

    // The Set Methods.
    public void setNumberOfPorts(int numberOfPorts) {this.numberOfPorts = numberOfPorts;}
    public void setPoweredByEthernet(boolean poweredByEthernet) {this.poweredByEthernet = poweredByEthernet;}

    @Override
    public boolean isSimilar(Device d) {
        if(d.getBrand().equalsIgnoreCase(getBrand()))
            return true;
        return false;
    }

    // Returning Information.
    @Override
    public String toString() {
        return super.toString() + "NetworkDevice{" +
                "numberOfPorts=" + numberOfPorts +
                ", poweredByEthernet=" + poweredByEthernet +
                '}';
    }
}

Advertisement

Answer

Your approach is correct. For clarification purposes, whenever you are overriding isSimilar method, you can you this keyword to shows that the comparison will take place between the brand of the device passed and the object which is calling the method. your implementation of isSimilar is also correct but you can optimize this by just keeping it as one liner.

  @Override
  public boolean isSimilar(Device d) {
        return d.getBrand().equalsIgnoreCase(this.getBrand()) ;
  }

Now to compare them, I have created a main class where I am creating the objects of class Server and Network device and calling the isSimilar method.

public class Main 
 
{
  public static void main(String args[]) {
    Device deviceA = new Server(1,"samsung","core-4","ten"); 
    
    Device deviceB = new Server(1,"samsung","core-4","twenty"); 
    
    System.out.println(deviceA.isSimilar(deviceB));
    
    
    Device deviceC = new NetworkDevice(1,"samsung",4,false); 
    
    Device deviceD = new NetworkDevice(1,"galaxy",6,true); 
    
    System.out.println(deviceC.isSimilar(deviceD));
    
  }
}

and the output is as follows

true
false
Advertisement