Implementation with various java signature

While I was developing class architecture to meet my needs, I came across this thing. I have an abstract class in which there are some methods inside it that must be implemented by the subclass, but in the subclass I found that I need to implement with a signature that inherits from the first.

To show you what I mean:

// person class public abstract class Person { protected void abstract workWith(Object o) throws Exception; } //developer class public class Developer extends Person {// i want to implement this method with Computer parametre instead of Object and throws `//DeveloperException instead of Exception` protected void workWith(Computer o) throws DeveloperException { //some code here lol install linux ide server and stuff } } // exception class public class DeveloperException extends Exception { } 

Is there any way to do this? I do not know if this is possible with the help of the general. Thank you so much.

+4
source share
1 answer

You can definitely use generics for this:

 public abstract class Person<T, U extends Exception> { protected abstract void workWith(T t) throws U; } class Developer extends Person<Computer, DeveloperException> { protected void workWith(Computer c) throws DeveloperException { //implementation code } } 

Performs what you want, but we need to get more detailed information about your use case to determine if it fits the right design.

+5
source

Source: https://habr.com/ru/post/1500569/


All Articles