inheritance

Is co-variance safe here?

狂风中的少年 提交于 2019-12-24 02:20:53
问题 class Food{} class Meat extends Food{} class Animal{ void feed(Food f){} } class Lion extends Animal{ void feed(Meat m){} } void foo(Animal a){ Food f = new Food(); a.feed(f); } What will happen if we send to foo(new Lion()) ? I know that it will get error but I need the explanation please 回答1: Your Lion can eat Meat , but it can also eat any kind of food (like Spinach). If your Lion could not eat any kind of Food , then it could not be considered an implementation of Animal . This is

Java inherit 2 interfaces with the same method

泪湿孤枕 提交于 2019-12-24 02:11:42
问题 In the following code snippets, first one does not compile, but second one does . Why? What is the difference? 1. public class test { static interface I1 { I1 m(); } static interface I2 { I2 m(); } static interface I12 extends I1,I2 { public I12 m(); } } 2. public class test { static interface I1 { I1 m(); } static interface I2 { I2 m(); } static class I12 implements I1,I2 { public I12 m(){ return null; } } } 回答1: In Java 1.4 or earlier, both snippets should fail to compile. In 1.5 or later,

How does inheritance and polymorphism work in this situation?

試著忘記壹切 提交于 2019-12-24 02:04:10
问题 This is the first class package test; public class Project { public void doSomething (String stuff) { writeStuff(); whichProject(stuff); } public void writeStuff(){ System.out.println("This is stuff"); } public void whichProject(String stuff){ System.out.println("This is a random project " + stuff); } } and this is the derived class package test; public class Project1 extends Project{ public void whichProject(String stuff){ System.out.println("Coding project number one: " + stuff); } public

Calling a method on a List of Generic Type from abstract Parent class

时间秒杀一切 提交于 2019-12-24 01:57:28
问题 Here is my current Type Hierarchy: I am trying to implement a method in PlaneRegion that will call a method named Shift() on a list in its derived classes where the list is called PlaneBoundaries in all of them but they are of different types like so: public abstract class PlaneRegion<T> { public abstract List<T> PlaneBoundaries { get; set; } } public class Polygon : PlaneRegion<LineSegment> { public override List<LineSegment> PlaneBoundaries { get { return _planeBoundaries; } set {

How can I access a static property in a subclass when the property is located in a base class?

痞子三分冷 提交于 2019-12-24 01:55:17
问题 Let's say I have: public class Fruit { public static List<String> Suppliers { get; protected set; } static Fruit() { Suppliers = new List<String>(); Suppliers.Add("Company A"); } } public class Banana : Fruit { static Banana() { Suppliers.Add("Company B"); } } If I just do this in the calling code: foreach(String supplier in Banana.Suppliers) Console.WriteLine(supplier); I get: Company A Whereas if I do: Banana b = new Banana(); foreach(String supplier in Banana.Suppliers) Console.WriteLine

How can I access a static property in a subclass when the property is located in a base class?

╄→гoц情女王★ 提交于 2019-12-24 01:54:57
问题 Let's say I have: public class Fruit { public static List<String> Suppliers { get; protected set; } static Fruit() { Suppliers = new List<String>(); Suppliers.Add("Company A"); } } public class Banana : Fruit { static Banana() { Suppliers.Add("Company B"); } } If I just do this in the calling code: foreach(String supplier in Banana.Suppliers) Console.WriteLine(supplier); I get: Company A Whereas if I do: Banana b = new Banana(); foreach(String supplier in Banana.Suppliers) Console.WriteLine

How do you call a subclass method from a superclass in Java?

六眼飞鱼酱① 提交于 2019-12-24 01:49:22
问题 I've looked around here find an answer to my question and I can't. How do you call a subclass method from a superclass in Java? Basically what I'm looking to do is this: I have a method called exec that takes a String as a parameter for a command. I want to be able to call the exec method in the subclass that the developer has overridden from the superclass without knowing the subclasses name ahead of time. This is like the way the Thread class works. I'm not looking to do what every answer I

How do you call a subclass method from a superclass in Java?

浪子不回头ぞ 提交于 2019-12-24 01:49:10
问题 I've looked around here find an answer to my question and I can't. How do you call a subclass method from a superclass in Java? Basically what I'm looking to do is this: I have a method called exec that takes a String as a parameter for a command. I want to be able to call the exec method in the subclass that the developer has overridden from the superclass without knowing the subclasses name ahead of time. This is like the way the Thread class works. I'm not looking to do what every answer I

VBA inheritance via construction, constructor not working? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-24 01:47:16
问题 This question already has an answer here : Can't seem to use Interfaces with properties in VBA and I can't work out why (1 answer) Closed 2 years ago . I am just getting started using classes in VBA and I'm following the "Inheritance by construction" method outlined here. My example uses a simple class that contains a value (as a variant) and a value type (as a string). I created a subclass in which value type is set to string in the constructor. Here is my code: Interface Class (IVal) 'IVal

C++ Trouble with operator code inheritage: am I require to copy same code for all derived classes?

二次信任 提交于 2019-12-24 01:33:06
问题 I'd like to write a group of class D_I (aka I=1,2,... just I mean '_I' to be class proper name, not integer) which handle some (boolean, for example) operations F with same meaning for all classes. But also I want to operate with "sums" of object of such classes, different classes of the group might be "added" together. Mentioned operation of sum of such object depends on corresponding operation of added objects. That's why I'm going to create common Base class B to handle operation of