Abstract Class confusion

I have learnt so far that abstract classes can’t be instantiated because they are only there for other classes to inherit from. But i cant get my head around this code i came across in the msdn library:

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

in the documentation it says the above code returns a WebRequest instance.

but the WebRequest class is an abstract class, and how comes the above code returns an instance of an abstract class when abstract classes can’t be instantiated.

please relieve me from my agony.

2

Most likely, the Create method in that example will return an instance of a subclass of WebRequest.

To see exactly what you’re getting back, you could try something like this:

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");
Type t = myRequest.GetType();
Console.WriteLine(t.Name);

1

Abstract classes can’t be instantiated immediately. That is, you can’t do new SomeAbstractClass(). However, you can instantiate subclasses of an abstract class. So abstract classes are similar in function to interfaces, except that they can contain method definitions and member fields.

As such, abstract classes are great for introducing a level of abstraction. The WebRequest class is a good example here: It has subclasses for various protocols such as HTTP or HTTPS. When you instantiate a request via WebRequest.Create("https://example.com/"), you get a web request instance that can handle the HTTPS protocol, but is also a WebRequest instance. The static method Create(…) is a factory method.

See also the documentation for WebRequest.Create(…)

1

WebRequest is an abstract class that can’t be instantiated. WebRequest.Create is a static method that returns a concrete type of System.Net.HttpWebRequest that is a derived subclass of System.Net.WebRequest. Because it is a derived type, it still identifies itself as a WebRequest even though it’s fully qualified type name is System.Net.HttpWebRequest.

The reason this is useful is that you no longer need to care about the actual implementation. You know that a WebRequest will have all of the functionality you need so if the concrete type is a System.Net.HttpWebRequest or a Steves.Awesome.WebRequest or a Georges.NotSoAwesome.WebRequest it doesn’t matter because they’re all inherited from WebRequest.

Your line of code:

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

This isn’t saying that it’s actually going to create a WebRequest and only a WebRequest. It’s saying that whatever is created will inherit from WebRequest and be compatible with anything you need going forward from the standpoint.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Abstract Class confusion

I have learnt so far that abstract classes can’t be instantiated because they are only there for other classes to inherit from. But i cant get my head around this code i came across in the msdn library:

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

in the documentation it says the above code returns a WebRequest instance.

but the WebRequest class is an abstract class, and how comes the above code returns an instance of an abstract class when abstract classes can’t be instantiated.

please relieve me from my agony.

2

Most likely, the Create method in that example will return an instance of a subclass of WebRequest.

To see exactly what you’re getting back, you could try something like this:

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");
Type t = myRequest.GetType();
Console.WriteLine(t.Name);

1

Abstract classes can’t be instantiated immediately. That is, you can’t do new SomeAbstractClass(). However, you can instantiate subclasses of an abstract class. So abstract classes are similar in function to interfaces, except that they can contain method definitions and member fields.

As such, abstract classes are great for introducing a level of abstraction. The WebRequest class is a good example here: It has subclasses for various protocols such as HTTP or HTTPS. When you instantiate a request via WebRequest.Create("https://example.com/"), you get a web request instance that can handle the HTTPS protocol, but is also a WebRequest instance. The static method Create(…) is a factory method.

See also the documentation for WebRequest.Create(…)

1

WebRequest is an abstract class that can’t be instantiated. WebRequest.Create is a static method that returns a concrete type of System.Net.HttpWebRequest that is a derived subclass of System.Net.WebRequest. Because it is a derived type, it still identifies itself as a WebRequest even though it’s fully qualified type name is System.Net.HttpWebRequest.

The reason this is useful is that you no longer need to care about the actual implementation. You know that a WebRequest will have all of the functionality you need so if the concrete type is a System.Net.HttpWebRequest or a Steves.Awesome.WebRequest or a Georges.NotSoAwesome.WebRequest it doesn’t matter because they’re all inherited from WebRequest.

Your line of code:

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

This isn’t saying that it’s actually going to create a WebRequest and only a WebRequest. It’s saying that whatever is created will inherit from WebRequest and be compatible with anything you need going forward from the standpoint.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật