Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

generic method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mala Gupta & everyone

hopefully everyone is fine.Here is my problem statement:

On code below, I am failing to understand clearly why iob.sameAvg(dob) in main method can not be applied to this method sameAvg(Stats<T> ob) yet T refers to any class which implements Number interface?


Pliz !! help, I am stuck.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tau murinda wrote:
On code below, I am failing to understand clearly why iob.sameAvg(dob) in main method can not be applied to this method sameAvg(Stats<T> ob) yet T refers to any class which implements Number interface?



It's a generic type mismatch. The iob variable is a Stats<Integer> object, so the method is working with an Integer generic type. You are passing a parameter with a different generic type.

Perhaps you wanted to make the sameAvg() method a generic method instead? In that case, there will be two generic types. One for the object (this). One for the method (local object). And your method is responsible for managing the differences.

Henry
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tau as Henry mentioned, your method sameAvg will take only an Object of Stats which has the same type T.

As Henry said, this is because the method has the same type as the instance
class Stats<T extends Number> {
...
boolean sameAvg(Stats<T> ob) {
...


If you want the method to be able to accept any Status object, you'll have to define the type in the method, something like:
<E extends Number> boolean sameAvg(Stats<E> ob)

Now your method will be able to accept any Stats object...
 
tau murinda
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Henry

Thank you Henry, it is now done and done, now diving into generic methods to get much of it.
 
tau murinda
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit,thank you guys
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic