Inherit instance values from a base class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Inherit instance values from a base class



Suppose you have a class relation like this:


class banana : fruit { }



If there are some public global values already assigned to fruit, is there a way to have a new banana class inherit these existing values?



For example:


class fruit
{
public int price;
public string origins;
}

class banana : fruit
{
public string peelDensity;

fruit (peelDensity p, int pr, string o)
{
peelDensity = p;
price = pr;
o = origins;
}
}



Say an existing fruit instance already has its price, origins and etc assigned. Suppose that is actually common fruit data that applies to a particular banana. How would the banana inherit these values?


price


origins



Is there a better way to do this than needing to supply all the existing values in the constructor?


banana b = new banana(peel, price, origins);





It can't inherit that. You will need to copy them over (factory method maybe?)
– Nkosi
41 mins ago







I am struggling to understand your question. Could you provide a Minimal, Complete, and Verifiable example showing how you are populating the values? Also please specify the results you expect (but aren't getting).
– mjwills
37 mins ago







It's really surprising that you made almost 8k rep without knowing how to format code on here, how can that be?
– DavidG
31 mins ago





@nkosi do you mean i should assign all the existing values in fruit manually in a constructor for banana?
– ina
30 mins ago





There's nothing mean in saying that, it's just a fact. How can you be a member for 8 years with hundreds of posts and not learn the most basic feature of making a post?
– DavidG
28 mins ago




4 Answers
4



There are two common ways to do this, by using a copy constructor and a factory approach.



Copy constructor:


public Banana(Fruit fruit)
{
Price = fruit.Price;
Origin = fruit.Origin;
}

Fruit apple = new Fruit(50, "some origin");
Banana banana = new Banana(apple);



Factory approach:


public static class FruitFactory
{
public static Banana BananaFromFruit(Fruit fruit)
{
return new Banana(fruit.Price, fruit.Origin);
}
}

Fruit apple = new Fruit(50, "some origin");
Banana banana = FruitFactory.BananaFromFruit(apple);



Notice that I corrected the casing of the classes and their properties to follow c# conventions.





Is there a way to automate the Copy Constructor? In my case, my base class has over 20 values that are already assigned.
– ina
1 min ago





I think i want to do something like this public Banana(Fruit fruit){ this = fruit; }
– ina
just now


public Banana(Fruit fruit){ this = fruit; }



As I understand your question I think you're pertaining to this:


This is your base class

public class Fruit{
public double Price { get; set; }
public string Origins { get; set; }

//or you can create a constructor
//one default constructor
public virtual void GetPrice()
{
Price = 69;
}
}



This is your child class:


public class Banana : Fruit{
public string peelDensity { get; set; }


//this is a sample of Default Constructor
public Banana()
:base()
{
Price = 0; //here you're setting default value to banana once it's initialized
}
public override void GetPrice()
{
//base.GetPrice(); //here you get the value from the Parent Class
Price = 100; // here you're setting a value for the price of Banana once you call this method;
}
}



Just to add some tips, since your Fruit class is very general, you can set it as an Abstract Class (collection of abstract methods that you can inherit with its child classes) but there are many ways you can abstract class for, and just set default values to your constructor if needed.





Why would I call GetPrice method if Price is publicly available to me?
– Chetan Ranpariya
3 mins ago



An other way as @JC Borlagdan mentioned is to create a Banana Factory which in turn creates Banana objects that have certain properties preset.
You then have a separate factory for each inheritance that takes care of the default properties.



You can simply google the factory pattern. There are plenty of examples and explanations.



The factory can also be a method and does not have to live in a class.
Here is a small example:


class BananaFactory {

public int price;
public string origins;

Banana CreateBanana() {
return new Banana(){
peelDensity = "SomeValue",
price = price,
origins = origins
};
}
}


var banana = bananaFactoryinstance.CreateBanana();

// banana has price and origins that are set in the factory



You could do something like this:


public class Fruit
{
public int Price { get; set; }
public string Origins { get; set; }
}

public class Banana : Fruit
{
public string peelDensity;

public static Banana AsBanana(Fruit f)
{
return f as Banana ?? new Banana { Price = f.Price, Origins = f.Origins };
}
}



The static method allows to return a Fruit as a Banana. If it's already a Banana being stored in a variable typed as a Fruit, it will be returned as a Banana. If not, a new Banana will be created with the values copied overB.



A Banana can always be assigned to a variable that is declared as a Fruit, but not vice versa. That's one of the major points about Object Oriented development.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results