0

I have already created a class called priority queue.

public class PriorityQueue<T> implements Iterable<T>

And now, I want to create a class SpecialPriorityQueue<T> with these details.

  1. While T in PriorityQueue can be any type, T in SpecialPriorityQueue has to implement an interface.
  2. SpecialPriorityQueue inherits from PriorityQueue, and it override some methods and uses some protected field of the PriorityQueue.

Is there any clean way to do this without changing the code of PriorityQueue?

1
  • "While T in PriorityQueue can be any type, T in SpecialPriorityQueue has to implement an interface." - I recommend reading a tutorial on bounded types, e.g. this one by Oracle. --- "SpecialPriorityQueue inherits from PriorityQueue, and it override some methods and uses some protected field of the PriorityQueue." - So, normal polymorphism then? Commented Aug 28, 2021 at 9:28

2 Answers 2

1

Not sure if I understand your question correctly, but - yes, sure you can accomplish all of that.

  1. You just declare SpecialPriorityQueue with a restricted type:

      public class SpecialPriorityQueue<T extends AnInterface> extends PriorityQueue<T> 
    
  2. Overriding methods and using protected fields from the parent class is straightforward:

      @Override
      public void inheritedMethod() {
          // just use inherited protected fields here
      }
    
Sign up to request clarification or add additional context in comments.

Comments

1
public class SpecialPriorityQueueu<T extends YourInterface> extends PriorityQueue<T>

Then to use protected fields and override methods you do it as you would do for any class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.