superclass

ArrayList containing different objects of the same superclass - how to access method of a subclass

夙愿已清 提交于 2019-12-19 17:58:03
问题 Hi I'm wondering if there is a simple solution to my problem, I have an ArrayList : ArrayList <Animal> animalList = new ArrayList<Animal>(); /* I add some objects from subclasses of Animal */ animalList.add(new Reptile()); animalList.add(new Bird()); animalList.add(new Amphibian()); They all implement a method move() - The Bird flies when move() is called. I know I can access common methods and properties of the super class by using this public void feed(Integer animalIndex) { Animal aAnimal

Java — Initializing superclass variables in subclasses?

喜你入骨 提交于 2019-12-18 05:56:26
问题 Okay, so, for example, let's say I have an abstract class called "Vehicle". The Vehicle class, has, among other things, a static variable called wheels, which is not initialized. What I want to do is have other subclasses extending from the Vehicle class, like "Motorcycle", and "Truck", and in these subclasses, have the wheels initialized. Code: public abstract class Vehicle { static int wheels; //number of wheels on the vehicle } But the below doesn't work: public class Motorcycle extends

Is protected method in super class visible in sub class in a different package? [duplicate]

送分小仙女□ 提交于 2019-12-17 21:12:31
问题 This question already has answers here : What is the difference between public, protected, package-private and private in Java? (28 answers) Closed 2 years ago . It seems very silly, but I am really confused. Please see below code: package com.one; public class SuperClass { protected void fun() { System.out.println("base fun"); } } ---- package com.two; import com.one.SuperClass; public class SubClass extends SuperClass{ public void foo() { SuperClass s = new SuperClass(); s.fun(); // Error

Private members in Java inheritance

风流意气都作罢 提交于 2019-12-17 15:45:16
问题 I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members. Can someone explain this to me. I am now totally confused. 回答1: No, the private member are not inherited because the scope of a private member is only limited to the class in which it is defined. Only the public and protected member are inherited. From the Java Documentation, Private Members in a Superclass A subclass does not

Python: RuntimeError: super-class __init__() of %S was never called

醉酒当歌 提交于 2019-12-17 09:58:18
问题 I tried to do some operation ( setParent ) on an object in Python (an instance of a class which inherits from a different class - to be specific, QtGui.QLabel ), but during runtime the above error was raised. The object itself has had some fields with actual content (verified on debug), but from some reason I couldn't "use" it. What does the error mean and how can I fix it? For some additional information, I shall say that the object was returned from a static method before I tried to do this

Python: RuntimeError: super-class __init__() of %S was never called

Deadly 提交于 2019-12-17 09:58:07
问题 I tried to do some operation ( setParent ) on an object in Python (an instance of a class which inherits from a different class - to be specific, QtGui.QLabel ), but during runtime the above error was raised. The object itself has had some fields with actual content (verified on debug), but from some reason I couldn't "use" it. What does the error mean and how can I fix it? For some additional information, I shall say that the object was returned from a static method before I tried to do this

Why call super() in a constructor?

醉酒当歌 提交于 2019-12-17 02:44:33
问题 I'm dealing with a class which extends JFrame . It's not my code and it makes a call to super before it begins constructing the GUI. I'm wondering why this is done since I've always just accessed the methods of the superclass without having to call super(); 回答1: There is an implicit call to super() with no arguments for all classes that have a parent - which is every user defined class in Java - so calling it explicitly is usually not required. However, you may use the call to super() with

Is it possible to access variable of subclass using object of superclass in polymorphism

我们两清 提交于 2019-12-14 03:49:17
问题 How can I access state variable of class KeyBoardPlayer with an object of class KalaPlayer ? /** * An abstract class representing a player in Kala. Extend this class * to make your own players (e.g. human players entering moves at the keyboard * or computer players with programmed strategies for making moves). */ public abstract class KalaPlayer { /** * Method by which a player selects a move. * @param gs The current game state * @return A side pit number in the range 1-6 * @throws

Passing mandatory data to super classes in Kotlin

柔情痞子 提交于 2019-12-14 02:11:44
问题 I've got an issue creating entities in Kotlin. In particular, it's about passing data to a super class. Find an example below. I've got an abstract super class called Trackable that classes can extend. It has a user property that stores who has created that particular object. abstract class Trackable( var createdBy: User ) : Persistable() An class called Contract would now implement the Trackable super class with a compile-time error for the time being: @Entity data class Contract( var

Objective-c: Calling [self message] on a base class is invoking the descendant method

浪子不回头ぞ 提交于 2019-12-13 13:26:17
问题 I just started learning this, so sorry if this has some obvious solution I still can't grasp. I have these two classes: @implementation Person -(void)saySomething:(NSString*) something { NSLog(@"%@",something); } -(void)yellSomething:(NSString*) something { [self saySomething:[something uppercaseString]]; } @end and @implementation ShoutingPerson : Person -(void)saySomething:(NSString*)something { [super yellSomething:something]; } @end This is causing a circular reference call because