- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Object-oriented programming
In this part of the C# tutorial, we will talk about object oriented programming in C#.
There are three widely used programming paradigms: procedural programming, functional programming and object-oriented programming. C# supports both procedural and object-oriented programming.
Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs.
There are some basic programming concepts in OOP:
- Abstraction
- Polymorphism
- Encapsulation
- Inheritance
The abstraction is simplifying complex reality by modeling classes appropriate to the problem. The polymorphism is the process of using an operator or function in different ways for different data input. The encapsulation hides the implementation details of a class from other objects. The inheritance is a way to form new classes using classes that have already been defined.
Objects
Objects are basic building blocks of a C# OOP program. An object is a combination of data and methods. The data and the methods are called members of an object. In an OOP program, we create objects. These objects communicate together through methods. Each object can receive messages, send messages and process data.
There are two steps in creating an object. First, we define a class. A class is a template for an object. It is a blueprint which describes the state and behavior that the objects of the class all share. A class can be used to create many objects. Objects created at runtime from a class are called instances of that particular class.
using System; public class Being {} public class Objects { static void Main() { Being b = new Being(); Console.WriteLine(b); } }
In our first example, we create a simple object.
public class Being {}
This is a simple class definition. The body of the template is empty. It does not have any data or methods.
Being b = new Being();
We create a new instance of the Being
class. For this we have the new
keyword. The b
variable is the handle to the created object.
Console.WriteLine(b);
We print the object to the console to get some basic description of the object. What does it mean, to print an object? When we print an object, we in fact call its ToString()
method. But we have not defined any method yet. It is because every object created inherits from the base object
. It has some elementary functionality which is shared among all objects created. One of this is the ToString()
method.
$ ./simpleobject.exe Being
We get the object class name.
Object attributes
Object attributes is the data bundled in an instance of a class. The object attributes are called instance variables or member fields. An instance variable is a variable defined in a class, for which each object in the class has a separate copy.
using System; public class Person { public string name; } public class ObjectAttributes { static void Main() { Person p1 = new Person(); p1.name = "Jane"; Person p2 = new Person(); p2.name = "Beky"; Console.WriteLine(p1.name); Console.WriteLine(p2.name); } }
In the above C# code, we have a Person
class with one member field.
public class Person { public string name; }
We declare a name member field. The public
keyword specifies that the member field will be accessible outside the class block.
Person p1 = new Person(); p1.name = "Jane";
We create an instance of the Person
class and set the name variable to "Jane". We use the dot operator to access the attributes of objects.
Person p2 = new Person(); p2.name = "Beky";
We create another instance of the Person
class. Here we set the variable to "Beky".
Console.WriteLine(p1.name); Console.WriteLine(p2.name);
We print the contents of the variables to the console.
$ ./person.exe Jane Beky
We see the output of the program. Each instance of the Person
class has a separate copy of the name member field.
Methods
Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of our objects. Methods bring modularity to our programs.
Methods are essential in the encapsulation concept of the OOP paradigm. For example, we might have a Connect()
method in our AccessDatabase
class. We need not to be informed how exactly the method Connect()
connects to the database. We only have to know that it is used to connect to a database. This is essential in dividing responsibilities in programming, especially in large applications.
Objects group state and behavior, methods represent the behavioral part of the objects.
using System; public class Circle { private int radius; public void SetRadius(int radius) { this.radius = radius; } public double Area() { return this.radius * this.radius * Math.PI; } } public class Methods { static void Main() { Circle c = new Circle(); c.SetRadius(5); Console.WriteLine(c.Area()); } }
In the code example, we have a Circle class. We define two methods.
private int radius;
We have one member field. It is the radius of the circle. The private
keyword is an access specifier. It tells that the variable is restricted to the outside world. If we want to modify this variable from the outside, we must use the publicly available SetRadius()
method. This way we protect our data.
public void SetRadius(int radius) { this.radius = radius; }
This is the SetRadius()
method. The this
variable is a special variable which we use to access the member fields from methods. The this.radius
is an instance variable, while the radius is a local variable, valid only inside the SetRadius()
method.
Circle c = new Circle(); c.SetRadius(5);
We create an instance of the Circle
class and set its radius by calling the SetRadius()
method on the object of the circle. We use the dot operator to call the method.
public double Area() { return this.radius * this.radius * Math.PI; }
The Area()
method returns the area of a circle. The Math.PI
is a built-in constant.
$ ./circle.exe 78.5398163397448
Running the example gives this outcome.
Access modifiers
Access modifiers set the visibility of methods and member fields. C# has four access modifiers: public
, protected
, private
and internal
. The public
members can be accessed from anywhere. The protected
members can be accessed only within the class itself and by inherited and parent classes. The private
members are limited to the containing type, e.g. only within its class or interface. The internal
members may be accessed from within the same assembly (exe or DLL).
Access modifiers protect data against accidental modifications. They make the programs more robust.
using System; public class Person { public string name; private int age; public int GetAge() { return this.age; } public void SetAge(int age) { this.age = age; } } public class AccessModifiers { static void Main() { Person p = new Person(); p.name = "Jane"; p.SetAge(17); Console.WriteLine("{0} is {1} years old", p.name, p.GetAge()); } }
In the above program, we have two member fields. One is declared public, the other private.
public int GetAge() { return this.age; }
If a member field is private
, the only way to access it is via methods. If we want to modify an attribute outside the class, the method must be declared public
. This is an important aspect of data protection.
public void SetAge(int age) { this.age = age; }
The SetAge()
method enables us to change the private
age variable from outside of the class definition.
Person p = new Person(); p.name = "Jane";
We create a new instance of the Person
class. Because the name attribute is public
, we can access it directly. However, this is not recommended.
p.SetAge(17);
The SetAge()
method modifies the age member field. It cannot be accessed or modified directly because it is declared private
.
Console.WriteLine("{0} is {1} years old", p.name, p.GetAge());
Finally, we access both members to build a string.
$ ./modifiers.exe Jane is 17 years old
Running the example gives this output.
Member fields with private
access modifiers are not inherited by derived classes.
using System; public class Base { public string name = "Base"; protected int id = 5323; private bool isDefined = true; } public class Derived : Base { public void info() { Console.WriteLine("This is Derived class"); Console.WriteLine("Members inherited"); Console.WriteLine(this.name); Console.WriteLine(this.id); // Console.WriteLine(this.isDefined); } } public class CSharpApp { static void Main() { Derived drv = new Derived(); drv.info(); } }
In the preceding program, we have a Derived
class which inherits from the Base
class. The Base
class has three member fields. All with different access modifiers. The isDefined member is not inherited. The private
modifier prevents this.
public class Derived : Base
The class Derived
inherits from the Base
class. To inherit from another class, we use the colon (:) operator.
Console.WriteLine(this.name); Console.WriteLine(this.id); // Console.WriteLine(this.isDefined);
The public
and the protected
members are inherited by the Derived class. They can be accessed. The private
member is not inherited. The line accessing the member field is commented. If we uncommented the line, the code would not compile.
$ ./protected.exe This is Derived class Members inherited Base 5323
Running the program, we receive this output.
The constructor
A constructor is a special kind of a method. It is automatically called when the object is created. Constructors do not return values. The purpose of the constructor is to initiate the state of an object. Constructors have the same name as the class. The constructors are methods, so they can be overloaded too.
Constructors cannot be inherited. They are called in the order of inheritance. If we do not write any constructor for a class, C# provides an implicit default constructor. If we provide any kind of a constructor, then a default is not supplied.
using System; public class Being { public Being() { Console.WriteLine("Being is created"); } public Being(string being) { Console.WriteLine("Being {0} is created", being); } } public class Constructor { static void Main() { new Being(); new Being("Tom"); } }
We have a Being class. This class has two constructors. The first one does not take parameters; the second one takes one parameter.
public Being(string being) { Console.WriteLine("Being {0} is created", being); }
This constructor takes one string parameter.
new Being();
An instance of the Being
class is created. This time the constructor without a parameter is called upon object creation.
$ ./constructor.exe Being is created Being Tom is created
This is the output of the program.
In the next example, we initiate data members of the class. Initiation of variables is a typical job for constructors.
using System; public class MyFriend { private DateTime born; private string name; public MyFriend(string name, DateTime born) { this.name = name; this.born = born; } public void Info() { Console.WriteLine("{0} was born on {1}", this.name, this.born.ToShortDateString()); } } public class Constructor2 { static void Main() { string name = "Lenka"; DateTime born = new DateTime(1990, 3, 5); MyFriend fr = new MyFriend(name, born); fr.Info(); } }
We have a MyFriend
class with data members and methods.
private DateTime born; private string name;
We have two private variables in the class definition.
public MyFriend(string name, DateTime born) { this.name = name; this.born = born; }
In the constructor, we initiate the two data members. The this
variable is a handler used to reference the object variables.
MyFriend fr = new MyFriend(name, born); fr.Info();
We create a MyFriend
object with two arguments. Then we call the Info()
method of the object.
$ ./constructor2.exe Lenka was born on 3/5/1990
We execute the constructor2.exe
program.
Constructor chaining
Constructor chaining is the ability of a class to call another constructor from a constructor. To call another constructor from the same class, we use the this
keyword.
using System; public class Circle { public Circle(int radius) { Console.WriteLine("Circle, r={0} is created", radius); } public Circle() : this(1) { } } public class ConstructorChaining { static void Main() { new Circle(5); new Circle(); } }
We have a Circle
class. The class has two constructors. One that takes one parameter and one that does not take any parameters.
public Circle(int radius) { Console.WriteLine("Circle, r={0} is created", radius); }
This constructor takes one parameter — the radius
.
public Circle() : this(1) { }
This is the constructor without a parameter. It simply calls the other constructor and gives it a default radius of 1.
$ ./constructorchaining.exe Circle, r=5 is created Circle, r=1 is created
This is the output of the constructorchaining.exe
program.
Class constants
C# enables to create class constants. These constants do not belong to a concrete object. They belong to the class. By convention, constants are written in uppercase letters.
using System; public class Math { public const double PI = 3.14159265359; } public class ClassConstants { static void Main() { Console.WriteLine(Math.PI); } }
We have a Math
class with a PI
constant.
public const double PI = 3.14159265359;
The const
keyword is used to define a constant. The public
keyword makes it accessible outside the body of the class.
$ ./classconstant.exe 3.14159265359
Running the example we see this output.
The ToString() method
Each object has a ToString()
method. It returns a human-readable representation of an object. The default implementation returns the fully qualified name of the type of the Object
. Note that when we call the Console.WriteLine()
method with an object as a parameter, the ToString()
is being called.
using System; public class Being { public override string ToString() { return "This is Being class"; } } public class ToStringMethod { static void Main() { Being b = new Being(); object o = new Object(); Console.WriteLine(o.ToString()); Console.WriteLine(b.ToString()); Console.WriteLine(b); } }
We have a Being
class in which we override the default implementation of the ToString()
method.
public override string ToString() { return "This is Being class"; }
Each class created inherits from the base object
. The ToString()
method belongs to this object class. We use the override
keyword to inform that we are overriding a method.
Being b = new Being(); object o = new Object();
We create one custom defined object and one built-in object.
Console.WriteLine(o.ToString()); Console.WriteLine(b.ToString());
We call the ToString()
method on these two objects.
Console.WriteLine(b);
As we have specified earlier, placing an object as a parameter to the Console.WriteLine()
will call its ToString()
method. This time, we have called the method implicitly.
$ ./override.exe System.Object This is Being Class This is Being Class
This is what we get when we run the example.
Inheritance
The inheritance is a way to form new classes using classes that have already been defined. The newly formed classes are called derived classes, the classes that we derive from are called base classes. Important benefits of inheritance are code reuse and reduction of complexity of a program. The derived classes (descendants) override or extend the functionality of the base classes (ancestors).
using System; public class Being { public Being() { Console.WriteLine("Being is created"); } } public class Human : Being { public Human() { Console.WriteLine("Human is created"); } } public class Inheritance { static void Main() { new Human(); } }
In this program, we have two classes. A base Being
class and a derived Human
class. The derived class inherits from the base class.
public class Human : Being
In C#, we use the colon (:) operator to create inheritance relations.
new Human();
We instantiate the derived Human
class.
$ ./inheritance.exe Being is created Human is created
We can see that both constructors were called. First, the constructor of the base class is called, then the constructor of the derived class.
A more complex example follows.
using System; public class Being { static int count = 0; public Being() { count++; Console.WriteLine("Being is created"); } public void GetCount() { Console.WriteLine("There are {0} Beings", count); } } public class Human : Being { public Human() { Console.WriteLine("Human is created"); } } public class Animal : Being { public Animal() { Console.WriteLine("Animal is created"); } } public class Dog : Animal { public Dog() { Console.WriteLine("Dog is created"); } } public class Inheritance2 { static void Main() { new Human(); Dog dog = new Dog(); dog.GetCount(); } }
We have four classes. The inheritance hierarchy is more complicated. The Human
and the Animal
classes inherit from the Being
class. The Dog class inherits directly from the Animal
class and indirectly from the Being
class. We also introduce a concept of a static
variable.
static int count = 0;
We define a static
variable. Static members are members that are shared by all instances of a class.
public Being() { count++; Console.WriteLine("Being is created"); }
Each time the Being
class is instantiated, we increase the count variable by one. This way we keep track of the number of instances created.
public class Animal : Being ... public class Dog : Animal ...
The Animal
inherits from the Being
and the Dog
inherits from the Animal
. Indirectly, the Dog
inherits from the Being
as well.
new Human(); Dog dog = new Dog(); dog.GetCount();
We create instances from the Human
and from the Dog
classes. We call the GetCount()
method of the Dog object.
$ ./inheritance2.exe Being is created Human is created Being is created Animal is created Dog is created There are 2 Beings
The Human
calls two constructors. The Dog
calls three constructors. There are two Beings instantiated.
We use the base
keyword to call the parent's constructor explicitly.
using System; public class Shape { protected int x; protected int y; public Shape() { Console.WriteLine("Shape is created"); } public Shape(int x, int y) { this.x = x; this.y = y; } } public class Circle : Shape { private int r; public Circle(int r, int x, int y) : base(x, y) { this.r = r; } public override string ToString() { return String.Format("Circle, r:{0}, x:{1}, y:{2}", r, x, y); } } public class Shapes { static void Main() { Circle c = new Circle(2, 5, 6); Console.WriteLine(c); } }
We have two classes: the Shape
class and the Circle
class. The Shape
class is a base class for geometrical shapes. We can put into this class some commonalities of the common shapes, like the x, y coordinates.
public Shape() { Console.WriteLine("Shape is created"); } public Shape(int x, int y) { this.x = x; this.y = y; }
The Shape
class has two constructors. The first one is the default constructor. The second one takes two parameters: the x, y coordinates.
public Circle(int r, int x, int y) : base(x, y) { this.r = r; }
This is the constructor for the Circle
class. This constructor initiates the r member and calls the parent's second constructor, to which it passes the x, y coordinates. Have we not called the constructor explicitly with the base
keyword, the default constructor of the Shape
class would be called.
$ ./shapes.exe Circle, r:2, x:5, y:6
This is the output of the example.
Abstract classes and methods
Abstract classes cannot be instantiated. If a class contains at least one abstract method, it must be declared abstract too. Abstract methods cannot be implemented; they merely declare the methods' signatures. When we inherit from an abstract class, all abstract methods must be implemented by the derived class. Furthermore, these methods must be declared with the same of less restricted visibility.
Unlike Interfaces, abstract classes may have methods with full implementation and may also have defined member fields. So abstract classes may provide a partial implementation. Programmers often put some common functionality into abstract classes. And these abstract classes are later subclassed to provide more specific implementation. For example, the Qt graphics library has a QAbstractButton
, which is the abstract base class of button widgets, providing functionality common to buttons. Buttons Q3Button
, QCheckBox
, QPushButton
, QRadioButton
, and QToolButton
inherit from this base abstract class.
Formally put, abstract classes are used to enforce a protocol. A protocol is a set of operations which all implementing objects must support.
using System; public abstract class Drawing { protected int x = 0; protected int y = 0; public abstract double Area(); public string GetCoordinates() { return string.Format("x: {0}, y: {1}", this.x, this.y); } } public class Circle : Drawing { private int r; public Circle(int x, int y, int r) { this.x = x; this.y = y; this.r = r; } public override double Area() { return this.r * this.r * Math.PI; } public override string ToString() { return string.Format("Circle at x: {0}, y: {1}, radius: {2}", this.x, this.y, this.r); } } public class AbstractClass { static void Main() { Circle c = new Circle(12, 45, 22); Console.WriteLine(c); Console.WriteLine("Area of circle: {0}", c.Area()); Console.WriteLine(c.GetCoordinates()); } }
We have an abstract base Drawing
class. The class defines two member fields, defines one method and declares one method. One of the methods is abstract, the other one is fully implemented. The Drawing
class is abstract because we cannot draw it. We can draw a circle, a dot or a square. The Drawing
class has some common functionality to the objects that we can draw.
public abstract class Drawing
We use the abstract
keyword to define an abstract class.
public abstract double Area();
An abstract method is also preceded with the abstract
keyword.
public class Circle : Drawing
A Circle is a subclass of the Drawing
class. It must implement the abstract Area()
method.
public override double Area() { return this.r * this.r * Math.PI; }
When we implement the Area()
method, we must use the override
keyword. This way we inform the compiler that we override an existing (inherited) method.
$ ./abstractclass.exe Circle at x: 12, y: 45, radius: 22 Area of circle: 1520.53084433746 x: 12, y: 45
We see the output of the abstractclass.exe
program.
This was the first part of the description of OOP in C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论