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 —

JavaScript

— Server Class —

JavaScript

— Network Device Class —

JavaScript

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.

JavaScript

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.

JavaScript

and the output is as follows

JavaScript
Advertisement