So the question title is a little weird but it is the only way I could think of the get the point across. I have a NullPointerException from doing this (The extra code is taken out):
public abstract class Generator extends SimpleApplication{
private static SimpleApplication sa;
public static void Generator(){
CubesTestAssets.initializeEnvironment(sa);
}
I know that the private static SimpleApplication sa; is null but when I instantiate it I get this error instead of a NullPointerException :
SimpleApplication is abstract; cannot be instantiated
I am using the jMonkey Engine 3. If anyone knows how I could solve this problem, that would be great! Thanks!
If you take a close look at the JMonkey documentation, SimpleApplication is made to be extended by you to create your application.
You should create a new class that extends SimpleApplication and implement the missing methods. You then instantiate your custom class and pass it as a parameter.
A little like this :
public class myCustomSimpleApplication extends SimpleApplication {
// Implementing the missing methods
#Override
public void simpleInitApp() {
}
#Override
public void simpleUpdate(float tpf) {
}
// etc...
}
And then...
private static SimpleApplication sa = new myCustomSimpleApplication();
public static void Generator(){
CubesTestAssets.initializeEnvironment(sa);
}
As others have commented, your base application class (I assume it is Generator) which extends SimpleApplication must not be abstract, i.e. it should implement, at least, the simpleInitApp method. See the JMonkey documentation for SimpleApplication or this basic example.
For completion, I will mention that if you really needed to create a dummy instance of an abstract class, you can do so "inline", without explicitly writing the full implementing class. You just need to write the implementation of the abstract methods enclosed by curly brackets after the constructor invocation:
public abstract class AbstractClass {
private static AbstractClass a = new AbstractClass() {
#Override
public void abstract_method() {/*implementation*/}
};
public abstract void abstract_method();
}
An anonymous class is created behind the scenes. But I definitely don't think this is what you need in this case.
Related
this is more of a java question rather than an android question. If we look at the code below, I need to access getEnrollmentUsername from my main class:
public interface IAirWatchSDKService extends android.os.IInterface {
public String getEnrollmentUsername(String publicKey);
public abstract static class Stub extends Binder implements IAirWatchSDKService {
private static class Proxy implements IAirWatchSDKService {
#Override
public IBinder asBinder() {
return null;
}
public String getEnrollmentUsername(String publicKey){
return "it worked";
}
}
}
}
but the problem is that it is wrapped inside of an abstract class. How can I get to it from my main class? And none of this code can change because it is part of a library, rather I need to write code from my main class only.
Proxy is declared as a private class. To access it from outside it needs public, protected or default(package private) depending on where you want to access it from.
Best practice is that if your main class needs to run this method, it should implement the interface. That, or the main class is aware of the interface with a method such as this: myIAirWatchSDKService.getEnrollmentUsername(s)
I am porting some OpenGL Nvidia C samples to jogl and I have the following (init is one of the abstract methods required by GLEventListener:
public abstract class NvAppBase implements GLEventListener {
#Override
public void init(GLAutoDrawable drawable) {
initRendering(gl4);
}
public void initRendering(GL4 gl4) {
}
}
public abstract class NvSampleApp extends NvAppBase {
#Override
public void init(GLAutoDrawable drawable) {
baseInitRendering(gl4);
}
protected void baseInitRendering(GL4 gl4) {
initRendering(gl4);
}
#Override
public void initRendering(GL4 gl4) {
}
}
public class BindlessApp extends NvSampleApp{
#Override
public void initRendering(GL4 gl4) {
}
}
Given that:
NvAppBase is not used at all, all the samples (such as BindlessApp) always extend NvSampleApp
I'd like the class extending NvSampleApp to being able to see (and overwrite) only the initRendering and not also the init
Is there a better way than just having NvSampleApp simply as a variable inside BindlessApp, like this for example?
public class BindlessApp {
private NvSampleApp sampleApp;
}
You can use the keyword final for this purpose.
Writing Final Classes and Methods on Oracle java tutorial.
You can declare some or all of a class's methods final. You use the
final keyword in a method declaration to indicate that the method
cannot be overridden by subclasses. The Object class does this—a
number of its methods are final.
Is there a better way than just having NvSampleApp simply as a
variable inside BindlessApp, like this for example?
Although it seems like more work, encapsulation is a great tool to help isolate parts of your code an decrease coupling.
I think in your case it might even be the better solution :)
See for more detail this answer: https://stackoverflow.com/a/18301036/461499
I have an interface called Relation, implemented by a class BasicRelation, and extended by subclasses (e.g. ParentChild, Sibling, Spouse). While developing my code, I realized that I often need a method which takes a String representation of a relation to create it. For example:
public class ParentChild implements Relation extends BasicRelation {
// e.g. "Jack is Emily's father. Jill is her mother." will return the list
// <ParentChild(Jack, Emily), ParentChild(Jill, Emily)>
static List<ParentChild> fromSentence(String s) {
...
}
}
Now, since I find myself needing this method (fromSentence(String)) in every class, except perhaps in BasicRelation, I would like to move it up the hierarchy. The problem is that the internal details of the method is subclass-dependent, so I can't have it as a static method in the interface Relation or the superclass BasicRelation.
Unfortunately, in Java, it is also not possible to have a static abstract method.
Is there any way to ensure that every subclass of BasicRelation (or every class implementing Relation) implements fromSentence(String)? If no, should I be designing this in a completely different way? I guess this last question is more of a request for design-advice than a question.
Why does the static method need to be in the interface? What's stopping you from having a 'Utility' class and having the method in there?
public class RelationUtility {
public static BasicRelation relationFactory(String asString) {
....
}
}
As a static method, there is no reason other than access to private members, which can also be accomplished by by 'default' permissions on those members....
You can try making the BasicRelation class an abstract class and use an abstract fromSentence(..) method. This would require the ParentChild class to override and implement the fromSentence method because you can't create an object for ParentChild without implementing fromSentence()
public abstract class BasicRelation extends Relation(){
public abstract List<..> fromSentence(String s);
}
public class ParentChild implements Relation extends BasicRelation {
fromSentence(){
//parentChild class's implementation
}
}
If I understood right... you can try an approach like this
public class BasicRelation {
public abstract List<ParentChild> fromSentenceInSubclass(s);
public List<ParentChild> fromSentence(String s){
fromSentenceInSubclass(s);
}
}
And then you could have:
public class SubclassRelation extends BasicRelation {
public List<ParentChild> fromSentenceInSubclass(s){
// do subclass relation stuff
}
}
You will probably need to change the code a bit and add some Generics around to make it happen the way you want.
Sotirios Delimanolis Factory suggestion might also be an option.
You can have the abstract class BasicRelation include the static method which throws an Exception. That way you will be forced to override (shadow) the static method in the subclasses when you use it.
Something like:
public abstract class BasicRelation {
public static List<..> fromSentence(String s) {
throw new RuntimeException();
}
}
Given an interface:
public interface GoAlgorithm
{
public void go();
}
A class which implements the interface:
public class GoByFlyingFast implements GoAlgorithm
{
public void go()
{
System.out.println("Now I'm flying FAAAAST. Woo-hoo!");
}
}
An abstract class
public abstract class Vehicle
{
private GoAlgorithm goAlgorithm;
public Vehicle()
{
System.out.printf("A new vehicle has entered the race! \n");
}
public void setGoAlgorithm(GoAlgorithm algorithm)
{
this.goAlgorithm = algorithm;
}
public void go()
{
this.goAlgorithm.go();
}
}
A class extending the abstract class:
public class Tomcat extends Vehicle
{
public Tomcat()
{
setGoAlgorithm(new GoByFlyingFast());
}
}
(Code examples from Design Patterns for Dummies)
Is there a way, within the abstract class's implementation of the interface's method, to determine the subclass of the object invoking the method of the interface?
Can go() determine if a Tomcat object is invoking the go() method as defined by the Vehicle class?
Apologies if this question has already been posed. As evidenced by this question's title, I'm struggling to word my question concisely.
Thank you!
EDIT: Pure curiosity question
Theoretically yes. You can get the current stack trace with Thread.getStackTrace(), analyze it and look at the dynamic class of your caller. However, this would be VERY bad design.
Alternatively you can have your subclasses implement a getName() function, which returns their classname, so you could access that in your superclass.
In any case, it is good design if your (abstract) classes do not change their behavior based on which class is subclassing them. So there should not be a need for obtaining such information.
I know it is not a good coding practice to declare a method as private in an abstract class. Even though we cannot create an instance of an abstract class, why is the private access modifier available within an abstract class, and what is the scope of it within an abstract class? In which scenario is the private access specifier used in an abstract class?
check out this code where Vehicle class is abstract and Car extends Vehicle.
package com.vehicle;
abstract class Vehicle {
// What is the scope of the private access modifier within an abstract class, even though method below cannot be accessed??
private void onLights(){
System.out.println("Switch on Lights");
}
public void startEngine(){
System.out.println("Start Engine");
}
}
Within is the same package creating a Car class
package com.vehicle;
/*
* Car class extends the abstract class Vehicle
*/
public class Car extends Vehicle {
public static void main(String args[]){
Car c = new Car();
c.startEngine();
// Only startEngine() can be accessed
}
}
Since an abstract class can contain functionality (as opposed to an interface) it can have private variables or methods.
In your example you might do something like
public void startEngine(){
injectFuel();
igniteSpark();
// etc. my understanding of engines is limited at best
System.out.println("Start Engine");
}
private void injectFuel() {}
private void igniteSpark() {}
That way you can spread some of the work to other methods (so you don't have a 1000 line startEngine method), but you don't want the children to be able to call injectFuel separately since it doesn't make sense outside the context of startEngine (you want to make sure it's only used there).
Or even more you might have a private method that gets called in several other public methods, with different parameters. This way you avoid writing the same code twice or more in each of the public methods, and grouping the common code in a private method makes sure the children don't access it (like they couldn't just call part of the public method before). Something like this:
public void startEngine() {
dishargeBattery(50);
System.out.println("Start Engine");
}
public void startRadio() {
dischargeBattery(20);
}
private void dischargeBattery(int value) {
battery.energy -= value; //battery should probably be a private field.
}
This way your methods can have access to the battery, but the children shouldn't mess with it, and you don't write the same line (battery.energy -= value) in both of them. Take note though, that these are very simple examples, but if dischargeBattery was a 500 line method, writing it in both the other methods would be a hassle.
It's the same as in a non-abstract class, there's no difference.
Which means that if nothing in your abstract class calls the private method, then you can just as well remove it, as it won't be called (baring some evil reflection work).
Usually, private methods are only used as internal utility methods that have a very specific task that the other methods in the class use to do their work.
I know it is not a good coding
practice to declare a method as
private in an abstract class.
I don't. Where did you get that idea?
what is the scope of it within an abstract class?
The abstract class.
The method can be accessed only from within the abstract class. For example, you could have an abstract class with a public final method that makes use of a private helper method.
package arrayafter;
public abstract class Abstract_Demo {
abstract void display();
private void display1() {
System.out.println("Private Method");
}
final void display2() {
System.out.println("final Method");
display1();
}
public static void display3() {
System.out.println("Static methods");
}
}
package arrayafter;
import java.util.Scanner;
public class Practice extends Abstract_Demo{
public static void main(String[] args) {
Practice pr=new Practice();
pr.display();
pr.display2();
Abstract_Demo.display3();
}
#Override
void display() {
// TODO Auto-generated method stub
System.out.println("Abstract method");
}
}