Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing fields in inherited classes

What's the best way to initialize constants or other fields in inherited classes? I realize there are many syntax errors in this example, but this is the best example to explain clearly what I am trying to do.

public abstract class Animal {
  public abstract const string Name; // #1
  public abstract const bool CanFly;
  public abstract double Price; // price is not const, because it can be modified

  public void Fly() {
    if (!CanFly)
      Debug.Writeln("{0}s can't fly.", Name);
    else
      Debug.Writeln("The {0} flew.", Name);
  }
}

public class Dog : Animal {
  public override const string Name = "Dog"; // #2
  public override const bool CanFly = false;
  public override double Price = 320.0;
}

public class Bird : Animal {
  public override const string Name = "Bird";
  public override const bool CanFly = true;
  public override double Price = 43.0;
}

A few things I'm trying to accomplish:

  • Base classes must assign these 3 fields.
  • Ideally I would want these initialized fields to be together at the top of the class so I can see which constants I assigned to each class and change them whenever needed.
  • The fields Name and CanFly cannot be changed.

I know that you could initialize these fields in a constructor (if you get rid of the const), but then they aren't guaranteed to be assigned. If you have these fields as properties instead and override them, you would still need to initialize the backing field of the property. How would you implement this?

A few syntax errors it complains about:

  • The modifier 'abstract' is not valid on fields. Try using a property instead. (#1)
  • A const field requires a value to be provided (#1)
  • The modifier 'override' is not valid for this item (#2)
like image 420
Senseful Avatar asked Jan 08 '10 23:01

Senseful


People also ask

Do fields have to be initialized when they are declared?

It is only necessary that they be declared and initialized before they are used.

Can you initialize variables in a class Java?

Core Java bootcamp program with Hands on practiceYou can initialize the instance variables of a class using final methods, constructors or, Instance initialization blocks.

How to initialise fields in Java?

The way to initialize class fields is with something called a static initializer. A static initializer is the keyword static followed by code in curly braces. You declare a class field much as you would declare a local variable. The chief difference is that field declarations do not belong to any method.

Can derived class have constructor?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.


1 Answers

If a base class requires a value to be provided by a derived class, the two most common ways are:

Require it in the constructor:

public readonly double Price;

protected BaseClass(double price)
{
    this.Price = price;
}

Derived classes must pass the price into the constructor:

public Derived() : base(32)
{
}

Or, make it an abstract property:

public abstract double Price { get; }

Derived classes must provide some way to return the value (though where they get it is up to the derived class, which in many cases provides more flexibility):

public override double Price
{
    get
    {
        return 32;
    }
}
like image 59
Rex M Avatar answered Oct 06 '22 01:10

Rex M