Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to create instance of child object from parent object

I'm creating a child object from a parent object. So the scenario is that I have an object and a child object which adds a distance property for scenarios where I want to search. I've chosen to use inheritance as my UI works equivalently with either a search object or a list of objects not the result of a location search. So in this case inheritance seems a sensible choice.

As present I need to generate a new object MyObjectSearch from an instance of MyObject. At present I'm doing this in the constructor manually by setting properties one by one. I could use reflection but this would be slow. Is there a better way of achieving this kind of object enhancement?

Hopefully my code below illustrates the scenario.

public class MyObject {      // Some properties and a location. }  public class MyObjectSearch : MyObject {      public double Distance { get; set; }          public MyObjectSearch(MyObject obj) {          base.Prop1 = obj.Prop1;          base.Prop2 = obj.Prop2;     } } 

And my search function:

public List<MyObjectSearch> DoSearch(Location loc) {    var myObjectSearchList = new List<MyObjectSearch>();            foreach (var object in myObjectList) {        var distance = getDistance();        var myObjectSearch = new MyObjectSearch(object);        myObjectSearch.Distance = distance;        myObjectSearchList.add(myObjectSearch);    }     return myObjectSearchList; } 
like image 352
Captain John Avatar asked Dec 30 '13 15:12

Captain John


People also ask

Can we create child class object with parent reference?

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.

Is a parent object created when a child object is created?

The constructor is called, therefore an object of the parent class is created. That object is used later to build an object of the child class. The reason for this is that an object of the child class is an object of the parent class with more things. Careful: there is no additional instance of parent class created.

Can we assign parent object to child objects in C#?

In real world, parents can accommodate children but children cannot accommodate parents. same is the case in OOP. class Parent { int prop1; int prop2; } class Child : Parent // class Child extends Parent (in case of Java Lang.)


1 Answers

The base class needs to define a copy constructor:

public class MyObject {     protected MyObject(MyObject other)     {         this.Prop1=other.Prop1;         this.Prop2=other.Prop2;     }      public object Prop1 { get; set; }     public object Prop2 { get; set; } }  public class MyObjectSearch : MyObject {      public double Distance { get; set; }      public MyObjectSearch(MyObject obj)          : base(obj)     {         this.Distance=0;     }     public MyObjectSearch(MyObjectSearch other)          : base(other)     {         this.Distance=other.Distance;     } } 

This way the setting of properties is handled for all derived classes by the base class.

like image 54
John Alexiou Avatar answered Oct 02 '22 06:10

John Alexiou