how can i call class name from extend class without implements in this class method (i want extend this function from parent class) and without call new class. If i call class name like this:  abstract class foo {      public static function get_name(){          return get_class();          //or          return __CLASS__;     } }  class bar extends foo {}  echo bar::get_name();   i get:  foo   expect:  bar
54427
I have two classes: Action and MyAction. The latter is declared as:  class MyAction extends Action {/* some methods here */}   All I need is method in the Action class (only in it, because there will be a lot of inherited classes, and I don’t want to implement this method in all of them), which will return classname from a static call. Here is what I’m talking about:  Class Action {  function n(){/* something */} }   And when I call it:  MyAction::n(); // it should return "MyAction"   But each declaration in the parent class has access only to the parent class __CLASS__ variable, which has the value “Action”.  Is there any possible way to do this?
33547
If I have an abstract parent class like this:  abstract class Vehicle {     public Vehicle(Person driver) {         //     } }   and then I create a child class like this:  public class MagicCarpet extends Vehicle {     //this won't compile }   I encounter the following Compile Error:     Error: Parent class has no 0-argument constructor for implicit construction at line ... column ...   Obviously I can explicitly re-declare a constructor on the child class toget it to compile:  public class MagicCarpet extends Vehicle {     public MagicCarpet(Person driver) {         super(driver);     } }   But I feel this isn't DRY. Not sure what's actually going on under the covers here.  How can I get a constructor (other than the implicit 0-argument one) to be inherited by a child class?
45128
Lets say I'm building a base class which will be extended upon by the children class. So a base class is called Base and children can be Child1, Child2, etc.  In the base class's constructor, how can i get the value of Child1/Child2?  This is all using PHP
61737
I would like to use Class.newInstance() but the class I am instantiating does not have a nullary constructor. Therefore I need to be able to pass in constructor arguments. Is there a way to do this?
73206
I have two classes:  public class MyBase {     public virtual void DoMe()     {      } }  public class MyDerived:MyBase {     public override void DoMe()     {         throw  new NotImplementedException();     } }   And I have the following code to instantiate MyDerived:          MyDerived myDerived=new MyDerived();   The thing is how to call DoMe of the base class? If I use myDerived.DoMe(), then the derived method wil be called, resulting in an exception. I tried to cast myDerived to MyBase, yet it is still the derived version of the method that gets called.   Edit: As mentioned in the below comment, I can't change eitehr MyDerived or MyBase because they are not my code.
86715
I'm looking at some GXT code for GWT and I ran across this use of Generics that I can't find another example of in the Java tutorials. The class name is  if you want to look at all of the code.  Here are the important parts:  private RpcMap map;  public &lt;X&gt; X get(String property) {   if (allowNestedValues &amp;&amp; NestedModelUtil.isNestedProperty(property)) {     return (X)NestedModelUtil.getNestedValue(this, property);   }   return map == null ? null : (X) map.get(property); }   X is defined nowhere else in the class or anywhere in the hierarchy, and when I hit "go to declaration" in eclipse it just goes to the &lt;X&gt; in the public method signature.  I've tried to call this method with the following two examples to see what happens:  public Date getExpiredate() {     return  get("expiredate"); }  public String getSubject() {     return  get("subject"); }   They compile and show no errors or warnings.  I would think at the very least I would have to do a cast to get this to work.  Does this mean that Generics allow a magic return value that can be anything and will just blow up at runtime?  This seems counter to what generics are supposed to do.  Can anyone explain this to me and possibly give me a link to some documentation that explains this a little better?  I've went through Sun's 23 page pdf on generics and every example of a return value is defined either at the class level or is in one of the parameters passed in.
1624
The very common beginner mistake is when you try to use a class property "statically" without making an instance of that class. It leaves you with the mentioned error message:     You can either make the non static method static or make an instance of that class to use its properties.   Why? I am not asking for solutions. I would be grateful to know what is the reason behind it. The very core reason!  private java.util.List&lt;String&gt; someMethod(){     /* Some Code */     return someList;             }  public static void main(String[] strArgs){                // The following statement causes the error. You know why..     java.util.List&lt;String&gt; someList = someMethod();          }
16897
I want to create an alias for a class name. The following syntax would be perfect: public class LongClassNameOrOneThatContainsVersionsOrDomainSpecificName {    ... }  public class MyName = LongClassNameOrOneThatContainsVersionOrDomainSpecificName;  but it won't compile.  Example Note This example is provided for convenience only. Don't try to solve this particular problem by suggesting changing the design of the entire system. The presence, or lack, of this example doesn't change the original question. Some existing code depends on the presence of a static class: public static class ColorScheme {    ... }  This color scheme is the Outlook 2003 color scheme. i want to introduce an Outlook 2007 color scheme, while retaining the Outlook 2003 color scheme: public static class Outlook2003ColorScheme {    ... }  public static class Outlook2007ColorScheme {    ... }  But i'm still faced with the fact that the code depends on the presence of a static class called ColorScheme. My first thought was to create a ColorScheme class that I will inherit from either Outlook2003 or Outlook2007: public static class ColorScheme : Outlook2007ColorScheme { }  but you cannot inherit from a static class. My next thought was to create the static ColorScheme class, but make Outlook2003ColorScheme and Outlook2007ColorScheme classes non-static. Then a static variable in the static ColorScheme class can point to either &quot;true&quot; color scheme: public static class ColorScheme {     private static CustomColorScheme = new Outlook2007ColorScheme();     ... }  private class CustomColorScheme  {     ... }  private class Outlook2008ColorScheme : CustomColorScheme  {     ... }  private class Outlook2003ColorScheme : CustomColorScheme  {    ... }  but that would require me to convert a class composed entirly of readonly static Colors into overridable properties, and then my ColorScheme class would need to have the 30 different property getters thunk down into the contained object. That's just too much typing. So my next thought was to alias the class: public static ColorScheme = Outlook2007ColorScheme;  But that doesn't compile. How can I alias a static class into another name?  Update: Can someone please add the answer &quot;You cannot do this in C#&quot;, so I can mark that as the accepted answer. Anyone else wanting the answer to the same question will find this question, the accepted answer, and a number of workarounds that might, or might not, be useful. I just want to close this question out.
37596
Say I have a class that's meant to perform a single function. After performing the function, it can be destroyed. Is there any reason to prefer one of these approaches?  // Initialize arguments in constructor MyClass myObject = new MyClass(arg1, arg2, arg3); myObject.myMethod();  // Pass arguments to method MyClass myObject = new MyClass(); myObject.myMethod(arg1, arg2, arg3);  // Pass arguments to static method MyClass.myMethod(arg1, arg2, arg3);   I was being intentionally vague about the details, to try to get guidelines for different situations. But I didn't really have in mind simple library functions like Math.random(). I'm thinking more of classes that perform some specific, complex task, but only require one (public) method to do it.
111415
I have a Generic class like that :  public class Repository&lt;T&gt; {...}   And I need to instance that with a string ... Example :  string _sample = "TypeRepository"; var _rep = new Repository&lt;sample&gt;();   How can I do that? Is that even possible?  Thanks!
80088
When referencing class variables, why do people prepend it with this? I'm not talking about the case when this is used to disambiguate from method parameters, but rather when it seems unnecessary.  Example:  public class Person {             private String name;      public String toString() {         return this.name;     } }   In toString, why not just reference name as name?  return name;   What does this.name buy?   a stackoverflow question whose code has this pre-pending.
85933
I was writing a toString() for a class in Java the other day by manually writing out each element of the class to a String and it occurred to me that using reflection it might be possible to create a generic toString() method that could work on ALL classes. I.E. it would figure out the field names and values and send them out to a String.  Getting the field names is fairly simple, here is what a co-worker came up with:  public static List initFieldArray(String className) throws ClassNotFoundException {      Class c = Class.forName(className);     Field field[] = c.getFields();     List&lt;String&gt; classFields = new ArrayList(field.length);      for (int i = 0; i &lt; field.length; i++) {         String cf = field[i].toString();         classFields.add(cf.substring(cf.lastIndexOf(".") + 1));     }      return classFields; }   Using a factory I could reduce the performance overhead by storing the fields once, the first time the toString() is called. However finding the values could be a lot more expensive.  Due to the performance of reflection this may be more hypothetical then practical. But I am interested in the idea of reflection and how I can use it to improve my everyday programming.
96692
What method do I call to get the name of a class?
69373
I have two Java classes: B, which extends another class A, as follows :  class A {     public void myMethod() { /* ... */ } }  class B extends A {     public void myMethod() { /* Another code */ } }   I would like to call the A.myMethod() from B.myMethod(). I am coming from the , and I don't know how to do this basic thing in Java.
123439
Ok, bear with me guys and girls as I'm learning.  Here's my question.  I can't figure out why I can't override a method from a parent class.  Here's the code from the base class (yes, I pilfered the java code from an OOP book and am trying to rewrite it in C#).  using System;  public class MoodyObject {     protected String getMood()     {         return "moody";     }      public void queryMood()     {         Console.WriteLine("I feel " + getMood() + " today!");     } }   and here are my other 2 objects that inherit the base class (MoodyObject):  using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace ConsoleApplication1 {     public class SadObject: MoodyObject     {         protected String getMood()         {             return "sad";         }          //specialization         public void cry()         {             Console.WriteLine("wah...boohoo");         }     } }   And:  using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace ConsoleApplication1 {     public class HappyObject: MoodyObject     {         protected String getMood()         {             return "happy";         }          public void laugh()         {             Console.WriteLine("hehehehehehe.");         }     } }   and here is my main:  using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             MoodyObject moodyObject = new MoodyObject();             SadObject sadObject = new SadObject();             HappyObject happyObject = new HappyObject();              Console.WriteLine("How does the moody object feel today?");             moodyObject.queryMood();             Console.WriteLine("");             Console.WriteLine("How does the sad object feel today?");             sadObject.queryMood();             sadObject.cry();             Console.WriteLine("");             Console.WriteLine("How does the happy object feel today?");             happyObject.queryMood();             happyObject.laugh();         }     } }   As you can see, pretty basic stuff, but here's the output:     How does the moody object feel today?   I feel moody today!      How does the sad object feel today? I   feel moody today! wah...boohoo      How does the happy object feel today?   I feel moody today! hehehehehehe.   Press any key to continue . . .   Not as I expected.  I've tried to make the base method virtual and calling override when trying to override it and that just gets me this error "cannot override inherited member 'MoodyObject.getMood()' because it is not marked virtual, abstract, or override".  I also tried it without the virtual and override and it thinks I'm trying to hide the base method.  Again, I'm new to OOP and would appreciate any guidance.  EDITED TO ADD:  I found it!  The MoodyObject.cs was only a "solution item" in the solution explorer as opposed to a "ConsoleApplication1" item.  I dragged it down to where it belonged in the solution explorer and voila!  It works now.  I marked Luc's answer below as the answer because he offered the help I needed to get to where I have it resolved... I'm learning so much here.  It's amazing and you guys and girls are crazy smart!
3079
In C# is there a technique using reflection to determine if a method has been added to a class as an extension method?  Given an extension method such as the one shown below is it possible to determine that Reverse() has been added to the string class?  public static class StringExtensions {     public static string Reverse(this string value)     {         char[] cArray = value.ToCharArray();         Array.Reverse(cArray);         return new string(cArray);     } }   We're looking for a mechanism to determine in unit testing that the extension method was appropriately added by the developer.  One reason to attempt this is that it is possible that a similar method would be added to the actual class by the developer and, if it was, the compiler will pick that method up.
29305
What does it mean to extend a function? Can someone please give an example?  Thanks in advance!