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.
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 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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I don't get how can I write a sub class which will extend a superclass and a couple of interfaces at the same time.
A class can only extend one superclass, but can implement multiple interfaces, which is done by separating them by a comma, like so:
public class MyClass extends AnotherClass implements AnInterface, AnotherInterface
Extending a class is simple: unless the class you are extending is abstract, your class does not even need to do anything apart from providing a suitable constructor.
Implementing an interface or extending an abstract class, on the other hand, is simply a promise to implement a specific set of methods, as declared in the corresponding abstract class and/or the interface. Again, all your class needs to do is providing some implementation for these methods.
Of course, the implementations need to make sense in the overall scheme of your design, but technically they just need to be there in order for the code to compile.
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
I know that a class which implements an interface can be declared a abstract but not sure if it can be declared as interface? If so,What will be its benefit?
No interface can implement an interface by definition.
An interface is a contract that formalizes a behavior and cannot implement logic.
However, interfaces can extend other interfaces.
An abstract class can implement an interface on the other hand, because either:
Behavior is implemented in the abstract class, or
Behavior implementation is deferred to classes extending the abstract class
Here's a general link to Oracle's OOP documentation you want to have a look at.
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.