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.
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
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.
Is it possible to bind a declared class to a UserControl and will it keep the class as a snapshot or as a running code?
Example: I declare a class named MyClass that has a single property called strTest. I set that value as "What". I have a UserControl on the form that needs access to strTest, so I pass the class to it by declaring a Property in the UserControl Called MyClass and on the main form I send it MyClass.
UserControl.MyClass = MyClass
Now, if I update MyClass on the main form by changing strTest to equal "Where", will the class in UserControl also update to "Where" or will it still have "What"?
Edit, It takes it like a snapshot.
So, how do I data bind a class to a class inside a UserControl (or other form for that matter)?
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.
This question is about OOP in any statically typed language. Suppose I have two classes whose instances maintain pointers/references to each other. In my case, one class is a container, and the other a wrapper around a contained object which maintains a pointer to the container in which it lives.
class Container {
Element[] elements;
}
class Element {
// ... data...
Container holds_me;
}
The constructor of Container creates an Element object to wrap each contained object, and sets their holds_me pointers to itself.
Now I want to inherit from these classes. I want a DerivedContainer class, which inherits from Container and contains DerivedElement objects, where DerivedElement inherits from Element and refers to the containing DerivedContainer object. What is the right way to do this (or is it the wrong thing to do)?
The most straightforward thing is for the constructor of DerivedContainer to create DerivedElements and store them in elements and set their holds_me pointer to itself. Then all the methods of Container and Element will work, but any new methods defined in DerivedContainer and DerivedElement will have to downcast the objects held in elements and holds_me in order to invoke any new methods on them that weren't defined in the base classes. This doesn't seem pretty; so I wonder, is there a better solution?
Yep, this is the right way to do it, without any more information, IMHO. It makes sense if you think that all of the methods in Element can apply to every Element, but the only classes that should know anything about the Derived set of functionality are (ideally only) DerivedElement and (if necessary) DerivedContainer. In other words, to anyone else, Elements and Containers are only Elements and Containers.
You can sometimes do a little better with templates(C++) or generics(Java), since the thought behind these features is that a Container<Element> knows that it holds Elements and a Container<DerivedElement> knows that it holds DerivedElements, but if you have a heterogeneous Container, you really have to have each subclass handle the derived functionality by trying to downcast.
If your language supports it, you could use Generics/Templates. The Container class could have the Elements class as a parameterized type. That way, you can forget about downcasting the Elements.
In case anyone is interested, I've realized that it's possible to do more or less what I originally wanted here using abstract types. Here is some Scala code:
abstract class Container { ctnr =>
type E <: Element
class Element { this : E =>
// data
def holds_me = ctnr
}
var elements : Array[E]
}
The original container is an abstract class containing both a nested class of elements and an abstract type of elements. The subtyping assertion E <: Element requires E to always be a subclass of Element, while the self reference this : E => forces any instantiation of Element to belong to the type E (as instantiated by some implementation of Container).
class ContainerImpl extends Container {
type E = Element
// initialize 'elements' using 'new Element()'
}
abstract class DerivedContainer extends Container {
override type E <: DerivedElement
class DerivedElement extends Element { this : E =>
// more data
}
}
class DerivedContainerImpl extends DerivedContainer {
type E = DerivedElement
// initialize 'elements' using 'new DerivedElement()'
}