Basic programming, .NET technology.

Abstract class và Interface (1)

We all know the pillars for object-oriented programming are: abstraction, inheritance, encapsulation, polymorphism. The motto is like that, but in an old programming language like C#, how do you apply it to a real problem. Bờm researched and learned, today he understood two keywords: abstraction and inheritance. In this "Hoa Son commentary on the code", Bờm presented: What is an abstract class? What are interfaces?

1. Abstract class. So we can say that an abstract class cannot have an instance (a concrete object, an instance). Abstract classes are often used to define base classes in inheritance.
In an abstract class, we can define abstract methods or non-abstract methods.

A small example will make it easier to understand:
We define 2 classes: Dog and Cat


We see that both the Dog and Cat classes share the same properties and methods. We wonder if there is a way to collect common properties and methods? OOP shows that: we can apply abstraction, and inheritance in this case. We abstract the two classes Dog and Cat into an Animal class with common properties and methods, using the abstract keyword to do this.

The Animal class will be declared as follows



Refactor Dog and Cat class by using inheritance.


Through this example, we need to remember:
- Use abstract class to achieve abstraction, inheritance in object-oriented programming
- Use the abstract keyword to declare a class or a method as abstract.
- To implement abstract members of an abstract class, we use the keyword "override" in a subclass.

2. Interface. We all know C# supports inheritance, but a class can only inherit from one superclass (single inheritance). It does not support multiple inheritances. To solve this problem, we can use an interface. 

In the subclass, we implement the interface. A subclass can implement many different interfaces. So what is an interface?

The interface is like a class, can contain methods, properties ... interface contains only declarations (declaration). The classes implement the interface, which will specify the methods and properties of that interface.

Going back to the above example, we analyze that between the two classes Dog and Cat have their own actions, for example, dogs can bite (bite)... if we declare a common method (Bite) in the abstract class: animal, then we must apply this method to the Cat class (which is not reasonable).

We could also define a Bite method for the Dog class only, but think further... some other animals have the ability to "bite" too, and we want to abstract this method so we can use it in subclasses (if necessary), in this case, we use interface.

We can declare an interface like this: We all know C# supports inheritance, but a class can only inherit from one superclass (single inheritance). It does not support multiple inheritances. To solve this problem, we can use an interface. In the subclass, we implement the interface. A subclass can implement many different interfaces. So what is an interface?

We can declare an interface like this:


The Dog class implements the interface, here we also see that: by using the interface, the Dog class can realize the dream of multiple inheritances.


So:
- Use interfaces for multiple inheritances.
- Use interfaces to achieve abstraction in OOP.
- In addition, the use of interfaces also reduces dependencies between classes. Applied to achieve at a theory called "Solid" that you study about programming principles, the design pattern has probably been heard.

In this article, Mr. Bom only introduces abstract classes and interfaces, the simplest way to achieve two pillars in object-oriented programming is abstraction and inheritance. The problem about using the interface in a more advanced form, to reach the solid programming principle, it lies in another realm... see you in the next "Hoa Son discussion of code".
Share:
Google Account Video Purchases Hồ Chí Minh, Việt Nam

0 nhận xét:

Đăng nhận xét

Featured Posts

Data type 3 - string type