overriding

Java Inheritance - this keyword

做~自己de王妃 提交于 2019-12-05 08:31:19
I searched online for similar question, but could not find it. So, posting here. In the following program why the value of 'i' is printed as 100? AFAIK 'this' refers to the current object; which in this case is 'TestChild' and the class name is also correctly printed. But why the value of the instance variable is not 200? public class TestParentChild { public static void main(String[] args) { new TestChild().printName(); } } class TestChild extends TestParent{ public int i = 200; } class TestParent{ public int i = 100; public void printName(){ System.err.println(this.getClass().getName());

Should I call the base class implementation when overriding a method in C# for ASP.NET?

心不动则不痛 提交于 2019-12-05 08:10:39
I understand overriding a method/function redefines its implementation in the derived class from its implementation in the base class. Now what confuses me, is if I override a class in ASP.NET such as CreateChildControls() (I picked it randomly for no particular reason), VS2008 auto generates: protected override void CreateChildControls() { base.CreateChildControls(); } Good enough, the default implementation just calls the base class' CreateChildControls() . So if I want to run some code, since I do not know how base.CreateChildControls() , should I do this: protected override void

Can a virtual function be overridden by a non-virtual function?

…衆ロ難τιáo~ 提交于 2019-12-05 08:10:28
In this code: class Base { public: virtual void method() = 0; }; class Derived1 : public Base{ public: virtual void method() override { } }; class Derived2 : public Base{ public: void method() override { } }; Is there any difference between Derived1 and Derived2 ? From section 10.3 Virtual functions of the c++11 standard (draft n3337) point 2: If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and refqualifier (or absence of same) as

Overriding operator new/delete in derived class

谁说胖子不能爱 提交于 2019-12-05 08:09:48
I have a stateless, abstract base class from which various concrete classes inherit. Some of these derived classes are stateless as well. Because many of them are created during a run, I'd like to save memory and overhead by having all stateless derived classes emulate a singleton, by overriding operator new()/delete(). A simplified example would look something like this: #include <memory> struct Base { virtual ~Base() {} protected: Base() {} // prevent concrete Base objects }; struct D1 : public Base { // stateful object--default behavior int dummy; }; struct D2 : public Base { // stateless

Overriding method with generics not working (no method found)

左心房为你撑大大i 提交于 2019-12-05 07:53:21
I am trying to @Override a method in a class that might look like having a complex inheritance structure, but it should actually be quite simple, however I cannot get it to work. public interface Action { } public interface Result { } public interface Player<A extends Action, R extends Result> { default public <P extends Player<A, R>> void onPostAction(final P target, final A action, final R result) { } } abstract public class GesturePlayer<A extends Action, R extends Result> implements Player<A, R> { } abstract public class RPSPlayer extends GesturePlayer<RPSGesture, RPSResult> { } public

Passing swipe touches from UIView to underlying UIScrollView for proper scrolling

爱⌒轻易说出口 提交于 2019-12-05 07:45:59
I have a situation similar to these two posts ( 1907297 AND 689684 ) and to describe my situation most concisely, I present this text/graphical layout (similar to what you'd see in IB, dots used to enforce indent levels) UIView (MainView: 320x460) . .UIScrollView (ScrollView: 320x460) . .UIView (OverlayView: 320x40) . . . .UIButton (ArbitraryButton1) . . . .UILabel (ArbitraryLabel1) . . . .UILabel (ArbitraryLabel2) The goal here is for the OverlayView to serve as a unified, transparent container to position and display some arbitrary buttons/labels on top of the ScrollView. These buttons

Overriding generic function error in swift

痞子三分冷 提交于 2019-12-05 07:44:55
Here's the code: class Test<T> { func foo<S:SequenceType where S.Generator.Element == T>(par : S){ print("foo") } } class TestInh : Test<Int> { override func foo<S:SequenceType where S.Generator.Element == Int>(par : S) { print("loo") } } And it yells such error: repl.swift:8:19: error: method does not override any method from its superclass override func foo<S:SequenceType where S.Generator.Element == Int>(par : S) { ~~~~~~~~ ^ How could I override the method in super class Test<Int> ? ==================additional======================= When it comes to the code blow. class Test<T> { func foo

What is the criteria for throwing exceptions in subclass

橙三吉。 提交于 2019-12-05 07:26:48
What I have known till now is that a subclass if overriding a superclass method should throw the same exception or a subclass of the exception. For example: This is correct class SuperClass { public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{ String signature = "(String, Integer[])"; System.out.println(str + " " + signature); return 1; } } public final class SubClass extends SuperClass { public int doIt(String str, Integer... data) throws ArrayIndexOutOfBoundsException { String signature = "(String, Integer[])"; System.out.println("Overridden: " + str + " " +

How can I override .hgrc options on the command line?

。_饼干妹妹 提交于 2019-12-05 07:17:05
I typically want to ignore white space changes when diff'ing with mercurial. If I set this as a default by putting ignorews = true in my .hgrc's [diff] section then there doesn't seem to be a way to force the display of white space changes for a single invocation on the command line. What am I missing? FWIW: None of the relevant command line options accepts an argument. Using the (deprecated) [defaults] section has the same behavior. I'm assuming the final answer will be "use an alias for ignoring space", but am hopeful something better exists. From the hg(1) man page: --config set/override

Overriding property getters with lazy loading in Objective-C

纵饮孤独 提交于 2019-12-05 07:09:24
I usually lazy instantiate my @property objects in their getter methods like this: @interface MyGenericClass : UIViewController @property(nonatomic, readonly) UIImageView *infoImageView // ... @implementation GenericClass - (UIImageView *)infoImageView { if (!_infoImageView) { _infoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PlaceholderInfoImage"]]; } return _infoImageView; } But when subclassing, I would often like to override some of the @properties to be more subclass specific. So I'd like to change the instantiation and do something like: @interface