Questions tagged [inheritance]

18733 questions
1

votes
2

answer
43

Views

Execute parent class method without calling super()

We have the following class structure: class NamePrinter(): def phone(): print(self.name) def email(): print(self.name) class PhoneCaller(NamePrinter): def __init__(self, name, number, mail): self.name = name self.number = number self.mail = mail def phone(self): # here, NamePrinter.phone() should b...
Jivan
1

votes
1

answer
53

Views

java.lang.StackOverflowError between two classes

I'm trying to create a JFrame in one class and adding a JPanel to it in my main class, is this not possible? This is my Main class public class Main { JPanel p; JLabel lbl1; public static void main(String[] args) { new Main(); } Main() { new Window(); JPanel p = new JPanel(); JLabel lbl1 = new JLabe...
Frazyhawk
1

votes
1

answer
38

Views

Private inheritance equivalent in Typescript (Only including or excluding specific class members or properties)

What is the best way to simulate private inheritance in Typescript? Specifically, the chid class wants to hide certain members of a parent class. For example, the expected workaround should achieve the following: Class CustomArray extends from Array, and hides specific members say pop() and shift()...
user3330840
1

votes
2

answer
76

Views

oop Check if return type is parent or child class

Suppose I have this class: public class Parent { public string Name {get; set;} } and this class, which inherits from Parent: public class Child : Parent { public string Toys {get; set;} } In some random class, I have a function that returns Parent: public class SomeClass { public Parent GetPerson()...
Cătălin Rădoi
1

votes
3

answer
24

Views

Read attribute value of derived class

I'm building the app which will work with SQLite and what I want to do now is to get table creation strings for each entity which should be stored with code like this: table_creation_string1 = Book.GetTableCreationString(); table_creation_string2 = Scroll.GetTableCreationString(); Which will allow m...
0

votes
0

answer
19

Views

Cannot initialize object parameter of type 'Parent' with an expression of type 'Derived'

I'm implementing a simple hierarchy: System and Device would be two base classes. A System has Devices. Then I'd have a Network derived from System, which will have Computers. Computer is derived from both System and Device. And finally a Computer has various Devices like CPU, Memory etc. The reason...
Le Nguyen Duy Anh
1

votes
1

answer
15

Views

Implementing subtypes for interface methods Java

I've been going over Generic classes and methods in Java and came across this question in a past paper I tried implementing the interface and classes proposed in the question, including the refuel method, and found no problem with passing the Car argument as a parameter Motorized interface public in...
Will170393
-1

votes
0

answer
24

Views

Create a polymorphic function that sorts shapes by area (parameter is vector of pointers)

BIG EDIT: unfortunately was not showing up in my function sortTheShapes parameter. I had to create multiple classes in order to calculate the area of a shape. I have my base class (Shape) and several derived classes such as circle, rectangle, and triangle. My task now is to create the polymorphic f...
Evan
26

votes
7

answer
2.6k

Views

c++ diamond problem - How to call base method only once

I'm using multiple inheritence in c++ and extending base methods by calling their base explicitly. Assume the following hierarchy: Creature / \ Swimmer Flier \ / Duck Which corresponds to class Creature { public: virtual void print() { std::cout
O. Aroesti
1

votes
0

answer
17

Views

How to fix a value non-assigned to parent field?

I'm creating a module in Odoo 10, which aims to rename bank statements by adding behind the imported file name the month and year which it correspond to. Problem : my custom name can't be assigned to parent's field. Previously, I was renaming all the imported bank statements manually, adding '- YYYY...
BChampin
-1

votes
0

answer
28

Views

Which overridden methods are invoked?

I am having hard time to understand the solution of the given question. I can't understand at each step which of the class' methods are invoked. I tried to make a list for what are a,b,c declared types and actual types then try to chose overridden or overloaded methods but it is complex. class Uppe...
Egeio
1

votes
1

answer
215

Views

Placement new crashing when used with virtual inheritance hierarchy in Visual C++

I am using virtual inheritance with a selection of classes in c++. It is currently crashing on destruction. It seems to compile fine in the online compilers, however when I run in Visual Studio, it crashes. I have a pure virtual base class, which is being inherited virtually by its implementation. I...
marsh
1

votes
1

answer
7.5k

Views

Implementing Interfaces in NodeJS

The NodeJS Documentation states: 'The request object is an instance of IncomingMessage. The request object that's passed in to a handler implements the ReadableStream interface' 'So far we haven't touched on the response object at all, which is an instance of ServerResponse, which is a WritableStrea...
Mohammad Daud Ibrahim
1

votes
2

answer
102

Views

Getting React flow error: module is not a polymorphic type. How to extend a class?

I am trying to extend my existing layout to basically override the method renderHeader() but flow doesn't like it: module ./Layout [1] is not a polymorphic type. how do I fix this? here is a class I am trying to extend (it's not abstract): Layout.js //@flow import * as React from 'react'; import H...
jeff
1

votes
5

answer
84

Views

How to make inheritence in CSS?

I have the following problem: I have a font with a given style in a css class: .font_arial_36 { font-family:Arial; font-size:36px; } And now I have a css that gives me the size of a div in a given situation: .a_div_test { width:300px; max-width:350px; } I want the a_div_test to have the properties o...
felipe
1

votes
2

answer
35

Views

Using alternative super class constructor in child class instantiation

I have a base class with two constructors, and a child class which has one constructor. Is it possible to instantiate a child class using the second base class constructor? Example code: abstract class RuleCondition(rule:Rule, field:String, equal:Boolean, inverted:Boolean) { // alternate constructor...
user826955
1

votes
2

answer
48

Views

How to properly copy fields from derived classes?

let's take the following classes as an example: base class: public class Spell { public int castRange; public Spell Copy() { Spell spell = new Spell(); spell.castRange = this.castRange; return spell; } } derived class: public class ManaSpell : Spell { public int manaCost; public new ManaSpell Copy()...
Gruhlum
1

votes
2

answer
44

Views

Does public static functions of base class become member functions of derived class if base class is inherited publicly?

I am trying to run the following code but getting error. #include template class Base { public : static T& Get() { static T t; return t; } }; class Derived : public Base { private : Derived() {} // friend Base; //un-commenting this will make code work. }; int main(...
kapil
1

votes
2

answer
57

Views

How to sort an abstract class object

I have a base abstract class of Shapes, and 3 derived class of Circle Square and Rectangle. After calculating the areas of all the shapes i'm required to sort them. I am having difficulties using std::sort in the library to sort as my Shapes is an abstract class. Hopefully someone can point me in...
New Pea
1

votes
2

answer
39

Views

Overloading only one specific attribute of __init__

I have a question regarding inheritance / overloading. To understand my problem, I think it is best to first show a piece of code: class A(): def __init__(self): self.var1 = 10 self.var2 = 20 self.var3 = 30 class B(A): def __init__(self): #Bad, because I am probably overloading the whole 'constructo...
Bahlsen
1

votes
2

answer
31

Views

Java - which function is called when inheritance, polymorphism, overloading and overriding are all involved?

I have this question involving inheritance, polymorphism, overloading and overriding. I understand all these terms but not sure how they work here. Here's the code: class A { public String show(D obj) {return 'A and D';} public String show(A obj) {return 'A and A';} } class B extends A { public Stri...
Karan Bijani
1

votes
1

answer
123

Views

C# - Allow Inheritance but Forbid Direct Use of Constructor

I want to allow inheritance, but forbid the direct construction of any of the inherited classes. Instead, I want to force the usage of the custom method New(). The goal is to make sure that every instance of inherited classes is a transparent proxy of itself. In this case, it's not possible to make...
Mr.Yeah
1

votes
2

answer
76

Views

Qt signal-slot duplication code avoidance

I would like to share signals and possibly slot implementations among different classes, but it seems Qt does not allow this. Basically I would like to have something like: class CommonSignals { signals: void mysignal(); }; class A : public QObject, public CommonSignals { Q_OBJECT public: void doS...
Saturnu
1

votes
1

answer
35

Views

child extended class method calls its super version but that still only sees child data

class B extends class A. I'll call A the parent and B the child. Both have constructors. B calls super() inside of its constructor. Both have a method with the same name. Perhaps just by coincidence or mistake both have a 'this.x' variable. There then becomes no way to access the parent's this.x...
user244488
1

votes
1

answer
69

Views

Is it fine to violate Composition Over Inheritance when necessary?

I have a set of classes: // This is #included from another header file // I don't want to inherit this, because it ain't my code class DrawableObject; class Animal { DrawableObject obj; // Does not define run() or swim() }; class Cat : public Animal { void run() { /* Calls obj.setPosition() and othe...
Phil Kang
1

votes
4

answer
296

Views

Retaining arguments passed to superclass constructor

In Scala no methods can be invoked on the current instance when the superclass constructor is called. So there is no opportunity for a method to be invoked which will memorise the value that it is returning, in the current instance. How can I retain the arguments produced to give to a superclass con...
Robin Green
1

votes
3

answer
572

Views

Exception polymorphism c++

I have made my own exception class which derives from runtime_error and is getting an int in the c'tor. I would like to make a base class for this exception, in order to use polymorphism, so I could catch only the base class and basically I would be catching the derived class. So, this is the base c...
Jjang
0

votes
0

answer
19

Views

Override static getter in ES6

Assume I have an ES6 mixin like this: // InputMixin.js export const InputMixin = (base) => { return class Base extends base { static get _attribs() { return ['autofocus', 'autocomplete', 'form', 'formaction', 'formenctype', 'formmethod', 'formnovalidate', 'formtarget', 'name', 'type', 'value'] } sta...
Merc
1

votes
0

answer
112

Views

Can Java recognize a type parameter, T, as a super type of some class?

Background: I have 3 classes: A, B, C where they all extends 'Super' and implements 'Interface' and I want to write code that only uses only the methods from 'Super' and 'Interface' Unfortunately, I don't have control over the source code of A, B, C, 'Super', 'Interface' to change the inheritance ch...
Sgene9
1

votes
0

answer
278

Views

How to remove annotations of a variable in a child class

Let us say that we have a class from API which has some fields, of which some are made mandatory using @NotNull annotation, like below: class SomeAPIClass { private String field1; private String field2; @NotNull public String getField1() { return field1; } public String getField2() { return field2;...
Rupesh
1

votes
0

answer
160

Views

JAVA How does the inheritance work under the hood?

How does the inheritance work under the hood in Java? What does JVM do in order to accompish the inheritance? When the object is created and stored on the heap is it containing the superclass' code? Is that the whole magic?
dBio
1

votes
0

answer
46

Views

How should I call the superclass's custom initialiser, when I use a custom one for my subclass?

I'm learning Objective-C and I don't clearly understand what is the best way to call superclass' custom initializer, while defining one in its subclass. Here is my class' (superclass) custom initializer: -(id) initWithWidth:(int)w andHeight:(int)h { self = [super init]; if (self) { [self setWidth:w...
Tigran Iskandaryan
1

votes
1

answer
14

Views

How can inherited functions with the same name get treated as overloaded functions?

I create a function in class A and class A is a base class sub-object of B class. In class B I create a function with the same name and signature with one in class A. In the function in class B, I want to call the function in class A but it not get overloaded. How can I use it ? class Document{ publ...
Thắng Nguyễn
0

votes
1

answer
21

Views

Trouble understanding the output of a set of Java Objects

There are 4 classes: class Lower extends Middle { private int i; private String name; public Lower(int i){ super(i+1); name = 'Lower'; this.i = i; } public void set(Lower n){ i = n.show();} public int show(){return i;} } class Middle extends Upper { private int j; private String name; public Middle(...
sktsasus
1

votes
0

answer
415

Views

Angular Component view Inheritance

I'm using angular4 since few month now, and I'm trying to figure out, how can I build inheritance between component. So in my exemple I have two component ParentComponent and ChildComponent ParentComponent.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-parent', tem...
Nicolas Pretot
1

votes
1

answer
57

Views

Why Cannot call child class method from object of interface implemented in parent class

public interface WebDriver { default void get() { System.out.println('Opening Url');} void click(); void sendKeys(); } public class RemoteWebDriver implements WebDriver { @Override public void click() { System.out.println('Clicking WebElement'); } @Override public void sendKeys() { System.out.printl...
RArora
1

votes
1

answer
54

Views

How is everything happened when a class's metaclass is a subclass of its subclass's metaclass?

It is about metaclasses in Python2.7. To be clear, the code is like: class MetaC(type): def __new__(cls, name, bases, attrs): print 'MetaC' return super(MetaC, cls).__new__(cls, name, bases, attrs) class MetaB(MetaC): def __new__(cls, name, bases, attrs): print 'MetaB' return super(MetaB, cls).__new...
Yang
1

votes
0

answer
62

Views

Static and dynamic bindning

I have tried to find information on the internet but I have not got a clear picture of it. My question is what affect static and dynamic binding have on encapsulation, inheritance, subtype polymorphism in object orientated programming?
thpthp
1

votes
0

answer
66

Views

How to use an parent instance to replace child object's parent in python

Here is my code: import socket import select class My_socket(socket.socket): def __init__(self, soc = None): if soc == None: socket.socket.__init__(self, socket.AF_INET, socket.SOCK_STREAM) .... else: # how to use soc to initialize or replace parent part .... rs, ws, es = select.select([my_socket_in...
MJ Chen
1

votes
2

answer
280

Views

Doctrine Class Table Inheritance can not remove() entity

I try to implement Doctrine's Class Table Inheritance. My Application needs a User Entity that is used to authenticate the user through Symfony's security system. On top of that, my application needs a special kind of User, a Doctor. Below you can find excerpts of my Entity classes. User class (Bas...
Momomentum

View additional questions