Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between an abstract class and a static one?

Tags:

c#

.net

oop

Neither is instantiable. What are the differences, and in what situations might you use one or the other?

like image 346
stannius Avatar asked Mar 06 '10 00:03

stannius


People also ask

What is the difference between static and abstract?

Static means which is initialized once for example our main method is initialized only once. an abstract class is that which can have one or more than one abstract methods. abstract methods are those which don't have implementation/body rather only has its declaration.it will necessarily have abstract keyword.

What is the difference between static class and abstract class in Java?

A static variable is a class variable. A single copy of the static variable is created for all instances of the class. It can be directly accessed in a static method. An abstract class in Java is a class that cannot be instantiated.

What is the difference between static and class?

The difference between a static class and a non-static class is that a static class cannot be instantiated or inherited and that all of the members of the class are static in nature. To declare a class as static, you should mark it with the static keyword in the class declaration.

Can an abstract class be static?

Yes, abstract class can have Static Methods. The reason for this is Static methods do not work on the instance of the class, they are directly associated with the class itself.


2 Answers

static indicates the class can only have static members and you cannot create an instance of it. This is used for stateless functionality (for example a type that just defines extension methods, or utility methods). You can also declare a member static on a non-static class. This allows you to attach functionality to a type without having to instantiate it.

Here's more detail on using static members and classes.

abstracts define the basic structure and functionality shared by all derivative types, but cannot be used by themselves. Think of them as, I suppose, a blue print and a contract. This is a core concept for OOP.

Here's more detail on using abstracts.

like image 139
moribvndvs Avatar answered Sep 21 '22 20:09

moribvndvs


Here is a short summary:

  • A static class can only contain static members (it is just a container for methods that do not logically belong to an instance of any standard class)
  • An abstract class can contain all usual kinds of members (static, abstract and also instance)

The key difference is that you can inherit from an abstract class, but you cannot inherit from a static class. Technically speaking, the .NET runtime doesn't have any notion of static classes, so the C# compiler compiles them as classes that are both abstract and sealed (meaning that you cannot inherit from them).

So, static classes are abstract classes that are also sealed (although this is not the usual way to look at the problem if you are C# programmer) and contain only static members (which is enforced by the C# compiler).

like image 42
Tomas Petricek Avatar answered Sep 20 '22 20:09

Tomas Petricek