Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the base class being called?

Here's my situation:

interface Icontainer{
 string name();
}

abstract class fuzzyContainer : Icontainer{
  string name(){
   return "Fuzzy Container";
  }
}

class specialContainer: fuzzyContainer{
  string name(){
   return base.name() + " Special Container";
  }
}


Icontainer cont = new SpecialContainer();
cont.name(); // I expected "Fuzzy Container Special Container" as the output.

When I run my code as described above the output is simply "Fuzzy Container". What am i missing here? Is there a better way to get the desired results?

like image 982
IaCoder Avatar asked Apr 30 '10 18:04

IaCoder


People also ask

What is base class called?

A base class is also called parent class or superclass. Derived Class: A class that is created from an existing class. The derived class inherits all members and member functions of a base class.

Why is the base class called when a derived class is created?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

What is known as base class in Java?

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Why do we create base class objects?

One reason for this could be that BaseClass is abstract (BaseClasses often are), you want a BaseClass and need a derived type to initiate an instance and the choice of which derived type should be meaningful to the type of implementation.


3 Answers

If you make name() virtual and then override in specialContainer, you will get the expected behavior.

public interface Icontainer
{
    string name();
}

public abstract class fuzzyContainer : Icontainer
{
    public virtual string name()
    {
        return "Fuzzy Container";
    }
}

public class specialContainer : fuzzyContainer
{
    public override string name()
    {
        return base.name() + " Special Container";
    }
}
like image 79
Matt Greer Avatar answered Sep 28 '22 07:09

Matt Greer


First off, it is helpful when you have questions that you post code that actually compiles. It is difficult to analyze a problem when it is full of missing modifiers and typos; it is hard to know whether the problem is the typo or not.

Once we do the work to fix up your program so that it actually compiles, the compiler emits a warning that tells you that the overloading looks wrong. Since your question is "why is the overloading wrong?" it would probably be a good idea to read the compiler warning that we emitted precisely so that you can analyze this problem.

The problem is that the derived class contains a new method called "name", not an override of the existing method. That's what the warning is trying to tell you.

There are two ways to fix this problem, depending on whether you intended the method to be "new" or "override". If you intended the method to be "override" then make the base implementation virtual and the derived implementation override.

If you intended the method to be "new" and you still want the new method to replace the binding to the interface implementation then use interface reimplementation:

class SpecialContainer: FuzzyContainer, IContainer 
{
  public new string Name()
  { 
    return base.Name() + " Special Container"; 
  } 
} 

Notice the "new" and the fact that we have re-stated that this class implements IContainer. This tells the compiler "disregard the bindings to methods of IContainer deduced from the base class and start over."

like image 23
Eric Lippert Avatar answered Sep 28 '22 06:09

Eric Lippert


This is because you are hiding the method and not overriding it. Without making the base method virtual it isn't possible to override it, but you can "hide" it by creating a method of the same name.

If you had:

SpecialContainer cont = new SpecialContainer();
cont.name();

Then you will also get the output you expected.

Matt's answer also works and may be preferable if you can edit the base class to make the method virtual. By default methods are not overridable unless explicitly marked as such with the virtual keyword

like image 41
Davy8 Avatar answered Sep 28 '22 08:09

Davy8