Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Generics and Inheritence.

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generics and Inheritence.

    I've an interface with generic methods in it. I would like to have specialized methods in the sub types. While doing that I'm seeing the following warnings in eclipse. Appreciate if someone can clarify:

    class Sorter {
    <E> void sort(E[] elements);
    };
     
    class StringSorter {
     
    // This gives me a warning 'hiding' to 'sort'
    <String> void sort(String[] elements) {
    }
     
    // Gives me an error "The method someCrap(String[]) in the type StringSorter is not applicable for the arguments (String[])"
    void someCrap(String[] elements) {
    }
    };

    I would like to understand why eclipse gives the above warnings and errors.

    Regards,
    -C


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    898
    Thanks
    0
    Thanked 166 Times in 148 Posts

    Default Re: Generics and Inheritence.

    Try this:
    class Sorter<E> {
    <E> void sort(E[] elements);
    };
     
    class StringSorter implements Sorter<String> {
     
    // This gives me a warning 'hiding' to 'sort'
    <String> void sort(String[] elements) {
    }
    };

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Generics and Inheritence.

    Quote Originally Posted by Cornix View Post
    Try this:
    class Sorter<E> {
    <E> void sort(E[] elements);
    };
     
    class StringSorter implements Sorter<String> {
     
    // This gives me a warning 'hiding' to 'sort'
    <String> void sort(String[] elements) {
    }
    };
    I don't what a generic class. I wish my component is agnostic to the specific type. My component gets injected with the implementation. Consumers consume it from my component. Consumer is expected to know the type of the instance and use it accordingly.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,304
    My Mood
    Mellow
    Thanks
    252
    Thanked 778 Times in 763 Posts

    Default Re: Generics and Inheritence.

    Please post actual code that demonstrates the problem. Your explanation is muddled by ambiguous terminology.

Similar Threads

  1. [SOLVED] Generics
    By stewbond in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 17th, 2013, 04:31 PM
  2. Inheritence and Polymorphism
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 1st, 2013, 08:59 AM
  3. Inheritence and problems with static methods.
    By Sylis in forum What's Wrong With My Code?
    Replies: 15
    Last Post: November 4th, 2012, 02:13 AM
  4. Generics.
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2011, 06:53 PM
  5. Problem with inheritence? arguments not applicable
    By rbk in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2011, 10:37 AM

Tags for this Thread