Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have been asked in a interview. It seems there is a scenario when, a constructor must need to write while writing a java class. I could not find a proper answer though. Please help me.
Cheers
You must write a constructor when your base class does not have a standard one, which takes no argument.
For example:
class A {
public A(int value) {
}
}
class B extends A {
}
This code does not compile. Java will try to add a default constructor to B, but it could not call super(), since A does not have a zero-argument constructor. Thus, you must define a constructor for B.
If you extend a class that doesn't have a parameterless constructor you must define a constructor.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
We cannot create objects from abstract classes in Java. But there is a part that I do not understand. I have an abstract class called "GameCharacter" in my project. A resource told me to write the code in my assignment like this, but I don't understand, did we not use an object in parentheses(GameCharacter gamechar) Didn't we use an object called gameChar in ?
public void setPlayer(GameCharacter gamechar){
}
This is where inheritance comes into play: gamechar can be any instance of GameCharacter, or any class that extends it. You may not be able to directly create a GameCharacter instance, but this method's logic should work with any instance of any concrete class that extends it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have one existing Java 7 method which accepts nothing but return class instance. I want to change it in java 8 using any existing functional interface but don't know what i can use here.
public NotificationPage infraSelection() {
......
return this;
}
Here NotificationPage is class name.
Maybe you can use the Supplier<T> interface?
It declares the method T get().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want all my other objects to be able to call one common method. What should my shared method type signature for parameters look like?
public class appSocket{
public appSocket appSocket(TYPE genericInstance){
// Do something with genericInstance here
}
}
In other words, what should "TYPE" look like?
I think you're a bit confused. This just corresponds to the current object. There is no reason to ever say someObject.this.foo(). You'd just say someObject.foo(). You just pass in the object.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've got an object that is interface, let's call it MyInterface something.
My interface is empty and is being implemented by two classes.
First on has one variable int x, and the second one two: int x, int y.
Variables are private but they've got "get" methods and i know them.
Is it possible to get to the variable of a class just by using "something"?
No, it's not possible (excluding reflection magic), unless you add a getX() method to MyInterface. Then, it's easy and it has the nice side-benefit of being a correct design.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In Android there's a pretty standard method for making your system classes interact with theirs:
#Override
public void onResume() {
super.onResume ();
//your code here
}
When you type "onr" and then control-space in eclipse, the stub fills out like this, with the super method in the call. how would you indicate that you want to do this from the
superclass?
There are two cases for calling a function in a subclass from the superclass:
When the superclass or one of its ancestors has declared the function that you want to call - simply call that function: the override is going to be called.
When the function is first introduced in a subclass - you need an explicit cast in your call. This option almost always indicates a problem in your design, and should be avoided.
when implementing subclassing, you can override any public or protected method, the default behavior of a overridden method in Eclipse is to do what the parent class does, therefor the automatic code for any method that's overriden is to call the parent via super.