Total members 11894 |It is currently Thu Nov 21, 2024 6:36 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





If I have a class A with a static method f(), and an object x of
type Class<? extends A> which has been assigned B.class, where B
extends A, how can I use x to call B.f()?




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

to get this approach working you have to use polymorphism in the
declaration of M.g:
Code:

public class M 
{
public static <T extends A> int g (T x) {return T.();} public
static 
<T extends B> int g (T x) {return T.();}
}

class A { public static int f() { return 28; } }
class B extends A { public static int f() { return 42; } }

public class testABC {
public static void main(String[] args) {

A alpha= new A ();
B beta = new B ();
M m = new M ();

int retA = m.(alpha);
int retB = m.(beta);

System.out.print(retA);
System.out.print(retB);
}
}
 


_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Passing Enum as Type Parameter to method
 Passing arrays as function parameter in java     -  
 argument type of a program's main() method     -  
 Enum Declaration     -  
 Java enum example     -  
 Define enum in java     -  
 how to use enum in jaxb unmarshalling     -  
 extending enum in java     -  
 Array Passing     -  
 Define Enum inside a class     -  
 get url parameter     -  



Topic Tags

Java Basics, Java OOP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com