superclass

When to implement an interface and when to extend a superclass?

筅森魡賤 提交于 2019-11-26 11:06:44
问题 I\'ve been reading a lot about interfaces and class inheritance in Java, and I know how to do both and I think I have a good feel for both. But it seems that nobody ever really compares the two side by side and explains when and why you would want to use one or the other. I have not found a lot of times when implementing an interface would be a better system than extending a superclass. So when do you implement an interface and when do you extend a superclass? 回答1: Use an interface if you

Understanding upper and lower bounds on ? in Java Generics

百般思念 提交于 2019-11-26 08:59:54
问题 I am really having a tough time understanding the wild card parameter. I have a few questions regarding that. ? as a type parameter can only be used in methods. eg: printAll(MyList<? extends Serializable>) I cannot define classes with ? as type parameter. I understand the upper bound on ? . printAll(MyList<? extends Serializable>) means: \" printAll will print MyList if it has objects that implement the Serialzable interface. \" I have a bit of an issue with the super . printAll(MyList<?

Calling superclass from a subclass constructor in Java

為{幸葍}努か 提交于 2019-11-26 08:25:14
问题 I am trying to create a constructor that takes a field as a parameter, then puts it in a field that is stored in a superclass. Here is the code I am using public crisps(String flavour, int quantity) { this.flavour = super.getFlavour(); this.quantity = quantity; } In the superclass I have initialised the field with private String flavour; and I have an accessor method public String getFlavour() { return flavour; } I am getting an error \" flavour has private access in the superclass \", but I

Java Inheritance - calling superclass method

落花浮王杯 提交于 2019-11-26 08:21:44
问题 Lets suppose I have the following two classes public class alpha { public alpha(){ //some logic } public void alphaMethod1(){ //some logic } } public class beta extends alpha { public beta(){ //some logic } public void alphaMethod1(){ //some logic } } public class Test extends beta { public static void main(String[] args) { beta obj = new beta(); obj.alphaMethod1();// Here I want to call the method from class alpha. } } If I initiate a new object of type beta, how can I execute the

When do you need to explicitly call a superclass constructor?

大兔子大兔子 提交于 2019-11-26 07:24:52
问题 So say I have a subclass that extends a superclass. In what scenarios do I need to explicitly type super() to get the superclass constructor to run? I\'m looking at an example in a book about abstract classes and when they extend it with a non-abstract subclass, the subclass\'s default constructor is blank and there\'s a comment that says the superclass\'s default constructor will be called. At the same time I\'ve also seen instances on here where someone\'s problem was not explicitly calling

Why aren&#39;t superclass __init__ methods automatically invoked?

感情迁移 提交于 2019-11-26 04:59:07
问题 Why did the Python designers decide that subclasses\' __init__() methods don\'t automatically call the __init__() methods of their superclasses, as in some other languages? Is the Pythonic and recommended idiom really like the following? class Superclass(object): def __init__(self): print \'Do something\' class Subclass(Superclass): def __init__(self): super(Subclass, self).__init__() print \'Do something else\' 回答1: The crucial distinction between Python's __init__ and those other languages

Use of Java [Interfaces / Abstract classes] [duplicate]

江枫思渺然 提交于 2019-11-26 04:47:40
问题 This question already has an answer here: Interface vs Abstract Class (general OO) 34 answers Lately i decided to take a look at Java so i am still pretty new to it and also to the approach of OO programming, so i wanted to get some things straight before learning more, (i guess it\'s never to soon to start with good practices). I am programming a little 2D game for now but i think my question applies to any non trivial project. For the simplicity I\'ll provide examples from my game. I have

How to call a superclass method using Java reflection

大憨熊 提交于 2019-11-26 03:56:58
问题 I have two classes. public class A { public Object method() {...} } public class B extends A { @Override public Object method() {...} } I have an instance of B. How do I call A.method() from b? Basically, the same effect as calling super.method() from B. B b = new B(); Class<?> superclass = b.getClass().getSuperclass(); Method method = superclass.getMethod(\"method\", ArrayUtils.EMPTY_CLASS_ARRAY); Object value = method.invoke(obj, ArrayUtils.EMPTY_OBJECT_ARRAY); But the above code will still

&#39;Must Override a Superclass Method&#39; Errors after importing a project into Eclipse

走远了吗. 提交于 2019-11-26 03:16:24
问题 Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or changed the location of the projects), almost all of my overridden methods are not formatted correctly, causing the error: The method must override a superclass method It may be noteworthy to mention this is with Android projects - for whatever reason, the method argument values are not always populated, so I have to manually populate them myself. For instance: list.setOnCreateContextMenuListener(new

Is it possible to call subclasses&#39; methods on a superclass object?

不问归期 提交于 2019-11-26 02:58:13
问题 Animal is a superclass of Dog and Dog has a method called bark public void bark() { System.out.println(\"woof\"); } Consider the following: Animal a = new Dog(); if (a instanceof Dog){ a.bark(); } What will happen? the assignment isn\'t allowed the call to bark is allowed and \"woof\" is printed at run time the call to bark is allowed but nothing is printed the call to bark causes a compile time error the call to bark results in a run time error I said 2 as we are checking if the object is a