member-hiding

Call hidden (non virtual) method of derived class from base

☆樱花仙子☆ 提交于 2020-01-24 19:25:48
问题 Is there a way (templates, macros anything else) to substitute a call to hidden_in_derived from common method at compile time , so that instance of Derived calls it's own hidden_in_derived ( without making hidden_in_derived virtual in Base) ? #include <iostream> class Base { public: void common() { // some calls to other methods hidden_in_derived(); // yet some calls to other methods } void hidden_in_derived() { std::cout << "A" << std::endl; } }; class Derived : public Base { public: void

Call hidden (non virtual) method of derived class from base

旧城冷巷雨未停 提交于 2020-01-24 19:25:06
问题 Is there a way (templates, macros anything else) to substitute a call to hidden_in_derived from common method at compile time , so that instance of Derived calls it's own hidden_in_derived ( without making hidden_in_derived virtual in Base) ? #include <iostream> class Base { public: void common() { // some calls to other methods hidden_in_derived(); // yet some calls to other methods } void hidden_in_derived() { std::cout << "A" << std::endl; } }; class Derived : public Base { public: void

Exact difference between overriding and hiding

匆匆过客 提交于 2019-12-17 23:31:13
问题 Can anybody tell the working of overriding and hiding in terms of memory and references. class A { public virtual void Test1() { //Impl 1} public virtual void Test2() { //Impl 2} } class B : A { public override void Test1() { //Impl 3} public new void Test2() { Impl 4} } static Main() { A aa=new B() //This will give memory to B aa.Test1(); //What happens in terms of memory when this executes aa.Test2(); //-----------------------SAME------------------------ } Here memory is with class B but in

Hiding Fields in Java Inheritance

我怕爱的太早我们不能终老 提交于 2019-12-17 18:56:03
问题 Within a class, a field that has the same name as a field in the superclass hides the superclass's field. public class Test { public static void main(String[] args) { Father father = new Son(); System.out.println(father.i); //why 1? System.out.println(father.getI()); //2 System.out.println(father.j); //why 10? System.out.println(father.getJ()); //why 10? System.out.println(); Son son = new Son(); System.out.println(son.i); //2 System.out.println(son.getI()); //2 System.out.println(son.j); /

Is hiding a member in the base class a bad smell in the code?

爷,独闯天下 提交于 2019-12-11 01:59:50
问题 To me, it sounds more like an exception to hide a member in the base class in order to hack something instead of refactoring a bad design... Maybe I am wrong. But... Is it a code smell to hide a member in the base class with 'new' keyword? 回答1: It depends on the intent. If the purpose is to clarify the concrete return type, for example, then it might not be unreasonable. The classic example here is DbConnection . It is useful that SqlConnection re-declares CreateCommand etc, by returning

Java field hiding

泄露秘密 提交于 2019-12-04 01:42:55
问题 In the following scenario: class Person{ public int ID; } class Student extends Person{ public int ID; } Student "hides ID field of person. if we wanted to represent the following in the memory: Student john = new Student(); would john object have two SEPARATE memory locations for storint Person.ID and its own? 回答1: Correct. Every class in your example has its own int ID id field. You can read or assign values in this way from the sub classes: super.ID = ... ; // when it is the direct sub

Why is the output different in the two cases?

假如想象 提交于 2019-12-02 01:59:44
问题 Why is the output different in the below case even when, the variable has been overridden? public class A { int a = 500; void get() { System.out.println("a is " + this.a); } } public class B extends A { int a = 144; } public class mainmethod { public static void main(String args[]) { B ob = new B(); System.out.println("a is " + ob.a); ob.get(); } } 回答1: There is no such thing as overridden variables. B actually has two instance variables named a : one it declares and another it inherits. See

Why is the output different in the two cases?

蹲街弑〆低调 提交于 2019-12-02 00:27:32
Why is the output different in the below case even when, the variable has been overridden? public class A { int a = 500; void get() { System.out.println("a is " + this.a); } } public class B extends A { int a = 144; } public class mainmethod { public static void main(String args[]) { B ob = new B(); System.out.println("a is " + ob.a); ob.get(); } } There is no such thing as overridden variables. B actually has two instance variables named a : one it declares and another it inherits. See this: B ob = new B(); System.out.println("B.a is " + ob.a); System.out.println("A.a is " + ((A)ob).a);

Java field hiding

青春壹個敷衍的年華 提交于 2019-12-01 08:33:44
In the following scenario: class Person{ public int ID; } class Student extends Person{ public int ID; } Student "hides ID field of person. if we wanted to represent the following in the memory: Student john = new Student(); would john object have two SEPARATE memory locations for storint Person.ID and its own? Correct. Every class in your example has its own int ID id field. You can read or assign values in this way from the sub classes: super.ID = ... ; // when it is the direct sub class ((Person) this).ID = ... ; // when the class hierarchy is not one level only Or externally (when they are

Redeclaring members in an extension hides the original member *sometimes*. Why?

本小妞迷上赌 提交于 2019-11-30 05:30:17
问题 By chance, I discovered that you can do this without the compiler complaining: extension Date { var timeIntervalSinceNow: TimeInterval { return 1000 } } What's weirder, is that this actually evaluates to 1000: Date().timeIntervalSinceNow The extension seems to hide the original member. So I tried to do this with my own class: class A { var a: String { return "A" } } extension A { var a: String { return "a" } } and it fails to compile: "invalid redeclaration of 'a'". I observed that this does