0

I am having difficulties knowing which of the following classes / interfaces can I instantiate in the Main method (which class / interface is completely OK). The code goes like this for some of the classes and interfaces:

interface A {public void Method();}
class B {public static int b;}
abstract class C:B {public void Method1();}
sealed class D:B {} ;
class E:A {};
class F:A {public void Method();}
class G:C {};

And then later on, we have the Main method in another Class, like this...

class Program 
{
    static void Main(string[] args) 
    {
        A a = new A();
        B b = new B();
        A ab = new B();
        B ba = new A();
        C c = new C();
        D d = new D();
        E e = new E();
        F af = new A();
        A fa = new F();
        G g = new G();
    }
}

So, which ones can we use from above? I know it's a silly question to ask, but this is what we actually get on out test at University.

martijnn2008
  • 3,552
  • 5
  • 30
  • 40
  • Google is your friend ;) Use the keyword 'instantiate' together with the modifiers you want to learn about. – martijnn2008 Sep 08 '18 at 16:40
  • `a` is out, you can't create an instance of an interface. `b` is OK as long as it is in scope, it is internal so depends on where it is in relation to Program.Main, `ab` is invalid since `B` doesn't implement interface `A`, `ba` is again invalid because you still cannot create an instance of an interface, `c` is out because `C` is abstract, `d` is OK, `e` is OK, `f` is still not valid (interface), `fa`is OK since `F` implements `A`, and `g` is OK (note, this was done by just reading, I might've missed an important and obscure hierarchy detail so **test it!**) – Lasse V. Karlsen Sep 08 '18 at 16:46
  • Additionally, `A` cannot be declared as-is since you cannot use visibility modifiers like `public` for members of an interface, `Method1` in `C` is not declared abstract so it requires a body, and same with `Method` in `F`. – Lasse V. Karlsen Sep 08 '18 at 16:47

3 Answers3

1

Most of your class declarations wouldn't compile. Only the declarations for B, D and G compile.

interface A {public void Method();} // "public" cannot be used on interface members
class B {public static int b;}
abstract class C:B {public void Method1();} // method without body should be marked as "abstract"
sealed class D:B {} ;
class E:A {}; // interface methods not implemented
class F:A {public void Method();} // method does not have a body
class G:C {};

For the statements in Main, most of them do not compile either:

A a = new A(); // cannot instantiate interface A
B b = new B(); // OK because B is a normal class
A ab = new B(); // B can be instantiated for aforementioned reasons, but cannot be
                // assigned to A because they are unrelated types
B ba = new A(); // cannot instantiate interface A. A also cannot be assigned to
                // B because they are unrelated.
C c = new C(); // cannot instantiate abstract class C
D d = new D(); // OK, D is a normal class. It is sealed, but that just means no 
               // class can derive from it, nothing to do with instantiation
E e = new E(); // OK, E is a normal class
F af = new A(); // cannot instantiate interface A
A fa = new F(); // F is a normal class, and is assignable to A because F implements A
G g = new G(); // OK, G is a normal class

General patterns:

  • abstract classes and interfaces can't be instantiated
  • The sealed keyword has nothing to do with instantiation
  • classes with only a private constructor can't be instantiated
  • An expression of type T1 can be assigned to a variable of type T2 if:
    • T1 and T2 are the same type, or;
    • T1 inherits T2, or;
    • T1 implements T2, or;
    • T1 is implicitly convertible to T2
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

You can't new up interfaces or abstract classes, so those would cause errors.

Interfaces are just contracts. They aren't classes. You can't instantiate them.

Abstract classes can only be inherited. They cant be created on their own.

Sealed classes just mean they can't be inherited. You can still instantiate them.

Todd Skelton
  • 6,839
  • 3
  • 36
  • 48
0
  • An abstract class cannot be instantiated.
  • The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.

source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract

  • An interface can't be instantiated directly.

source: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/

This means that:

class Program 
{
    static void Main(string[] args) 
    {
        A a = new A();  // ERROR: cannot instantiate interface
        B b = new B();  // OK
        A ab = new B(); // ERROR: class B doesn't implement interface A
        B ba = new A(); // ERROR: cannot instantiate interface
        C c = new C();  // ERROR: cannot instantiate abstract class
        D d = new D();  // OK
        E e = new E();  // OK
        F af = new A(); // ERROR: cannot instantiate interface
        A fa = new F(); // OK:    class F does implement interface A
        G g = new G();  // OK
    }
}
martijnn2008
  • 3,552
  • 5
  • 30
  • 40
  • Thank you very much. As for class G, we don't need to implement the methods from class C as it's abstract? That's only what is unclear to me that's left. – Srđan Todorović Sep 08 '18 at 16:48
  • There are also some things wrong with the class declarations. You do need to implement methods in the child class that are marked as `abstract` in the parent class. Methods that do not have an implementation in the `abstract` class must also have the modifier `abstract`. – martijnn2008 Sep 08 '18 at 16:53