www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inheritance question

reply negerns <negerns2000 gmail.com> writes:
How do I hide A's getname()and use another method name for it? I wanted 
the user of my class B to use the getnewname() instead of using 
getname() which is inherited from class A.

     class A {
       private char[] _name;
       public char[] getname() { return _name; }
       ...
     }

     class B : A {
       public char[] getnewname() { return A.getname(); }
       ...
     }

     void main() {
       B b;
       ...
       writefln(b.getnewname());
     }


-- 
negerns
Aug 20 2007
parent Regan Heath <regan netmail.co.nz> writes:
You could use deprecated like so:

import std.stdio;

class A {
	private char[] _name;
	this(char[] name) { _name = name; }
	public char[] getname() { return _name; }
}

class B : A {
	this(char[] name) { super(name); }
	public char[] getnewname()  { return A.getname(); }
	deprecated char[] getname() { return A.getname(); }
}

void main() {
	B b = new B(cast(char[])"test");
	writefln(b.getname());  //Error: function dep.B.getname is deprecated
	writefln(b.getnewname());
}
Aug 20 2007