polymorphism

Is method hiding a form of Polymorphism?

◇◆丶佛笑我妖孽 提交于 2019-12-19 03:23:01
问题 Polymorphism is the ability to take many forms. Method overriding is runtime polymorphism. My questions are: Is there anything like static polymorphism in Java? Can method hiding be considered a form of polymorphism? In this question's answer, it is said that static methods are not polymorphic. What is the reason for that? 回答1: If we run this test class A { static void x() { System.out.println("A"); } } class B extends A { static void x() { System.out.println("B"); } } class Test { public

Is it ok to cast a STL container with Base type to Derived type?

天涯浪子 提交于 2019-12-19 03:16:59
问题 Is it ok to cast a STL container from Base type to Derived type ? For example, I have two vectors. First one is in type of a Base class, second is in type of a Derive class. class Base { // Code }; class Derive : public Base { // Code }; Usage vector<Base*>* vec_base = new vector<Base*>; // Add some Derive type data to vec_base vector<Derive*>* vec_derive = (vector<Derive*>*)(vec_base); // Using elements as Derive pointers. Works fine. Is this ok ? (It works fine, but I wanted to get some

What's design pattern does Collections.sort use?

别等时光非礼了梦想. 提交于 2019-12-18 21:32:29
问题 When applying a comparator to a list in the following manner, what is the design pattern being used or what is the technique being used here? Collections.sort(myCollection, new Comparator<MyItem>() { @Override public int compare(MyItem item1, MyItem item2) { return item1.getId().compareTo(item2.getId()); } }); 回答1: TL;DR : Collections.sort is an example of a simple polymorphic substitution regardless of whether you use Functional Programming or Object Oriented Programming to make this

Polymorphism and Static Methods

丶灬走出姿态 提交于 2019-12-18 20:04:31
问题 I have a question about this code right here public Car { public static void m1(){ System.out.println("a"); } public void m2(){ System.out.println("b"); } } class Mini extends Car { public static void m1() { System.out.println("c"); } public void m2(){ System.out.println("d"); } public static void main(String args[]) { Car c = new Mini(); c.m1(); c.m2(); } } I know that polymorphism does not work with static methods, only to instance methods. And also that overriding doesn't work for static

Polymorphism and Static Methods

一世执手 提交于 2019-12-18 20:04:02
问题 I have a question about this code right here public Car { public static void m1(){ System.out.println("a"); } public void m2(){ System.out.println("b"); } } class Mini extends Car { public static void m1() { System.out.println("c"); } public void m2(){ System.out.println("d"); } public static void main(String args[]) { Car c = new Mini(); c.m1(); c.m2(); } } I know that polymorphism does not work with static methods, only to instance methods. And also that overriding doesn't work for static

Why do virtual functions need to be passed with a pointer and not by value(of the object)?

試著忘記壹切 提交于 2019-12-18 16:53:20
问题 I think I understand the concept of virtual methods and vtables, but I don't understand why there is a difference between passing the object as a pointer(or reference) and passing it by value (which kind of scraps the vtable or something?) Why would something like this work: Material* m = new Texture; poly->setMaterial(m); // methods from Texture are called if I keep carrying the pointer around And not this?: Material m = Texture(); poly->setMaterial(m); // methods from Material are called if

Polymorphism and forms in Ruby on Rails

限于喜欢 提交于 2019-12-18 13:39:19
问题 I've been full of questions lately, but thanks to this awesome community, I'm learning a ton. I got all the help I needed with polymorphic associations earlier and now I have a question about tackling forms with polymorphic models. For instance I have Phoneable and User, so when I create my form to register a user, I want to be able to assign a few phone numbers to the user (ie: cell, work, home). class User < ActiveRecord::Base has_many :phones, :as => :phoneable end class Phone <

Does a child object lose its unique properties after casting back and forth between a parent class

被刻印的时光 ゝ 提交于 2019-12-18 12:56:14
问题 Consider the following classes: public class Phone { private boolean has3g; public boolean has3g() { return has3g; } public void setHas3g(boolean newVal) { has3g = newVal; } } public class Blackberry extends Phone { private boolean hasKeyboard; public boolean hasKeyboard() { return hasKeyboard; } public void setHasKeyboard(boolean newVal) { hasKeyboard = newVal; } } If I was to create an instance of Blackberry , cast it to a Phone object and then cast it back to Blackberry , would the

Using Rails polymorphism for nested comments

被刻印的时光 ゝ 提交于 2019-12-18 12:44:30
问题 I need to build a nested comments system in a Rails 3 application that allows for comments on many models (articles, posts, etc) and am debating rolling my own solution along the lines of this post. There are gems available like acts_as_commentable_with_threading with awesome_nested_set, but they feel bloated for my needs. I need to be able to add comments to multiple models I need to be able to add comments to comments, infinitely deep I need to be able to efficiently retrieve all

Is there ever a reason to hide inherited members in an interface?

早过忘川 提交于 2019-12-18 12:28:51
问题 I understand that a class which inherits from another class may hide a property by using the new keyword. This, however, is hiding a specific implementation of the property, so I can see how it could be used. Is there any practical reason to hide members in interfaces which implement other interfaces? For example consider the example below. IChildInterface implements IParentInterface , and hides PropertyA . interface IParentInterface { string Name { get; set; } int PropertyA { get; set; } int