Method Overridden In Method Parameter?
Jul 20, 2014
I was following a tutorial for libGdx and suddenly became confused by the following syntax:
up.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y){
position.y += 1f;
//currentFrame = animation.getKeyFrame(12);
}
});
"up" is basically a text Button:
TextButton up = new TextButton("up", textButtonStyle);
and .addListener is just one of the methods "TextButton" has (actually I think its inherited from "Button" but that doesn't matter).
Basically my question is what's going on inside the parentheses? From what I see its a new instance of "ClickListener" but then suddenly they override an actual method within. Is this simply just a way to override a method from the ClickListener class or is it something else?
View Replies
ADVERTISEMENT
Aug 26, 2014
can a static method be overridden?
View Replies
View Related
Sep 4, 2014
I would like to know what does it mean when a method is overridden by a class??Like for example when you append an object value to StringBuilder
View Replies
View Related
Mar 19, 2015
I am practicing OCPJP ....
import java.lang.*;
class InvalidValueException extends IllegalArgumentException {}
class InvalidKeyException extends IllegalArgumentException {}
class BaseClass {
void foo() throws IllegalArgumentException {
throw new IllegalArgumentException();
[Code] .....
Which one of the following options correctly describes the behavior of this program? And the answer is (definitely) --> The program will print : InvalidKeyException exception, but when i saw the explanation, it tells
It is not necessary to provide an Exception thrown by a method when the method is overriding a method defined with an exception (using the throws clause).
I don't know, but i think it will compiled because the Exception that is thrown by the foo method in DeriDeri class is inherited from unchecked exception.. so it is not necessary to declare throws statement on its method.. and if the exception was checked exception the answer must be different right?
View Replies
View Related
Mar 5, 2015
For starters here is my code:
Java Code: class A {
int x=5;
}
class B extends A {
int x=6;
}
public class CovariantTest {
public A getObject() {
[Code] ....
And this is the output I get:
sub
5
I am unable to figure out how this is outputting 5 instead of 6. The getObject method of SubCovariantTest is obviously the one being called, and it returns a new B(). So why am I getting class A's x value? I thought since I was getting a B object returned that I would get B's x value.
View Replies
View Related
May 14, 2015
If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you're calling the supertype version of the method.
is this true? maybe i'm misunderstanding it, but i thought the JVM looks at the object at run time and checks the object type. the context of the quote is about checked exceptions, but it seems like the statement should stand regardless of context. but this doesn't back up my experience. for example:
public class Test{
public void print(){
System.out.println("Super");
} public static void main(String[] args){
Test t = new SubTest();
[Code] ....
Will invoke the subclass method. like i said, maybe i'm missing something.
View Replies
View Related
May 28, 2014
I don't understand, why when in the constructor of the superclass A the method init() is called (line 4), the overridden version of the subclass is invoked (line 26) and not the superclass version (line 7):
class A {
public A () {
init();
}
public void init() {
System.out.println("test");
[Code] ....
I would have guessed that above code prints
test
1
But instead you get a NPE (because when the constructor of B is invoked
public static void main(String[] args) {
new B();
}
Then there is first the implicit call to super:
public B() {
s = " ";
init();
}
Which is the constructor of A:
public A () {
init();
}
But here now this init() method is not the version of A ( public void init() {
System.out.println("test");
}) but the overriden version of the subclass (B): public void init() {
System.out.println(s+=s.length());
}...
Which throws an NPE of course, because the initialization of s has not occured yet (it happens only after the implicit call to super() has finished (see public B() {
s = " ";
init();
}))
View Replies
View Related
Nov 27, 2014
I have two methods with parameters out of the main method, both of them work fine alone but I am finding a problem to use them together. I need to use the parameter of Method one in the second Method.
View Replies
View Related
Jan 2, 2014
So, I am learning Swing, and Swing Layouts. I just came across a really confusing (for me) line of code.
Java Code:
setLayout(new BorderLayout()); mh_sh_highlight_all('java');
So, this is weird for me because I don't really understand why the BorderLayout class constructor is being initialized as a parameter for the setLayout..
View Replies
View Related
Jan 30, 2015
Java SE Runtime Environment build 1.8.0..This is part of the code:
public static int addAddress (String[] number, boolean[] front, double[] total) {
int num = 0;
double ffee = 0;
/*boolean value = false;*/
[code]...
I have tried using the line of code commented out, /*boolean value = false;*/. However, another error is displayed. The compiler shows the following...
Inspection.java:33: error: incompatible types: boolean cannot be converted to boolean[]
front[num]= defineFront(num, value);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output error...I know that boolean values are by default stored as false, once you create the array. However, I'm having trouble passing the variable to the method.
View Replies
View Related
Jan 3, 2015
I have a method that accepts as a parameter an object:
public void addClient(Client c){ }
Client is a class which has a constructor that has two String parameters:
public Cliente (String name, String lastname){
this.name = name;
this.lastname = lastname;
}
In the main add a Client in this way:
addClient(new Cliente("first", "second"));
Now, i have an array of Client, so I would like to enter within this. Example:
public void addClient(Client c){
for (int i = 0; i<client.length ; i++) { // client is an array of Client object
client[i] = c; // Enter a c in the array, but does not work!
System.out.println("test "+clienti[i]); // print Client@15db9743
}
}
I have used the println for check if worked insertion, but the result shows no
View Replies
View Related
Jan 15, 2014
how is cast a class at a methods parameters? i have a problems in a methods paramter. i draw red line my exception and mymethods is orage color ....
Caffe drink = new Caffe();
CoffeCup cup = new CoffeCup();
cup.setTempeture(100);
drink.drinkcaffe((CaffeCup)(cup.getTempeture()));
[code]....
View Replies
View Related
Jul 1, 2013
I was going through some lectures online and found that to compare or even swap, the use of comparable or comparator argument like
public static boolean less(Comparable v,Comparable w)
{
return v.compareTo(w)<0;
}
public static void swap(Comparable []a,int i,int j)
{
Comparable swap=a[i];
a[i]=a[j];
a[j]=swap;
}
I did not get the use of passing Comparable or Comparator to the function as parameters. Object as parameter could have been used too?
View Replies
View Related
Jul 31, 2014
I have the following method:
public static void doSomething(List<? extends GenericClass> input)
{
// op
}
^
This compiles and works, ensuring I can only pass in a List of a type extending GenericClass.But now I want it to accept an Array instead of List. This is where I'm stuck:
public static void doSomething(<? extends GenericClass>[] input)
{
// op
}
^
A wrong guess at the syntax which does not compile. One solution is to convert Array into ArrayList before calling the 1st method above, but I'd rather bypass this if possible.
View Replies
View Related
Apr 28, 2015
I have a drop-down which contains the four sections simple buttons(filters). When click any of these buttons some settings are applied. I have successfully auotmated it using simple if else and switch but in that case i have to use 8 parameters(8 are the number of button)
public void editFilters(WebElement filter1, WebElement filter2, WebElement filter3, WebElement filter4,WebElement filter5,WebElement filter6,WebElement filter7,WebElement filter8 String edit, String expectedColour) {
switch (edit) {
case "selectFilter":
if (filter1 != null) {
[Code] .....
But want to make it more effective by using hashes. I do not want to use 8 different parameters to perform action on the respective button.
So now what i want to implement.
Create a method in which i pass the parameter1 as hash and 2nd parameter as 0 or 1, 0 means unSelectFilter and 1 means select the filter.
With parameter 1, in code i want to pass the name or xpath or anything else for any number of filters , that those filters names should be stored into that hash and then by passing 0 or 1, i can select/unselect those filters.
View Replies
View Related
Jul 14, 2014
I am following this article [URL] .... till now I have made some code
This is my Interface
public interface Comparable<T> {
public int compareTo(T o);
}
And this is my class where I am using Bound Type Parameter on Generic Methods
public class GenericMethodBoundType {
public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
[Code] .....
What else I need to do both in main method and at what parameterized types I need to pass at the class?
View Replies
View Related
Feb 19, 2014
how to get an access to the method with a parameter of class type variable, lets say: public void insert(Record newRecord, int pos)?
View Replies
View Related
Apr 9, 2014
The instructions:
Write a method named hopscotch that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that many hops. A "hop" is defined as the split into two numbers and then back together again into one.For example, hopscotch(3); should print:
1
2 3
4
5 6
7
8 9
10
how to start this program.
View Replies
View Related
Apr 23, 2014
If you write
byte b = 100; it works (implicit conversion of implicit int literal 100 to byte.
But if you have a methodvoid bla(byte b){}
And want to invoke it with a literal (which is an int by default):bla(8) then there is no implicit conversion.
Is the byte b = 100; just an exception in Java? And is it the rule that one has to explicitely cast (narrow) integer literals when passing to smaller-than-int types?
View Replies
View Related
Mar 9, 2015
so i have this question where it wants me to create a recursion method that takes ONLY THE ARRAY as a parameter, and without using loops or static variables inside the method, and then the method returns the smallest value in that array. However, i tried making the simple if statements where i compare the first element of the array with the second element using the length of the array and decreasing it to get the next elements and compare it again by calling the recursion method, but the problem is when i call the method again, the length does not decrease, even if i store it in a variable, the variable will initialize itself again, and the length wont change.
View Replies
View Related
Feb 7, 2015
In my book for learning java, one of the questions asks us to create a method header named convertTOKM that takes an int parameter, which is the number of miles, and returns a double value for the converted value in kilometers. I made one, but wanted to know if I was right in any way.
Here it is:public double convertTOKM(int miles, double kilometers){
View Replies
View Related
Nov 4, 2014
I'm doing an assignment where a method receives a Queue as a parameter then convert it into a stack, I made it but now I want to try it in my main, but how can I do this? there's no toString and I cant use a for loop because I'm using a queue and not an array. This is the head of my method:
public static void QueueStack(Queue<Integer> q){
View Replies
View Related
Nov 1, 2014
I am practicing some basic recursion and I was trying to solve this problem
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words:
sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n
For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should throw an IllegalArgumentException if passed a value less than 0.
This is my attempt to do it , however my output is always a 0.0 , and i do not understand why :
public static double sumTo(int n ){
if(n<0){
throw new IllegalArgumentException();
}
else if (n==0){
return 0.0;
[Code] .....
View Replies
View Related
Apr 22, 2014
i have to "Write a method called addToOverThirty which takes the array nums3 as a parameter. It adds 1 to all numbers that have a value greater than 30 in the array.
Add a call to the addToOverThirty method, then display a message telling what the output is followed by the results For example:The nums3 array after adding 1 to all numbers greater than 30 is10 6 15 and so on (check with the values you assigned to nums3)"its pointless because we were told not to make the array have a number over 30.
import java.lang.*;
import java.util.*;
public class LastProject
public static void main(String[] args) {
int nums1[] = new int[15];
int nums2[] = new int[15];
int nums3[] = {5,2,15,8,26,22,1,29,4,23,30,11,19,6,24};
[code]....
View Replies
View Related
Nov 18, 2014
I had a TestColor class which contained methods to change hue, saturation, brightness, red, green, blue of TestColor's instances but also had static methods which take in an additional parameter for an instance of TestColor and returns the affected instance of TestColor. Now instead of having one method for every possible color effect to be applied to an image, how can I have one method that takes in an Image parameter, a static or non-static method reference from TestColor parameter and lastly an intensnity value parameter. This is so that I can make an affectedImage object instance inside the method and a Graphics2D object for drawing to each pixel of the new image, now I have one for loop and one nested for loop for the x and y pixels of width and height of the old image and inside the nested for loop I'd create a TestColor by calling getRGB on the image's pixel. Then I would apply the static or non-static method reference somehow to change the color with the intensnity value and after applying it draw to the new Image with Graphics2D. How to would I parametize a method reference and be able to use it in such way?
View Replies
View Related
May 27, 2014
I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?
View Replies
View Related