this is just a sample code
class parent{ //abstact class
//pure virtual function
virtual fun=0;
}
class child : parent{
fun;
}
main()
{
//what should i do here,so i can add parent in vector
attach(child);
}
void attach(parent* p){
vector.push_back(p); //want to add reference of parent into vecotr
}
and i want to cast child into parent but not able
to do please any one help me?
The child instance has the type parent (and child). If you have an instance of child, there is no extra instance of parent lying around. You can use a child instance wherever a parent instance is required. There is no need to cast.
Class cast excetion :
Occurs when u try to cast a parent class into child class.
Reason: the parent class has not everything that a child class has, on the other hand a child has everything that a parent has so you can cast a child into parent.
In other words, the instance that you want to downcast must be an instance of the class that to which you are downcasting.
Related
So I want to modify a variable which is not directly in parent class but is in a class which is instantiated in parent class. for eg.
class cfg;
int a
endclass
class parent;
cfg cfg1
endclass
class child extends parent;
<how to change variable "a" here which is declared in cfg?>
endclass
Your first problem is using the terms parent and child with respect to inheritance. Those terms imply two separate objects. What you have is a base class type and an derived class type. When constructing a derived class object, you have access to everything in the base class as if it were all in the same class.
class cfg;
int a
endclass
class base;
cfg cfg1
endclass
class derived extends base;
//<how to change variable "a" here which is declared in cfg?>
//Answer: Just use cfg1.a
endclass
I have a child class extends parent class. I am writing child class objects to the orcfile. I have child class objects in a List. I am looping through the list and writing to orc file. The child class object has parent class variables also.
Class objectClass = Child.class
ObjectInspector inspector = ObjectInspectorFactory.getReflectionObjectInspector(objectClass, ObjectInspectorFactory.ObjectInspectorOptions.JAVA);
orcFileWriter = OrcFile.createWriter(fs, new Path(partPath), config, inspector, 67108864, CompressionKind.ZLIB, 10, 0);
I have observed the NULLvalues for the parent class variables. When I verify the child class object, I can see values for the parent class variables.
My understanding is that we can't override the member variable but when i am running the below program, i am getting unexpected o/p
class Parent {
String message = "parent";
void say() {
System.out.println(message);
}
}
class Child extends Parent {
String message = "child";
}
public class Test {
public static void main(String[] args) {
new Child().say();
}
}
In the o/p i am getting "parent" While we are calling the say method using child object and even there is no Parent reference.
Can anybody help me to understand it.
Thanks
The "say" method is on the parent class, not on the child. So when it call for the "message" member it looks at his own, not at the child's one. The fact that the call is make through a child class has nothing to do in it.
Indeed, the member variable is not overriden here. This is expected behavior.
EDIT :
The Java Language Specification says that "If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class."
"Inherit" does not mean "copy". When you call the "say" method on a child instance, is not a "code copy" of the method that is called, but the method of the parent class, as it is defined in the parent class. And the parent class know nothing about the child variable member.
your child class is extend parent class features! so child class has say method by default and it's value (parent) because parent class say method will called when you call say method from child object. if you want to print "child" instead, you need override your say method in the child class and change that default feature, which extends from parent class.
I was coding a very simple case with 2 classes: Parent and Child. Parent has n children, and children has 1 Parent. I set up a bidirectional relationship between them.
I was trying to add a business rule to my parent, that rule checked for equality between the child's parent and the instance handling the call. It returned false when it should have returned true. So I simplified everything to get to the root of the problem. So I tested the same equality outside the POCO and it returned true:
Parent parent0 = session.Load<Parent>(0);
Child child = session.Load<Child>(0);
bool externalTest = parent0 == child.Parent;
I then coded a method for my Parent to test the exact same thing:
bool internalTest = parent0.IsRelated(child);
... Parent Class code
public virtual bool IsRelated(Child child)
{
return child.Parent == this;
}
...
And it returns false... I just don't get it. It's the exact same code.
More info:
So to get more info, I modified my test:
Parent parent0 = session.Load<Parent>(0);
Child child = session.Load<Child>(0);
bool externalTest = parent0 == child.Parent;
System.Diagnostics.Debug.WriteLine("outside parent: " + externalTest);
System.Diagnostics.Debug.WriteLine("Number of parent instances before call to IsRelated:" + Parent.NumberOfInstances);
parent0.IsRelated(child, parent0);
System.Diagnostics.Debug.WriteLine("Number of parent instances after call to IsRelated:" + Parent.NumberOfInstances);
... Parent Class code
public virtual void IsRelated(Child child, Parent sameAsThis)
{
bool internalTest = child.Parent == this;
System.Diagnostics.Debug.WriteLine("inside parent:" + internalTest);
bool sameTest = sameAsThis == this;
System.Diagnostics.Debug.WriteLine("this should equal sameAsThis:" + sameTest);
}
...
I passed the parent instance directly to itself and verify it was the same instance. Well it's not, I get another instance created when I enter the IsRelatedMethod.
Here are my test results:
outside parent: True
Number of parent instances before call to IsRelated:1
inside parent:False
this should equal sameAsThis:False
Number of parent instances after call to IsRelated:2
What am I doing wrong ?
For detailed mapping files and pocos, see (http://stackoverflow.com/questions/13253459/relationships-fixup-in-entityframework-vs-nhibernate)
This is because of the proxy that Nhibernate uses to lazy load entities. In your case the child's parent is a proxy instance.
To solve you problem simply change .Load to .Get
.load does not actually hit the database and populate the entity. See this blog post by Ayende for more info
If I have two entities, Parent and Child, Parent needs to know about all of its Child children, and every Child instance needs to know about its parent Parent instance, how do I do this properly (in terms of DDD etc)?
The easy way would be to do parent.addChild(new Child(parent)), but this seems ugly - as well as:
parent.addChild(new Child()); // Then call some setParent method on child, which needs to be public
Do I need to use a factory here? And if so, how?
Thanks
One option would be to have a relevant instance method on the Parent class. A static Factory is probably not necessary here since you are operating on existing objects and simply need to connect object with another.
public class Parent {
// ...
public Child createChild() {
Child c = new Child(this);
this.addChild(c);
return c;
}
protected void addChild(c) {
// ...
}
// ...
}
public class Child {
public Child(Parent p) {
// ...
this.addParent(p);
}
protected addParent(Parent p) {
// ...
}
}
If the Child constructor needs arguments, you can pass those to the createChild method.
You are not limited to a single one approach. You should use the approach that fits you.
In SWT the child is linked to parent in constructor:
new Label(parentComposite, SWT.NONE);
After that parentComposite knows its child.
Note: SWT requires parent on creation which limits some functionality - you can't create child without specifying a child. This is limitation of the SWT.
In Swing you can create child widget and then add it to the parent.
Those above just the examples. Your personal solution will be based on your needs.
I would consider to use less methods, more consistency (don't leave your childs unlinked from parent)
Talking about code I would use following method:
Parent {
addChild(Child child) {
children.add(child);
child.setParent(this);
}
}
Hope that helps. Happy designing!
How about to do something like this:
public class Child {
Child (Parent parent) {
...
this.parent = parent;
parent.addChild(this);
}
}
So you can set parent to Child only while creating child.