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:

Data type 3 - string type

In any programming language, to represent the value of a variable, we need to declare the data type of that variable. 
In previous lessons, Bờm reviewed value types and reference types. Following that series, Bờm wrote something about a data type that we also use a lot to process strings, which is String. So what do we know about string type in C# language? With limited knowledge, I will only point out a few basic points about strings as a way to review.

Yeah, let's go:

1. What is String? string and System.String are the same or different?
For string processing .Net framework provides us with a class: System.String (fields, methods, constructor...). A String in C# is an object of type String, its value is text. This means that if you want to store information of a string, a text, you can completely use string type to store. 

            // String of characters 
            System.String nickName = "Victor";

            // String made of an Integer 
            System.String age = "28";

Many of you also asked if string and System.String is different or the same, oh good question... When I was a student, I used to ask the same question... And the answer is: in In C#, the keyword string is an alias of String, so the two notes string and String are basically the same. In programming you want to use string or String keyword, it's up to you!!!

2. String is an immutable.
This means you can't change the string once it's created.

For example:
          string string1 = "Hello World!!";

It will occupy a memory area on the heap to store this string.

If you want to change the value of string1 by adding to string1 the value "from Tutorials teacher". You think: the system just needs to change the value of string1 by adding the value "from Tutorials teacher" and it's ok...

Oh No, because of its immutable nature, it will create a new string "Hello!! from tutorials teacher" and assign this value to string1.
(image: string_memory; source: tutorialteacher.com)

Unprecedented magic and this magic brings a disadvantage that if we modify the string1 many times, it also creates many new strings, result in reduced program performance.

To solve this problem, C# martial arts secrets give us a spell String "Biu Do".

3. String Builder
String builder helps to increase performance when working with string type when we need to edit, delete, or any operation on string. It increases performance because each time it manipulates a string, it doesn't create any new strings, simply manipulates the string itself.


(image: stringbuilder_memory; source: tutorialteacher.com)

Let see the below example:

Yup!! Here's the result, surprise!!!


In short, string builder is useful when working with string concatenation and string operations, it can increase performance when manipulating strings.

When working on strings, we need to call methods or apply operations on them. So what happens when our string variable has a value of null.

4. Null string in C#
A null string is a variable of type string but it is uninitialized or has a value of null.
If we call a property or a method on a null string, boom.... an exception will be thrown... This makes us think, to get the best handling code. A small note, but martial.

use the method: IsNullOrEmpty

The above is just a little basic theory about string so that we have the concept of null string, what is an immutable string? how to solve performance problems when working with strings... There are many things about strings, such as helper methods, properties, fields... I think the basic knowledge of what string is, string is immutable it needs to be stored in our memory before we find a way to apply it to real string processing problems.

Related Posts:

Share:

Featured Posts

Data type 3 - string type