superclass

how to inherit Constructor from super class to sub class

别说谁变了你拦得住时间么 提交于 2019-11-27 04:28:29
How to inherit the constructor from a super class to a sub class? Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass. Here is an example of how this works: class Foo { Foo(String str) { } } class Bar extends Foo { Bar(String str) { // Here I am explicitly calling the superclass // constructor - since constructors are not inherited // you must chain them like this. super(str); } } Superclass constructor CAN'T be inherited in extended class. Although it can be invoked in extended class

Difference between super() and calling superclass directly

拟墨画扇 提交于 2019-11-27 04:24:17
问题 In Python 2.7 and 3, I use the following method to call a super-class's function: class C(B): def __init__(self): B.__init__(self) I see it's also possible to replace B.__init__(self) with super(B, self).__init__() and in python3 super().__init__() . Are there any advantages or disadvantages to doing this either way? It makes more sense to call it from B directly for me at least, but maybe there's a good reason where super() can only be used when using metaclasses (which I generally avoid).

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

落爺英雄遲暮 提交于 2019-11-27 04:11:35
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? BalusC Use an interface if you want to define a contract . I.e. X must take Y and return Z. It doesn't care how the code is doing that.

Jackson serialization: how to ignore superclass properties

不问归期 提交于 2019-11-27 03:04:02
问题 I want to serialize a POJO class which is not under my control, but want to avoid serializing any of the properties which are coming from the superclass, and not from the final class. Example: public class MyGeneratedRecord extends org.jooq.impl.UpdatableRecordImpl<...>, example.generated.tables.interfaces.IMyGenerated { public void setField1(...); public Integer getField1(); public void setField2(...); public Integer getField2(); ... } You can guess from the example that that this class is

Dynamically change an object's superclass

不羁岁月 提交于 2019-11-27 02:50:48
问题 Is it possible to change an object's superclass at runtime? If so, how? 回答1: a short question, a short answer: yes, isa swizzling What Makes Objective C Dynamic?, page 66 An example: I have a class that handles connections to a REST-API, it is called APIClient. In testing I want to connect to a different server. In the testing target I subclass APIClient #import "ApiClient.h" @interface TestApiClient : ApiClient //… @end @interface TestApiClient () @property (nonatomic, strong, readwrite)

Calling superclass from a subclass constructor in Java

蹲街弑〆低调 提交于 2019-11-27 01:59:31
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 believe this shouldn't matter as I am calling the accessor method that returns it to the field? What you

Java Inheritance - calling superclass method

眉间皱痕 提交于 2019-11-26 22:26:33
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 alphamethod1 logic found in class alpha rather than beta? Can I just use super().alphaMethod1() <- I wonder if

Understanding upper and lower bounds on ? in Java Generics

非 Y 不嫁゛ 提交于 2019-11-26 22:08:25
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<? super MyClass>) means: " printAll will print MyList if it has objects of MyClass or any class which extends

When do you need to explicitly call a superclass constructor?

非 Y 不嫁゛ 提交于 2019-11-26 21:38:08
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 super() . Is the distinction from calling the superclass's default/non-default constructor from the subclass

why does initializing subclasses require calling the super class's same init function?

不问归期 提交于 2019-11-26 21:29:49
问题 I have heard that when you have a subclass, you are supposed to initialize the superclass with the same init function from within the subclass's init. What I mean is that the subclass's init should call [super init] and the subclass's initWithFrame should call [super initWithFrame]. Why is this? Why does calling the super's init from a subclass's initWithFrame result in an infinite loop? If this is required, then does this mean I can't create a new init function within a subclass such as