I have a simple question.
There is a base class Product.
And derived classes as Bracelet, Earring and Ring.
But ring class has an extra property.
How am I going to reach that size property and use it in a method in below code.
public class Product
{
public int id;
public string title;
}
public class Bracelet : Product
{
}
public class Earring : Product
{
}
public class Ring : Product
{
public int size;
}
Product product;
if(category = 1) // this is a Bracelet
{
product = new Bracelet();
}
else if(category = 2) // this is a Earring
{
product = new Earring();
}
else if(category = 3) // Wola, this is a ring
{
product = new Ring();
product.size = 4; // I cant reach size.. I need to assign size of the ring to decrease stock correctly.
}
product.decreaseStock();
Just declare the value locally first:
This way, you have access to the variable as a
Ringwithin yourifblock, but it also assigns it to the more generalproductvariable too.Alternatively, you can just use initializer syntax: