inheritance

why can't I call a subclass method using a reference of a parent type that refers to an instance of a sub-type?

℡╲_俬逩灬. 提交于 2021-02-16 20:22:45
问题 I am Learning Java, while trying to understand inheritance. I couldn't figure out why the overridden method at the subclass walk() executed but not the other xyz() method. class Person{ public void walk() { System.out.println("walking like a person "); } } public class Soldier extends Person{ @override public void walk() { System.out.println("marching like a soldier "); } public void xyz() { System.out.println("xyzng like a pro"); } public static void main(String[] args) { Person sp = new

How to override generic method with derived type in c#

馋奶兔 提交于 2021-02-16 18:42:09
问题 I have the following classes: public interface IService { void ApplyChanges<T>(T parameters) where T : ParamBase; } public class ServiceBase : IService { public virtual void ApplyChanges<T>(T parameters) where T : ParamBase { } } public abstract class Service : ServiceBase { public override void ApplyChanges<T>(T parameters) where T : ParamBase { Console.WriteLine(parameters.Param2); //Apply changes logic here... } } public abstract class ParamBase { public string Param1 { get; set; } }

Prohibit addition of new methods to a Python child class

给你一囗甜甜゛ 提交于 2021-02-16 18:06:53
问题 I have two classes that are supposed to implement the same test cases for two independent libraries (let's call them LibA and LibB ). So far I define the test methods to be implemented in an abstract base class which ensures that both test classes implement all desired tests: from abc import ABC, abstractmethod class MyTests(ABC): @abstractmethod def test_foo(self): pass class TestsA(MyTests): def test_foo(self): pass class TestsB(MyTests): def test_foo(self): pass This works as expected, but

How to fix the error: " AlphaSorter must implement the inherited abstract method java.lang.Comparable<AlphaSorter>.compareTo(AlphaSorter)?

时光总嘲笑我的痴心妄想 提交于 2021-02-16 15:39:05
问题 I have been asking questions about creating a sorting method to sort a linked list of contact names read in from a text file and have advanced from my previous question:(What is a better method to sort strings alphabetically in a linked list that is reading in lines from a text file?), but am now running into two errors after creating a separate class called AlphaSorter.java with the method compareTo() which is intended to override my sort() method in my Linked List program/class called

Override initial value of a member variable of base class

二次信任 提交于 2021-02-15 05:16:41
问题 I am very confused with the inheritance right now. I planned to simply override the initial value of a variable. In the following code i just inherit the base class and try to get it's name, which is saved as a string withing the class. I expected that the derived class can override this value, however it does not do it. My expected out put was Derived Derived However i get Base Base What is the correct way to implement the following? #include <iostream> #include <string> struct Base {

Override initial value of a member variable of base class

╄→尐↘猪︶ㄣ 提交于 2021-02-15 05:14:32
问题 I am very confused with the inheritance right now. I planned to simply override the initial value of a variable. In the following code i just inherit the base class and try to get it's name, which is saved as a string withing the class. I expected that the derived class can override this value, however it does not do it. My expected out put was Derived Derived However i get Base Base What is the correct way to implement the following? #include <iostream> #include <string> struct Base {

Should I use inherited tests?

大城市里の小女人 提交于 2021-02-11 14:46:30
问题 I am reviewing some code where the developer has some classes ClassA and ClassB. They both inherit from ParentClass so both must implement a number of abstract methods (e.g. return5Values(2) ) In ClassA the values are all double the previous value: [2,4,8,16,32] In ClassB the values are all +1 the previous value [2,3,4,5,6] There are also other constraints such as raising an error if the parameter is negative etc. Other tests like getting the 3rd value only, also exist etc. (Obviously these

Should I use inherited tests?

霸气de小男生 提交于 2021-02-11 14:42:36
问题 I am reviewing some code where the developer has some classes ClassA and ClassB. They both inherit from ParentClass so both must implement a number of abstract methods (e.g. return5Values(2) ) In ClassA the values are all double the previous value: [2,4,8,16,32] In ClassB the values are all +1 the previous value [2,3,4,5,6] There are also other constraints such as raising an error if the parameter is negative etc. Other tests like getting the 3rd value only, also exist etc. (Obviously these

c++ : move assignement operator and inheritance

夙愿已清 提交于 2021-02-11 14:17:12
问题 This code compiles and runs fine: #include <iostream> class Base { public: Base(int value) : clean_(true) { value_ = new int; *value_ = value; } ~Base() { if(clean_) delete value_; } Base(Base&& other) noexcept : value_{std::move(other.value_)}, clean_(true) { other.clean_=false; } Base& operator=(Base&& other) noexcept { value_ = std::move(other.value_); other.clean_=false; clean_=true; } void print() { std::cout << value_ << " : " << *value_ << std::endl; } int* value_; bool clean_; };

Scala Traits and Inheritance

大兔子大兔子 提交于 2021-02-11 13:37:55
问题 I have my Scala classes structured as below: trait ActualClass extends ParentClass { override def method1(inputStr:String):String = { "actual "+ inputStr } def execute():String = { this.method1("test") } } trait WithClass extends ParentClass { override def method1(inputStr:String):String = { "with "+ inputStr } } class ParentClass { def method1(inputStr:String):String = { "parent "+ inputStr } } object TestClass extends App{ val actualClass = new ActualClass with WithClass { } println