473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

abstract class

An abstract method has no implementation. Its implementation logic is provided instead by classes that derive from it. We use an abstract class to create a base template for derived classes.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. abstract class Test
  4. {
  5.     public int _a;
  6.     public abstract void A();
  7. }
  8.  
  9. class Example1 : Test
  10. {
  11.     public override void A()
  12.     {
  13.     Console.WriteLine("Example1.A");
  14.     base._a++;
  15.     }
  16. }
  17.  
  18. class Example2 : Test
  19. {
  20.     public override void A()
  21.     {
  22.     Console.WriteLine("Example2.A");
  23.     base._a--;
  24.     }
  25. }
  26.  
  27. class Program
  28. {
  29.     static void Main()
  30.     {
  31.     // Reference Example1 through Test type.
  32.     Test test1 = new Example1();
  33.     test1.A();
  34.  
  35.     // Reference Example2 through Test type.
  36.     Test test2 = new Example2();
  37.     test2.A();
  38.     }
  39. }
Derived classes. When you create a derived class like Example1 or Example2, you must provide an override method for all abstract methods in the abstract class. The A() method in both derived classes satisfies this requirement.
Override

Int field. An abstract class can have an instance field in it. The derived classes can access this field through the base syntax. This is a key difference between abstract classes and interfaces.
Int

Cannot instantiate abstract class. The important part of an abstract class is that you can never use it separately from a derived class. Therefore in Main you cannot use the new Test() constructor.

However:You can use the Test type directly once you have assigned it to a derived type such as Example1 or Example2.
Aug 30 '14 #1
0 4409

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
16
by: Merlin | last post by:
Hi Been reading the GOF book and started to make the distinction between Class and Interface inheritance. One question though: Do pure abstract classes have representations? (data members?)...
8
by: Claire | last post by:
I have a base abstract class A which contains several abstract functions I then have an abstract class B (derived from A) which overrides a small subset of A's abstract functions Finally I...
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
0
by: Craig Buchanan | last post by:
i am trying to build an application that uses plugins to extend the business functionality of an application. i've decided to use inheritance of abstract classes as the mechanism to do this. ...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
2
by: Rahul | last post by:
Hey Guys Just go easy on me . I have an abstract class which is pure is a kind of asmx. I know the answer that I am asking but still need somebody's advice. Since it's an abstract class, the...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.