About abstract classes?

URL: Link (1)

According to this weightbet .. you cannot implement abstract classes, but derive from them. It makes sense and I've read it many times.

Like an interface, you cannot implement an instance of an abstract class, however, you can implement methods, fields, and properties of an abstract class that can be used by a child class.

But on MSDN

URL: TextWriter CLASS on MSDN

TextWriter is an abstract class, but it has two constructors defined ... and according to MS book 70-536, the following statement holds true:

TextWriter tw = new File.CreateText("myFile.Txt")

      

The file's static class and CreateText method works great for me as I studied it on MSDN, but can anyone explain this little contradiction I found? Am I not the first?

Why is it possible to create basic abstract classes?

+2


a source to share


7 replies


All abstract classes have at least one constructor - either you implement it, or the compiler generates a default constructor with no parameters. The constructor is executed when the derived class is instantiated β€” it doesn't matter that the base class is abstract.



But you cannot instantiate an abstract class - it File.CreateText()

is a static method and returns an instance of a class derived from TextWriter

, but not an instance TextWriter

.

+3


a source


File.CreateText does not return a TextWriter, but a StreamWriter that implements a TextWriter.



+4


a source


You can instantiate what appears to be an abstract class for the same reason that you can instantiate what appears to be an interface. In fact, you are not creating a class of this type, but creating a class that can be classified as this type. tw

can be defined as a TextWriter, when in fact it cannot be a TextWriter.

However, I am suspicious of the new keyword. Are you sure this works? It shouldn't. File.CreateText

creates and returns (something that inherits) a TextWriter, but new

points to a constructor, which is not what's going on.

+3


a source


The File.CreateText implementation creates and returns a type that is a concrete TextWriter implementation. That is, Whatever type is returned - TextWriter is not a magical creation of an abstract class.

While you cannot create an abstract class, you can refer to it as an instance of a derived type by its base type without issue. If you couldn't do this, then polymorphism would be useless!

+1


a source


The magic of polymorphism / inheritance.

File.CreateText

returns a StreamWriter

that inherits from TextWriter

.

+1


a source


It's impossible.

What happens (as soon as you remove the keyword new

to make the code work altogether) the method CreateText

creates an instance StreamWriter

that inherits TextWriter

. The reference StreamWriter

is assigned to a variable TextWriter

, which is valid as a StreamWriter

is TextWriter

.

The reason why an abstract class has constructors is to initialize the data in the abstract class. The constructor cannot be used directly, but it will be called by the constructor in the class that inherits the abstract class.

+1


a source


CreateText returns StreamWriter

public static StreamWriter CreateText(string path)

      

StreamWriter is a subclass of TextWriter and therefore can be stored in the TextWriter variable. The example uses an abstract class because it doesn't care about the actual implementation of what it writes text to.

0


a source







All Articles