derived-class

Java: Newbie-ish inheritance question...

Suppose I have a base class B, and a derived class D. I wish to have a method foo() within my base class that returns a new object of whatever type the instance is. So, for example, if I call B.foo() it returns an object of type B, while if I call D.foo() it returns an object of type D; meanwhile, the implementation resides solely in t...

How can I alter the signature of a member function for a derived class in Java

Firstly I'm extending an existing class structure and cannot alter the original, with that caveat: I would like to do this: class a { int val; ... // usual constructor, etc... public int displayAlteredValue(int inp) { return (val*inp); } } class b extends a { ... // usual constructor, etc... public in disp...

C#: Determine derived object type from a base class static method

In a C# program, I have an abstract base class with a static "Create" method. The Create method is used to create an instance of the class and store it locally for later use. Since the base class is abstract, implementation objects will always derive from it. I want to be able to derive an object from the base class, call the static C...

C#: How do I call a static method of a base class from a static method of a derived class?

In C#, I have base class Product and derived class Widget. Product contains a static method MyMethod(). I want to call static method Product.MyMethod() from static method Widget.MyMethod(). I can't use the base keyword, because that only works with instance methods. I can call Product.MyMethod() explicitly, but if I later change Widg...

C#: Pass derived class as parameter

I have a base class that does calculations on image sizes. I'm deriving a class from that and have predefined image sizes that will be used in my code. While what I have works, I have a strong feeling that I'm not doing it properly. Ideally, I'd like to just pass DerviedClass.PreviewSize as the parameter to GetWidth without having to cr...

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

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. ...

Execute a derived constructor before the base constructor in C#

My problem here is that I would like to pass an object to a derived class, but it must be done before the base class constructor, since the base class will immediately call the derived class's Start() method that uses the object. Here's an excerpt from the base class, (renamed from BarcodeScanner for convenience). public abstract class...

Serialization Problem in C# with a derived class

Hello, the problem i encounter seems pretty obvious but i cannot find the flaw on my code, so i guess it might be too obvious for me to see the problem... I'm building a notification framework and for that i'm serializing and deserializing a basic class, from wich all the classes i want to send will derive. the problem is that the cod...

Deriving from a Form class in .NET

Theoretically you can derive from a Form but is it something you should not do? I intuitively think so but I've never heard of any rule like this. Edit: of course I meant some conrete class that already derives from Form. For example if I've got class MyForm : Form, the question is can i derive from MyForm? ...

Deriving Class from Generic T

I have a parameterized hibernate dao that performs basic crud operations, and when parameterized is used as a delegate to fulfil basic crud operations for a given dao. public class HibernateDao <T, ID extends Serializable> implements GenericDao<T, ID> I want to be able to derive Class from T at runtime to create criteria queries in Hi...

Events in Base Classes

Ok, so I have a base class which declares the event StatusTextChanged. My child class, of course cannot directly raise this event. So I wind up with something like this (for simplicity sake): Public MustInherit Class FooBase Public Event StatusTextChanged(ByVal StatusText As String) Protected Sub RaiseStatusTextChangedEvent(By...

Design Pattern for optional functions?

I have a basic class that derived subclasses inherit from, it carries the basic functions that should be the same across all derived classes: class Basic { public: Run() { int input = something->getsomething(); switch(input) { /* Basic functionality */ case 1: doA(); break; case 2: ...

How to partially specialize a class template for all derived types?

I want to partially specialize an existing template that I cannot change (std::tr1::hash) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is implemented in the CRTP base class. If I only want to partially specialize for a the CRTP base...

Access base class fn with same signature from derived class object

Hi, Is it possible to access a base class function which has the same signature as that of a derived class function using a derived class object?. here's a sample of what I'm stating below.. class base1 { public: void test() {cout<<"base1"<<endl;}; }; class der1 : public base1 { public: void test() {cout<<"der1"<<endl;...

derive problem about c++

Why I can't access base class A's a member in class B initialization list? class A { public: explicit A(int a1):a(a1) { } explicit A() { } public: int a; public: virtual int GetA() { return a; } }; class B : public A ...

Is there a better way to call each derived object's method for a base class?

I have a base class where I derive several classes. I have another class that uses all those derived classes in a different way. However, I want to call the Update() method (inherited from the base class) on each derived class. Is there an easy way to do this, or do I have to do something like: dim a As Derived1 a.Update dim b As Deriv...

Mapping a derived class to a table in Linq-to-SQL

I have an abstract base class for audit properties. For brevity say it has one property Public MustInherit Class AbstractAuditableEntity ... Public Property CreatedTime() As DateTimeOffset ... End Class And then my auditable domain objects inherit from this class Public Class Source Inherits AbstractAuditableEntity ...

Why does XMLSerializer take DefaultValue Attribute of base class to serialize

using System.ComponentModel; using System.IO; using System.Xml.Serialization; namespace SerializerTest { static class Program { static void Main() { using (TextWriter textWriter = new StreamWriter("data.xml")) { Data data = new Data(); new XmlSerializer(typeof(Data)).Serialize(textWriter, data); textW...

Using an abstract class to implement a stack of elements of the derived class

I have to do this for a basic C++ lecture at my university, so just to be clear: i would have used the STL if i was allowed to. The Problem: I have a class named "shape3d" from which i derived the classes "cube" and "sphere". Now i have to implement "shape3d_stack", which is meant be able of holding objects of the types "cube" and "sphe...

Creating which derived type of class at runtime.

I've tried to understand some of the posts of similar, but don't quite understand their purposes and thought I'd explain my own... I have a class -- fully defined with code with properties, and methods. Many methods are virtual to be overriden by further derived class. So, I have something like the following Class_Main -- Class_A ...