FAQ Database Discussion Community


Java: using setter of a subclass when creating an object of type Superclass

java,subclass,getter-setter,superclass
I have a superclass Person and two subclasses Man and Woman in subclass Man, I have an instance variable age: public class Man extends Person{ private double age; public final static double MIN_AGE = 0.0; public final static double MAX_AGE = 65.0; //no argument constructor public Man(){ super(); initAge(); //...

Getting clone of superclass

swift,clone,subclass,superclass
Here is my playground snippet: class Box { func clone() -> Box { return Box() // <- how to return superclass here? } } class VBox:Box { } let vb = VBox() let cBox = vb.clone() // now cBox is a Box, not a VBox My clone function returns a...

Relationship between LinearModel & GeneralizedLinearMixedModel classes

matlab,oop,time-series,linear-regression,superclass
Matlab defines LinearModel and GeneralizedLinearMixedModel classes. Browsing the documentation indicates that either (i) one is derived from the other, or (ii) there is automatic conversion. These are complex objects, and I am just starting to explore them, so I apologize if their relationship is obvious, but what exactly is their...

When using the 'Class' datatype, how can I specify the type so I only accept subclass of a specific class?

actionscript-3,class,actionscript,superclass
I've got a method that accepts a parameter of type Class, and I want to only accept classes that extend SuperClass. Right now, all I can figure out to do is this, which does a run-time check on an instance: public function careless(SomeClass:Class):void { var instance:SomeClass = new SomeClass(); if...

How to refer to parent class of base class from sub-sub-base-class static method without specifying base class name

php,subclass,static-methods,superclass,base-class
In PHP, I want to call a static method in the parent of the parent class from the sub-sub-class, without referring to the parent class of the parent class's name (please see the comment in the code below): class Base { public static function helloStatic() { return "Hello base!\n"; }...

Difficulty understanding RubyMonk exercise called “Whodunnit” involving superclasses

ruby,while-loop,superclass
I'm trying to understand a solution to a RubyMonk exercise. I can follow most of the code but not the following line, while not (superclass = klass.superclass).nil? is this creating a variable called 'superclass' to use in the rest of the 'while not loop', and then testing it as !=...

Difference between two types of a super class instantiation in Java [duplicate]

java,object,java-ee,instantiation,superclass
This question already has an answer here: Java: difference between A x = new A() and A x = new B() when B extends A [duplicate] 6 answers I have an abstract super class called Document and two child classes called Magazine and Book. What is the difference between...

Why can't I add extra argument validation in the subclass constructor?

java,validation,constructor,abstract,superclass
I have two classes. The superclass: public abstract class Question(){ public Question(String question, String answer, String... alts){... } And the subclass: public class StringOptionsQuestion extends Question { public StringOptionsQuestion(String question, String answer, String... alts){ if (alts.length == 0){throw new IllegalArgumentException();} //The compiler doesn't like this line. super(question, answer, alts); }}...

What is invoking the super class constructor here?

java,inheritance,constructor,superclass,super
I came across this code while looking for exam prep questions. I don't understand what is invoking the superclass constructor in this code? The output is ---> feline cougar cc THL public class Feline{ public String type = "f "; public Feline(){ System.out.print("feline "); } } - public class Cougar...

Java generics: collection of super of super of some type

java,generics,superclass,super
Consider the following self-contained sample: import java.util.*; class TestApplication { interface Type<C> { Collection<Type<? super C>> getSuperTypes(); } static class Test<C> { private final Type<? super C> mySuperType = get( null ); public Collection<Type<? super C>> getSuperTypes() { Collection<Type<? super C>> superTypes = new ArrayList<>(); superTypes.add( mySuperType ); //directly passing...

Can't reach fields from super class with object

java,oop,object,iteration,superclass
I'm trying to figure out a nice way to iterate and print out the objects inside my ArrayList. Problem is that i can't seem to reach the fields in which the user is typing the info of the objects (such as color and brand). The new instance of an object...

Can a subclass also be a superclass?

java,oop,inheritance,subclass,superclass
Can a subclass also be a superclass of another subclass in Java? Perhaps this is not the best example, but consider the following classes: public class Animal { } public class Dog extends Animal { } public class Cat extends Animal { } public class Siamese extends Cat { }...

Fill a hashmap in superclass from subclass constructor in Java

java,javafx,subclass,abstract,superclass
I have an abstract superclass that is set up like this in JavaFX: abstract class View { protected static HashMap<String, View> viewMap; View(){ viewMap = new HashMap<String, View>(); } public static void addMap(String key, View value){ viewMap.put(key, value); } This super class includes the method: public static VBox goToView (final...

Subclasses and inheritance

android,inheritance,subclass,superclass
Let's have I have 3 classes. The First one is the parent class, The second one is a subclass of the Parent class. And the third class is a extends the 2nd class, which is a subclass itself. Will the 3rd class inherit the methods from the parent class?

Swift: Declaring a constant in a subclass but still have a reference to it in the superclass?

ios,swift,constants,subclass,superclass
Let's say you had the class Apple with several subclasses. class Apple { let color = "" } class redDelicious :Apple { let color = "Red" } class grannySmith :Apple { let color = "Green" } func eatApple (appleToEat: Apple) { if appleToEat.color == "Red" { //do something } else...

How can I access the members of a subclass from a superclass with a different constructor?

c++,inheritance,constructor,subclass,superclass
I have the following class and typedef: class Object { protected: long int id; public: Object(void); ~Object(void) {}; long int get_id(void); }; typedef map<string, Object> obj_map; And then I have its child: class Image: public Object { private: path full_path; int x; int y; img image; public: Image(path p, int...

Unexpected output of “this.getClass().getSuperclass()”

java,inheritance,superclass
Consider below two cases Case I I have a simple Java class which has a no-arguments constructor. Below is the code public class TestClassOne { public TestClassOne() { System.out.println("Parent class of TestClassOne is :" + this.getClass().getSuperclass()); } } Object is the super class of all Java classes. So when I...

Android: where in code super class must be calling

java,android,superclass
What is right? @Override protected void onPause() { // myCode super.onPause(); } or @Override protected void onPause() { super.onPause(); // myCode } ...

Output is “NaN”

java,class,superclass
So, I have developed a class that is suppose to be used by another class. The class I developed is as follows: public class Car { private double milesPerGallon; private double gas; //Constructs a car with a given fuel efficiency public Car(double milesPerGallon) { gas = 0.0; } //Increases the...

How does method redirection takes place in super class and base class?

java,superclass,base-class
class HouseHold extends Customer { public void requestCoupon() { Transaction(); CouponCount = 20; } public double Transaction () { Payment += CouponPayment; return Payment; } } class GCustomer extends HouseHold { public double Transaction () { Payment += DisCPayment; return Payment; } } I'm making an object (A) of the...

Passing SuperClass constructor parameters to a SubClass?

inheritance,polymorphism,subclass,superclass
Just beginning to start implementing Super and Sub classes to our projects but I'm having a bit of a problem creating the Subclass constructors to allow for different types of accounts but following the same rules as the Superclass. Here is the constructor error I'm having. http://i.imgur.com/C3n7MxQ.png ...

Ontologies - Do superclasses get instances of their subclasses?

superclass,ontology,topbraid-composer
My question is about superclass inheritance. My understanding says that if I have instances that belong to Class B, then Class A which is the mother class of Class B will also have these instances. For instance: suppose I have a superclass called Car, and two subclass classes Sedan and...

What is the class of keywords like def, alias, and begin in Ruby, if any?

ruby,class,inheritance,keyword,superclass
As far as I understand, everything in Ruby is an object, and every object has a superclass, except BasicObject, which is at the top of the inheritance tree. What is the superclass of keywords like def, begin, and alias?...

Avoid repeating named argument defaults when calling superclass initializer in Ruby (2.1+)

ruby,superclass,named-parameters
Say I have parent class whose initializer has an argument with a default value: class Parent attr_reader :foo def initialize(foo: 123) @foo = foo end end I want to create a subclass that has the same default value for foo. I can do this if I repeat the declaration: class...

C++ inheritance, sending a pointer to base class

c++,pointers,inheritance,constructor,superclass
I have a ninjaCreep class that is derived from a class Creep. I want to pass the pointer that I have acquired through the parameters of the derived class to the base class' constructor however I am getting this error: ../ninjacreep.cpp|4|error: no match for ‘operator*’ (operand type is >‘Ogre::SceneManager’)| The...

MATLAB: Set inherited properties to read-only

matlab,oop,inheritance,superclass
Lets say I have a superclass called rectangle: classdef Rectangle properties width height x0 = 0 y0 = 0 angle = 0 end methods function obj = Rectangle(x0, y0, width, height, angle) obj.x0 = x0; obj.y0 = y0; obj.width = width; obj.height = height; obj.angle = angle; end end end...

How to call subclass methods when subclass objects are stored in superclass array?

java,arrays,inheritance,subclass,superclass
{ Ship ships = new Ship(); CargoShip cargoShips = new CargoShip(); CruiseShip cruiseShips = new CruiseShip(); Ship[] allShips = {ships, cargoShips, cruiseShips}; allShips[0].setShipName("Boom"); allShips[0].setYearBuilt("1900"); allShips[1].setShipName("Bang"); allShips[1].setCargoCapaicty(200); allShips[2].setShipName("Bam"); allShips[2].setMaxPassengers(500); for (int i = 0; i < allShips.length; i++) { System.out.println(allShips[i]); } } So the Ship class is the super class while...

Assign super to variable in AS3

actionscript-3,oop,inheritance,superclass
I have this: public class Base { public function whoAmI() { trace("base"); } } public class Extended extends Base { public function Extended() { this.whoAmI() //prints extended super.whoAmI() //prints base var test = super; test.whoAmI() //prints extended } public override function whoAmI() { trace("extended"); } } The problem is when...

Compare Object superclass package with Object package

java,inheritance,package,superclass
I'm trying to check if an object has a superclass in the same package. I made the following example Vehicle.java package myPackage; public class Vehicle { public Vehicle(){} } Car.java package myPackage; public class Car extends Vehicle{ public Car(){ super(); } } Main.java 1 package myPackage; 2 3 public class...

ISA swizzling and calls to `super`

objective-c,superclass,objective-c-runtime,swizzling,isa-swizzling
Assume the following class hierarchy. Class A is publicly declared: @interface A : NSObject + (A)createInstance; - (void)a; @end Class _B is a private subclass of A: @interface _B : A - (void)a; - (void)b; @end Assume objects of class A should only be created using the factory method createInstance,...

How to cast subclass object to superclass object

java,inheritance,casting,subclass,superclass
i have 2 classes, called superclass and subclass, i tried to cast the subclass object to superclass, but its seems does not work when i want to use the subclass object from the superclass. Please help to explain. Thanks. These are the code:- public class superclass { public void displaySuper()...