superclass

Why is my superclass calling my subclass method?

 ̄綄美尐妖づ 提交于 2021-02-20 19:33:09
问题 When I call a method that was overrided from my constructor, I am getting an error and it says that it is missing an argument (due to the subclass requiring a second argument). However, I called the method in the super(), so why doesn't it call the super class version of that method? This is best illustrated with a short example: class A: def __init__(self): self.do() def do(self): print("A") class B(A): def __init__(self): super().__init__() self.do("B") def do(self, arg2): super().do()

Java: Extending a class, constructor of subclass gives error

微笑、不失礼 提交于 2021-02-10 09:20:30
问题 A little background: There are three classes involved: Tester(main method) , DNASequence(object) and ProteinDNA(subclass of DNASequence) . All three are under the same package. The constructor for ProteinDNA accepts an object DNASequence and an integer public class ProteinDNA extends DNASequence{ public ProteinDNA(DNASequence dna, int startAt){ //this is the constructor Compiling the class ProteinDNA gives me an error at the constructor. The error in Eclipse is: "Implicit super constructor

Java : Super class array object assigned with sub class array object

拟墨画扇 提交于 2021-02-05 02:53:54
问题 I'm trying to assign a sub class object array to its super class. The program compiles successfully, but I' getting an ArrayStoreException . I know that arrays parent and child are references to same array, but shouldn't I be able to access method func at least? class Pclass { Pclass() { System.out.println("constructor : Parent class"); } public void func() { System.out.println("Parent class"); } } class Cclass extends Pclass { Cclass() { System.out.println("Constructor : Child class"); }

Java : Super class array object assigned with sub class array object

☆樱花仙子☆ 提交于 2021-02-05 02:53:10
问题 I'm trying to assign a sub class object array to its super class. The program compiles successfully, but I' getting an ArrayStoreException . I know that arrays parent and child are references to same array, but shouldn't I be able to access method func at least? class Pclass { Pclass() { System.out.println("constructor : Parent class"); } public void func() { System.out.println("Parent class"); } } class Cclass extends Pclass { Cclass() { System.out.println("Constructor : Child class"); }

Accessing Scanner objects from subclass in a superclass?

怎甘沉沦 提交于 2021-01-28 22:26:00
问题 I am a very new, relatively inexperienced Java programmer. The project I am working is just a test of my current skills, and my goal is to write as efficient a program as possible. In essence, I have three classes: A, B, and C. B extends A and C extends B, but I want a Scanner object in C to be used in a switch statement (part of a larger method) in A. The reason I want this is because I do not want to overload the method in A (copy-pasting the same code with different parameters is not ideal

PHP Call Superclass Factory Method from Subclass Factory Method

让人想犯罪 __ 提交于 2021-01-28 14:22:46
问题 I am writing a php app with subclasses and since I want to have multiple ways to create an object I am doing different factory methods instead of multiple constructors. I have a User with factory methods User::from_id User::from_netid I have several subclasses of User . I was previously calling the parent superconstructor, but when I switched to the factory method that constructor didn't exist. I have Student , a subclass of User . To get it to work, I had to duplicate almost all of my

java initialize base class fields in subclass constructor

非 Y 不嫁゛ 提交于 2021-01-27 13:01:35
问题 This is a very basic question about subclasses in java, I still don't get it... Suppose I have a superclass with three fields and with only the default constructor: public class Superclass { public int a; public int b; public int c; } and I want to add a field x. I cannot change Superclass , so I make a subclass: public class Subclass extends Superclass { public int x; public Subclass(Superclass s) { super(); // what to do?? } } I now want to generate a Subclass object from an existing

Python change self to inherited class

こ雲淡風輕ζ 提交于 2021-01-27 07:32:56
问题 I have this kind of structure: class Foo: def __init__(self, val1): self.val1 = val1 def changeToGoo(self) HOW??? class Goo(Foo): def __init__(self, val1, val2): super(val1) self.val2 = val2 a = Foo(1) a.changeToGoo() 'a' is now an instance of Foo now i would like to change it to be an instance of Goo, by using the method "changeToGoo", and add the other value. How can this be done in Python? I have tried: self.__class__ = Goo but when I check: type(a) it's still Foo, and not Goo. 回答1: In

Why can't classes be used as modules?

倖福魔咒の 提交于 2020-06-12 06:06:43
问题 Module is the superclass of Class : Class.superclass # => Module In OOP, this implies that an instance of Class can be used in every place where an instance of Module can be used. Surprisingly, this is not the case with Class instances in Ruby: class C end c = C.new module M end # Let's do all the extend/include/prepend stuff with M! c.extend M C.include M C.prepend M # All worked fine until this line. # Let's turn to classes now! # First, get a class to work with. class C_as_M end C_as_M

How to define a Doctrine mappedSuperclass using XML or YAML instead of annotation mapping

久未见 提交于 2020-03-05 03:26:57
问题 The following script comes from https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html#mapped-superclasses, and was only changed to include a second sub-class. It is my understanding that MappedSuperclassBase cannot exist by itself but must be extended by one and only one sub-class (i.e. either EntitySubClassOne or EntitySubClassTwo ), and is the same concept as supertype/subtype for SQL. Agree? How is a super/sub type defined using either YAML or XML