polymorphism

How fields work in Polymorphism Java? [duplicate]

泪湿孤枕 提交于 2019-12-18 07:09:37
问题 This question already has answers here : Slight confusion regarding overriding where variables are concerned (6 answers) Closed 3 years ago . I was reading Interview Questions about java and found nice example and got confused. Because there is not well/more explanation that could help me to understand this example. Here is the example. public class MainClass { public static void main(String[] args) { Parent p = new Child(); System.out.println(p.getObject().x); } } class Parent { int x = 10;

C++: How do I pass a container of derived classes to a function expecting a container of their base classes?

老子叫甜甜 提交于 2019-12-18 07:06:47
问题 HI! Anyone know how I can make the line " chug(derlist); " in the code below work? #include <iostream> #include <list> using namespace std; class Base { public: virtual void chug() { cout << "Base chug\n"; } }; class Derived : public Base { public: virtual void chug() { cout << "Derived chug\n"; } void foo() { cout << "Derived foo\n"; } }; void chug(list<Base*>& alist) { for (list<Base*>::iterator i = alist.begin(), z = alist.end(); i != z; ++i) (*i)->chug(); } int main() { list<Base*>

How does the mechanism behind the creation of boxed traits work?

孤者浪人 提交于 2019-12-18 06:54:06
问题 I'm having trouble understanding how values of boxed traits come into existence. Consider the following code: trait Fooer { fn foo(&self); } impl Fooer for i32 { fn foo(&self) { println!("Fooer on i32!"); } } fn main() { let a = Box::new(32); // works, creates a Box<i32> let b = Box::<i32>::new(32); // works, creates a Box<i32> let c = Box::<Fooer>::new(32); // doesn't work let d: Box<Fooer> = Box::new(32); // works, creates a Box<Fooer> let e: Box<Fooer> = Box::<i32>::new(32); // works,

Best Practice For List of Polymorphic Objects in C++

只谈情不闲聊 提交于 2019-12-18 06:25:19
问题 What is a common practice for the storage of a list of base class pointers each of which can describe a polymorphic derived class? To elaborate and in the interest of a simple example lets assume that I have a set of classes with the following goals: An abstract base class whose purpose is to enforce a common functionality on its derived classes. A set of derived classes which: can perform a common functionality, are inherently copyable (this is important), and are serializable. Now alongside

C# Generics and polymorphism: an oxymoron?

谁说胖子不能爱 提交于 2019-12-18 03:54:45
问题 I just want to confirm what I've understood about Generics in C#. This has come up in a couple code bases I've worked in where a generic base class is used to create type-safe derived instances. A very simple example of what I'm talking about, public class SomeClass<T> { public virtual void SomeMethod(){ } } public class DeriveFrom :SomeClass<string> { public override void SomeMethod() { base.SomeMethod(); } } The problem comes up when I then want to use derived instances in a polymorphic way

C# Generics and polymorphism: an oxymoron?

一曲冷凌霜 提交于 2019-12-18 03:54:15
问题 I just want to confirm what I've understood about Generics in C#. This has come up in a couple code bases I've worked in where a generic base class is used to create type-safe derived instances. A very simple example of what I'm talking about, public class SomeClass<T> { public virtual void SomeMethod(){ } } public class DeriveFrom :SomeClass<string> { public override void SomeMethod() { base.SomeMethod(); } } The problem comes up when I then want to use derived instances in a polymorphic way

Can't use virtual and override on the same method in C#

做~自己de王妃 提交于 2019-12-18 02:16:22
问题 So apparently you cannot use the virtual modifier with the override modifier. virtual - a method that can be overridden override - a method that is overriding a method of the same name in its parent's class This leads me to believe that if I override a method in a child class, if that child has a child you can't override that method again. And it is safe to say that if you put override and virtual in a method declaration you will get a compile error in C#. However I can't understand why the

Why use id when we can just use NSObject?

岁酱吖の 提交于 2019-12-17 23:15:29
问题 I know that when we want to create an unknown value object we use id. However, I'm curious that why did Apple to choose id which decides it's value during runtime, when every object is a subclass of NSObject. So instead of id delegate we could have used NSObject *delegate Does anyone know why? Thanks. 回答1: id erases the type and it is equivalent to saying "this object responds to any selector visible to the translation". of course, it is your responsibility to make sure your program is

C++ member function virtual override and overload at the same time

孤街醉人 提交于 2019-12-17 21:55:59
问题 If I have a code like this: struct A { virtual void f(int) {} virtual void f(void*) {} }; struct B : public A { void f(int) {} }; struct C : public B { void f(void*) {} }; int main() { C c; c.f(1); return 0; } I get an error that says that I am trying to do an invalid conversion from int to void*. Why can't compiler figure out that he has to call B::f, since both functions are declared as virtual? After reading jalf's answer I went and reduced it even further. This one does not work as well.

Force base method call

那年仲夏 提交于 2019-12-17 19:56:53
问题 Is there a construct in Java or C# that forces inheriting classes to call the base implementation? You can call super() or base() but is it possible to have it throw a compile-time error if it isn't called? That would be very convenient.. --edit-- I am mainly curious about overriding methods. 回答1: There isn't and shouldn't be anything to do that. The closest thing I can think of off hand if something like having this in the base class: public virtual void BeforeFoo(){} public void Foo() {