14

Can I create instance of abstract class in C#/.net like in Java ?

Additional Info

I think a lot of us does not understand what do I mean? So, In java I can create abstract class like this :

Simple abstract class :

/**
 * @author jitm
 * @version 0.1
 */
public abstract class TestAbstract
{
    public abstract void toDoSmth();
}

Code where I've create instance of abstract class

/**
 * @author jitm
 * @version 0.1
 */
public class Main {
    public static void main(String[] args) {
        TestAbstract testAbstract = new TestAbstract() {
            @Override
            public void toDoSmth() {
                System.out.println("Call method toDoSmth");
            }
        };
    }
}

Can I to do in c# something like this ?

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
jitm
  • 2,569
  • 10
  • 40
  • 55
  • 7
    Currently your question is *far* too vague. In particular, you've mentioned abstract classes in the title and in the tags, but not in the body of the text. If you have a particular piece of Java code in mind, please post it. – Jon Skeet Aug 09 '10 at 18:34
  • 1
    He mentioned a class named "Abstact". Might be relevant. – Hans Passant Aug 09 '10 at 18:44
  • 1
    No, class Abstract equals abstract :) – jitm Aug 09 '10 at 18:57
  • 6
    You're not really instantiating an abstract class in the Java example either, although it looks that way. What really happens is that you are implicitly defining a new, _concrete_ class that derives from `TestAbstract` and overrides a specific method. – Roman Starkov Aug 09 '10 at 19:18
  • 2
    If you need to instantiate an abstract class, it probably should not be an abstract class – ravibhagw Aug 09 '10 at 19:28
  • 2romkyns I understood but I do not need to create new class directly only anonymous. – jitm Aug 09 '10 at 19:31
  • 2 baultista, Probably Yes :), but my abstarct class contains some common functionality and I need to test how it works ... – jitm Aug 09 '10 at 19:32

3 Answers3

9

Neither in Java nor in C# you can create an instance of an abstract class. You will always need to create a concrete class that inherits from the abstract class. Java lets you do it without naming the class, using anonymous classes. C# does not give you that option.

(Edited to show a delegate as a replacement. I don't have access to VS here, so it may not compile, but this is the idea )

Usually in Java when you use an abstract class with a single abstract method (SAM) what you are really trying to achieve is to pass some code as a parameter. Let's say you need to sort an array of objects based on the class name, using Collections.sort(Collection, Comparator) (I know Comparator is an interface, but it is the same idea) Using an anonymous class to avoid extra typing, you can write something like

   Comparator<Object> comparator = new Comparator<Object>{
        @Override
        public int compare(Object o1, Objecto2) {
            return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()));
        }//I'm not checking for null for simplicity
   } 
   Collections.sort(list, comparator)

In C# 2.0 and beyond you can do pretty much the same using the Comparison<T> delegate. A delegate can be thought as a function object, or in java words, a class with a single method. You don’t even need to create a class, but only a method using the keyword delegate.

Comparison<Object> comparison = delegate(Object o1, Object o2)
{
    return o1.class.Name.CompareTo(o2.class.Name);        
};

list.sort(comparison);

In C# 3.0 and beyond you can write even less code using lambdas and type inference:

list.sort((o1, o2) => o1.class.Name.CompareTo(o2.class.Name))

Anyway, if you are migrating code form java to c# you should read about delegates...in many of cases you will use delegates instead of anonymous classes. In your case, you are using a method void toDoSmth(). There is a delegate called Action which is pretty much the same thing, a method with no parameters and no return.

Timwi
  • 65,159
  • 33
  • 165
  • 230
Pablo Grisafi
  • 5,039
  • 1
  • 19
  • 29
  • Yes, my question was can I create instance like in java (using anonymous) currently I understood that I cannot. Thanks. – jitm Aug 09 '10 at 19:35
  • 2
    In some (most?) cases, you should replace anonymous classes with delegates and you will write even less code...Specially with one method classes. – Pablo Grisafi Aug 09 '10 at 20:05
  • 2 Pablo: Could you post example how to do it, because currently I've created simple class from abstract and use it. – jitm Aug 10 '10 at 07:04
7

No. Not directly. The closest you can come (assuming you have the following class structure):

public abstract class AbstractClass
{
}

public class ChildClass : AbstractClass
{ 
}

Is:

AbstractClass instance = new ChildClass();
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
-4

Your example uses an anonymous implemention for an abstract class. C# does not support the anonymous implementation, so you need to create another one that extends it.

public abstract class TestAbstract { }

public class AnotherTest : TestAbstract { }

Rodrigo Matias
  • 352
  • 2
  • 7
Struan
  • 645
  • 1
  • 5
  • 11
  • In Java I can, only I need it is implement abstract methods, but, I can to do it like anonymous implementation. – jitm Aug 09 '10 at 18:59
  • In that case, then no - C# (as far as I know doesn't allow you to create an anonymous implementation of an abstract class). – RQDQ Aug 09 '10 at 19:21
  • You still aren't instantiating the abstract class, you are instantiating an anonymous subclass. – Struan Aug 09 '10 at 19:34
  • Yes, I understood, but without creating new type only anonymous implementation of abstract body. – jitm Aug 09 '10 at 19:37
  • @jitm: It still creates a new type. – Timwi Mar 08 '11 at 23:32