polymorphism

Why parent class type reference variable having reference to child class object can't access child class's Methods

爷,独闯天下 提交于 2019-12-11 08:59:29
问题 Since this object(stated in title) can invoke overridden methods in child class, why it can't invoke other methods of child class? I need answer as detailed as possible like memory organization, internal logic in JVM etc. below code will give you clear understanding of my question. class A { int x=10; public A() { System.out.println("Constructor of class A called!!!"); } public void sayGreetings() { System.out.println("accept hye from class A"); } } class C extends A { int x=30;//why this is

Laravel - Polymorphic many-to-many returns empty

对着背影说爱祢 提交于 2019-12-11 08:20:12
问题 I've been at this for 5 hours now and can't seem to figure out the mistake. I'm using Polymorphic Many-to-Many Relations in Laravel 4.1. A Job and an Event each can have a Tag and I've set up the stuff accordingly. But whenever I call $job->tags it returns empty. I have these 3 classes (BaseModel extends Eloquent, namespace is always App\Models, I have to reference them in the e.g. "morphToMany" function, doesn't work with "use"): class Job extends BaseModel { public function tags() { return

Fluent NHibernate Polymorphic Mapping Challenges

纵饮孤独 提交于 2019-12-11 08:17:24
问题 I am having some problems mapping the following scenario in Fluent Nhibernate using Table Per Concrete class: Let's say I have the following class definitions: public class Reading { .... } public class CarReading : Reading { .... } public class TruckReading : Reading { .... } public class Alert { .... public virtual Reading AReading { get; set; } } So my question is how to create the mapping class for Alert, if it has a one to one relationship with reading class (could be either truck

how to extend a abstract class in java

房东的猫 提交于 2019-12-11 07:45:14
问题 I have a made an engine class that is as follows. based on Engine: Interface that extends Java’s Comparable (to compare among engines) and declares an integer getter method “getForce”, which indicatesthat engine subclasses will have a force they are capable of producing. public interface Engine extends Comparable<Engine>{ public int getForce(); } I am trying to make an AbstractEngine class based on the following description. AbstractEngine : Abstract class implemented by most engines. It

C++ Binary Tree Traverse and Function Pointer Parameter

旧巷老猫 提交于 2019-12-11 07:43:59
问题 What I want to do is to traverse the node in order, so that I can print the node in binary tree in order. void inorder_traverse(node* root, function<void(node*)> visit) { if (root == nullptr) return; cout << root->data << ", "; inorder_traverse(root->left, visit); visit(root); inorder_traverse(root->right, visit); } I saw this code for inorder traversing a binary tree. Traversing goes all the nodes so I thought I could print all the data of all visited nodes using traversing function. Would

Field belongs to a class, but how to use polymorphism

江枫思渺然 提交于 2019-12-11 07:35:12
问题 I have a method in a base class class Base { private static string Colour = "blue"; string DoStuff() { return ColourProp; } protected virtual string ColourProp { get{ return Base.Colour; } } } that is called on an instance of this subclass class Sub { private static string Colour = "orange"; protected override string ColourProp { get{ return Sub.Colour; } } } At the moment I'm using virtual properties, is this the only way? (considering that fields cannot be virtual)... 回答1: Yes, you do need

Access member variables of derived class object pointed to by a pointer to its base class

走远了吗. 提交于 2019-12-11 07:25:48
问题 I have one base class and two classes derived from it. I have several virtual functions in the base class so I create a pointer to the base class and use this pointer to access these methods. Now I want to access member variable of derived class (this variable is not present in the base class) using the pointer to base class which points to a derived class object. Can this be done? Should this be done? 回答1: Why do you want to access the derived class' interface? When you use polymorphism, you

Polymorphism AND type safety in parallel inheritance chains

僤鯓⒐⒋嵵緔 提交于 2019-12-11 06:37:00
问题 I have two parallel inheritance chains: Chain1: Animal <- Lion <- Gazelle Chain2: Food <- Meat <- Grass I want to implement the "Eats" polymorphic property on Animal. This is how it looks like: public abstract class Animal { public abstract Food Eats { get; set;} } public class Lion : Animal { public override Food Eats { get { return new Meat();} set { if (value is Meat) DoSomething(value); else throw new Exception("Lions only eat meat. " + "You better learn that, dude!"); } } } However, this

Adding properties to an existing object retrieved using SubSonic

与世无争的帅哥 提交于 2019-12-11 06:36:57
问题 I think this is more of a polymorphism question but it applies to SubSonic table objects... Here's the thing (and I love this one): TblUser userObj = new TblUser(1); Which fills userObj's properties with all of PK=1's goodies. Now, I'd like to add more properties to the existing user object, for example, an ArrayList property of say, account numbers. I've seen questions like this around - "add a property to an existing object...", but in this case, would it be most-recommended to create a

Why does this ploymorphic List property go null when adding additional objects to the list?

风流意气都作罢 提交于 2019-12-11 06:35:36
问题 Consider the following: public class VideoContainer<T> { public string Name { get; set; } //public List<VideoContainer<T>> VideoContainers { get; set; } } public class Perspective : VideoContainer<Perspective> { public List<VideoContainer<SourceContainer>> VideoContainers { get; set; } } I want to ensure VideoContainer<Perspective>.VideoContainers can only contain VideoContainer<SourceContainer> types. I add a new Perspective object to a List<Perspective> with three VideoContainers . The