Skip to main content

Questions tagged [base-class]

In Object Oriented Programming, a base class is one from which other classes inherit. For example, a child-class `Male` and another child-class `Female` may both inherit from the base-class `Human`.

Filter by
Sorted by
Tagged with
1715 votes
42 answers
741k views

What is the best way of implementing singleton in Python

This question is not for the discussion of whether or not the singleton design pattern is desirable, is an anti-pattern, or for any religious wars, but to discuss how this pattern is best implemented ...
theheadofabroom's user avatar
839 votes
38 answers
201k views

Interface vs Base class

When should I use an interface and when should I use a base class? Should it always be an interface if I don't want to actually define a base implementation of the methods? If I have a Dog and Cat ...
169 votes
11 answers
283k views

Does delete on a pointer to a subclass call the base class destructor?

I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class (class B. When I'm done with an object of class B, ...
Nick Bolton's user avatar
  • 39.1k
138 votes
8 answers
63k views

C# Class naming convention: Is it BaseClass or ClassBase or AbstractClass

What is the recommended approach to naming base classes? Is it prefixing the type name with "Base" or "Abstract" or would we just suffix it with "Base"? Consider the following: type: ViewModel e.g. ...
Soni Ali's user avatar
  • 18.8k
137 votes
7 answers
98k views

Will the base class constructor be automatically called?

class Person { public int age; public Person() { age = 1; } } class Customer : Person { public Customer() { age += 1; } } Customer customer = new Customer(...
Ivan Li's user avatar
  • 1,900
119 votes
33 answers
163k views

Is it possible to assign a base class object to a derived class reference with an explicit typecast?

Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?. I have tried it and it creates a run-time error.
Maddy.Shik's user avatar
  • 6,747
90 votes
6 answers
13k views

Why is there no universal base class in C++?

From a design point of view, why is that, in C++, there is no mother-of-all base-class, what's usually object in other languages?
salezica's user avatar
  • 76k
79 votes
10 answers
127k views

How to hide an inherited property in a class without modifying the inherited class (base class)?

If i have the following code example: public class ClassBase { public int ID { get; set; } public string Name { get; set; } } public class ClassA : ClassBase { public int JustNumber { ...
Ahmed Magdy's user avatar
  • 6,066
64 votes
9 answers
71k views

Cast base class to derived class python (or more pythonic way of extending classes)

I need to extend the Networkx python package and add a few methods to the Graph class for my particular need The way I thought about doing this is simplying deriving a new class say NewGraph, and ...
zenna's user avatar
  • 9,136
60 votes
7 answers
56k views

how to get derived class name from base class

I have a base class Person and derived classes Manager and Employee. Now, what I would like to know is the object created is Manager or the Employee. The person is given as belows: from Project....
Sadiksha Gautam's user avatar
37 votes
5 answers
8k views

GCC issue: using a member of a base class that depends on a template argument

The following code doesn't compile with gcc, but does with Visual Studio: template <typename T> class A { public: T foo; }; template <typename T> class B: public A <T> { public: ...
Jesse Beder's user avatar
  • 33.7k
36 votes
3 answers
81k views

How to resolve "pure virtual method called"

I understand why this is happening, but I'm stuck trying to resolve it...here is what my code is doing when the error is generated (thus, leading to a crash) when my program exits... pure virtual ...
user869525's user avatar
36 votes
2 answers
20k views

How to avoid error "Constructor on type 'MyType' not found" when inheriting a base class

I have a Visual Studio 2010 Windows Forms app which includes a Form base class that other classes will inherit. The base class' constructor takes a parameter that the child classes will pass to the ...
Jed's user avatar
  • 10.8k
34 votes
1 answer
42k views

conversion to inaccessible base class is not allowed [duplicate]

I define a class B1 and a derived class D1 at first. Then I want to define a reference to B1 and initialize that to the D1 object I just defined. Here comes the error, saying that "conversion to ...
Cheng Lu's user avatar
  • 459
34 votes
9 answers
34k views

.NET: Unable to cast object to interface it implements

I have a class (TabControlH60) that both inherits from a base class (UserControl) and implements an interface (IFrameworkClient). I instantiate the object using the .NET Activator class. With the ...
user193327's user avatar
34 votes
8 answers
22k views

How to call an explicitly implemented interface-method on the base class

I have a situation, where two classes (one deriving from the other) both implement the same interface explicitly: interface I { int M(); } class A : I { int I.M() { return 1; } } class B : A, I ...
M4N's user avatar
  • 96k
28 votes
4 answers
24k views

Why use base class pointers for derived classes

class base{ ..... virtual void function1(); virtual void function2(); }; class derived::public base{ int function1(); int function2(); }; int main() { derived d; base *b =...
Balaji Sridharan's user avatar
28 votes
3 answers
10k views

ExecuteCore() in base class not fired in MVC 4 beta

I have a base controller class: And all my other controller inherits this BaseClass like this All this works great in MVC3 (test again today, it really works) but it seems that the ExecuteCore in ...
BladeLeaf's user avatar
  • 458
28 votes
5 answers
12k views

In .NET, can you use reflection to get all non-inherited methods of a class?

Because of this issue here, I'm trying to write a custom JsonConverter that handles cases where you subclass a list or a collection, then add extra properties to it. As such, one approach would be to ...
Mark A. Donohoe's user avatar
27 votes
10 answers
18k views

What are good candidates for base controller class in ASP.NET MVC?

I've seen a lot of people talk about using base controllers in their ASP.NET MVC projects. The typical examples I've seen do this for logging or maybe CRUD scaffolding. What are some other good uses ...
kenwarner's user avatar
  • 29k
27 votes
2 answers
7k views

Non-testable base class extending PHPUnit_Framework_TestCase

Summary How can I create a base class that extends PHPUnit_Framework_TestCase and use that for subclassing actual test cases, without having the base class itself tested by PHPUnit? Further ...
jgivoni's user avatar
  • 1,635
27 votes
3 answers
15k views

Can you extend the default JsonConverter used in JSON.NET for collections?

I'm trying to write a custom JsonConverter for cases where a person subclasses a list or collection, but then adds extra properties to the subclass (see here). The current implementation of JSON.NET ...
Mark A. Donohoe's user avatar
26 votes
2 answers
38k views

How do derived class constructors work in python?

I have the following base class: class NeuralNetworkBase: def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs): self.inputLayer = numpy.zeros(shape = (numberOfInputs)...
devoured elysium's user avatar
26 votes
2 answers
26k views

How can I polymorphic deserialization Json String using Java and Jackson Library?

I've some classes A, B, C they all inherit from class BaseClass. I've a String json that contains the json representation of the A, B, C or BaseClass. I want to have some way to deserialize this ...
mohamede1945's user avatar
  • 7,180
25 votes
2 answers
13k views

Android: Call super() at the beginning or end of onStart(), onStop(), onDestroy() in activity?

Where in onStart(), onStop(), onDestroy() of an activity do I call super.onStart(), super.onStop(), super.onDestroy() ?
mrd's user avatar
  • 4,631
24 votes
2 answers
52k views

Abstract base class in Dart

I have been programming in Java for nearly two years but I am now more shifting to web programming and thus to Javascript, or in my case to Dart. For a project I'm working on I would like to have ...
Tomasito665's user avatar
  • 1,208
24 votes
2 answers
54k views

How do I call a derived class method from the base class?

I have read several similar questions about this but none seem to solve the problem I am facing. The typical answer is to cast as the derived class but I cannot since I do not know the derived class ...
CramerTV's user avatar
  • 1,406
21 votes
2 answers
4k views

Why is a base class in C# allowed to implement an interface contract without inheriting from it?

I've stumbled upon this "feature" of C# - the base class that implements interface methods does not have to derive from it. Example: public interface IContract { void Func(); } // Note that ...
etarassov's user avatar
  • 329
20 votes
2 answers
621 views

Name lookup differences between g++ and MSVS

Consider this code: #include <iostream> namespace N { class A {}; void f(A a) { std::cout << "N::f\n"; } } void f(int i) { std::cout << "::f\n"; } template <typename T&...
oz1cz's user avatar
  • 5,744
20 votes
5 answers
9k views

Do you have a common base class for Hibernate entities?

Do you have a common base class for Hibernate entities, i.e. a MappedSuperclass with id, version and other common properties? Are there any drawbacks? Example: @MappedSuperclass() public class ...
cretzel's user avatar
  • 20.1k
19 votes
5 answers
26k views

Calling a constructor of the base class from a subclass' constructor body

I was under impression that it's impossible, see for example: Calling the constructor of the base class after some other instructions in C++ But the following program runs and produces two lines of "...
TT_ stands with Russia's user avatar
18 votes
7 answers
52k views

How to Get Base Class Instance from a Derived Class

I don't know if this is possible, but I am trying to get the Base Class instance from a Derived Class. In C#, I can use the base keyword to access properties and methods of the Base Class (of course), ...
OneSource's user avatar
  • 529
18 votes
4 answers
14k views

C# private (hidden) base class

Is it possible to make a C# base class accessible only within the library assembly it's compiled into, while making other subclasses that inherit from it public? For example: using System.IO; class ...
David R Tribble's user avatar
16 votes
2 answers
11k views

Understanding virtual base classes and constructor calls

I'm a bit confused about how virtual base classes work. In particular, I was wondering how the constructor of the base class gets called. I wrote an example to understand it: #include <cstdio>...
pythonic metaphor's user avatar
16 votes
2 answers
26k views

Python inheritance - calling base class methods inside child class?

It baffles me how I can't find a clear explanation of this anywhere. Why and when do you need to call the method of the base class inside the same-name method of the child class? class Child(Base): ...
user1369281's user avatar
14 votes
5 answers
4k views

C++ template duck-typing vs pure virtual base class inheritance

Which are the guidelines for choosing between template duck-typing and pure virtual base class inheritance? Examples: // templates class duck { void sing() { std::cout << "quack\n"; } }; ...
Giovanni Funchal's user avatar
14 votes
1 answer
794 views

Scala: How can I make my immutable classes easier to subclass?

I've recently created an immutable class supporting operations like +, -, etc. that returns a new instance of that class when it is changed. I wanted to make a subclass of that class to add a bit of ...
Dobes Vandermeer's user avatar
14 votes
7 answers
6k views

Compiler warning at C++ template base class

I get a compiler warning, that I don't understand in that context. When I compile the "Child.cpp" from the following code. (Don't wonder: I stripped off my class declarations to the bare minimum, so ...
eike's user avatar
  • 141
13 votes
1 answer
311 views

Why does the compiler select the base class constructor inside the template argument list?

Follow-up question to this one. Basically, in the following code, why does the compiler think that the B inside A<B> in Cs constructor refer to the (inaccessible) constructor of the B base ...
Xeo's user avatar
  • 131k
13 votes
3 answers
36k views

C++: Accessing parent methods and variables

In which way should I access this parent method and parent variable? class Base { public: std::string mWords; Base() { mWords = "blahblahblah" } }; class Foundation { public: Write( std::...
user avatar
12 votes
5 answers
7k views

Call a C++ base class method automatically

I'm trying to implement the command design pattern, but I'm stumbling accross a conceptual problem. Let's say you have a base class and a few subclasses like in the example below: class Command : ...
Dinaiz's user avatar
  • 2,223
12 votes
5 answers
11k views

C# protected members accessed via base class variable [duplicate]

It may seems rather newbie question, but can you explain why method Der.B() cannot access protected Foo via Base class variable? This looks weird to me: public class Base { protected int Foo; } ...
Roman's user avatar
  • 4,581
12 votes
2 answers
18k views

Call derived class method from base class reference

class Material { public: void foo() { cout << "Class Material"; } }; class Unusual_Material : public Material { public: void foo() { cout << "Class Unusual_Material"; } }; int ...
user487100's user avatar
12 votes
5 answers
13k views

C# "Rename" Property in Derived Class

When you read this you'll be awfully tempted to give advice like "this is a bad idea for the following reason..." Bear with me. I know there are other ways to approach this. This question should be ...
bopapa_1979's user avatar
  • 9,147
11 votes
3 answers
14k views

Order of calling base class constructor from derived class initialization list

struct B { int b1, b2; B(int, int); }; struct D : B { int d1, d2; // which is technically better ? D (int i, int j, int k, int l) : B(i,j), d1(k), d2(l) {} // 1st Base // or D (int i, ...
iammilind's user avatar
  • 69.4k
11 votes
4 answers
16k views

c++ casting base class to derived class mess

If I were to create a base class called base and derived classes called derived_1, derived_2 etc... I use a collection of instances of the base class, then when I retrieved an element and tried to use ...
alan2here's user avatar
  • 3,289
10 votes
4 answers
35k views

What is the difference between a simple base class and abstract class?

I was doing a kind of R&D and am confused with the concept of an abstract class. What I know about an abstract class is that it may contain concrete methods, and it may contain virtual methods. ...
peter's user avatar
  • 8,538
10 votes
3 answers
10k views

Is it possible a class to inherit only some(not all) base class members?

Is there a way that a derived class could inherit only a few of all the base class members..in C#? If such maneuver is possible, please provide some example code.
CSharp4eto's user avatar
10 votes
3 answers
7k views

Use selenium webdriver as a baseclass python

I searched for a while for this one and was surprised i couldn't find anything, maybe because it's simple. I've been programming in python for about 3 months doing automated testing with selenium ...
ReckerDan's user avatar
  • 336
10 votes
1 answer
3k views

Angular 2 Base Class Output EventEmitter doesn't get raised or handled

Very simple base class Closer import {EventEmitter, Output} from 'angular2/core'; export class Closer { @Output() closed: EventEmitter<any> = new EventEmitter(); constructor() {} close(...
jkyoutsey's user avatar
  • 2,031

1
2 3 4 5
14