OOP, objects, classes. Inheritance and Interfaces

Table of contents:

OOP, objects, classes. Inheritance and Interfaces
OOP, objects, classes. Inheritance and Interfaces
Anonim

Beginning to write a program, a modern developer must first of all think over the architecture of the future project. From the point of view of an object-oriented approach to software development, this means that the programmer must create code that will consist of logical blocks (classes and objects) and think over their interaction. The code must be maintainable. This means that in the future this program can be painlessly supplemented or expanded without changing its main components.

Object approach
Object approach

In the object-oriented development paradigm, there are three main tools that developers use to write good code:

  • Encapsulation.
  • Inheritance.
  • Polymorphism.
  • Inheritance.

Inheritance

This is the process of creating a new class based on an existing one. The object-oriented approach is primarily used to describe the concepts and phenomena of the real world in a programming language. Therefore, in order to understand OOP, one must learndescribe the task facing the programmer, the language of classes and objects.

The concept of inheritance
The concept of inheritance

In order to understand the essence of the process of inheritance, we can give an example from life. There is an abstract concept of "man". Any person has general characteristics that can describe him. This is height, weight, hair color, voice timbre, and so on. Ivanov Ivan, student of class 1 "A" is a specific representative of the concept of "man", that is, in the language of an object-oriented approach, the concept of Ivanov Ivan is the heir to the concept of man. Or, for example, the neighbor's cat Barsik is the heir to the concept of "cat". You can make a more complex inheritance: the concept of Barsik is inherited from the concept of Maine Coon, which in turn is inherited from the concept of "cat".

How is this described by the programming language?

In software development, the process of inheritance makes it possible to create a class from others. The parent class is called the base class, and the rest of the descendants are derived. Borrowing means that the methods, properties and fields of the parents are transferred to the descendants. The syntax for class inheritance in the C programming language might look like this. For example:


class Ivan: Person { // class body {

Example of class inheritance mechanism:


class Person { public int Age { get; set; } } class Ivan: Person { public void Talk() { // Ivan is talking } } class Peter: Person { public void Walk() { // Peter is walking } } class Program { static void Main(string[] args) { Ivan ivan=newIvan(); ivan. Age=29; // designate Peter's age peter=new Peter(); peter. Age=35 ivan. Talk(); peter. Walk()(); } }

Derived classes are created based on the Person base class, and the Age property is available for both classes.

Base class constructor

When creating a class with a declared constructor and a derived class, a logical question arises: which constructors will be responsible for what when creating objects of these classes?

If it is set only in the inherited class, then when creating a child object, the default constructor (parent) is called, and then its receiver.

If constructors are defined in both the parent and child classes, then the base keyword is used to call the base one. When declaring such an inheritance structure, the following syntax is used to specify a constructor:


public Ivan (int age): base (age) { //constructor body }

Inheritance of class methods is the most important property of OOP. When creating an object of a derived class, the arguments of the base class constructor are passed to it, after which its constructor is called, and then the rest of the calls to the inheritor constructor are executed.

Access fields from child class

Inheritance of access to the properties and methods of the base class from the derived class becomes possible if all of them are declared with the following keywords:

1. Public.

Grants unlimited access to class members: from Main, from derived classes, from other analogues.

2. Protected.

Access tothe member remains only from the original class, or from derived analogues.

3. Internal.

The member constraint is localized only by this project assembly.

4. Protected internal.

Restriction to a member is limited only by this assembly of the project or from derived classes.

Access Modifiers
Access Modifiers

Inheritance access to class members depends on keywords. If a member is not declared as such, accessibility is set by default (private).

Multiple inheritance

Interfaces are a contract that guarantees that a set of methods will be implemented in a class that inherits from an interface. The latter do not contain static members, fields, constants, or constructors. It is illogical and you cannot create interface objects.

Interface examples
Interface examples

Structure data is created outside of classes, declared using the interface keyword:


interface IMyInterfave { void MyMethod (int someVar); }

It's good practice when creating interfaces to start the name of a construct with a capital letter I so that they can be easily distinguished later on. The body of the interface declares the signatures of its members.

To indicate that a class will implement an interface, you must, just as with normal inheritance, specify the name of the construct after the class declaration:


class MyClass: IMyInterface { public void MyMethod (int someVar) }

Interfaces are required in order to implement multiple inheritance. ATC++ programming language, scissor inheritance of classes is possible; C uses interfaces for this purpose. A class can implement multiple interfaces, in which case they are listed with a comma after the class declaration:


class MyClass: IMyFirstInterface, IMySecondInterface { //class body }

By default, all properties and methods of interfaces have public access, because they must define some functionality that will be further implemented in the class. Inheritance in C almost always involves the use of this handy tool.

binary code
binary code

A little more about class inheritance

Java, С, С++ - all object-oriented languages implement the inheritance mechanism in their own way. But the essence is the same everywhere: to transfer the properties of the parent to the child object. Interfaces, abstract classes are all handy tools for this task.

Popular topic

Editor's choice

  • IPv6 protocol: setup on Windows systems
    IPv6 protocol: setup on Windows systems

    Probably, many users of computer systems, delving into the network settings, noticed that in the list of protocols, in addition to the well-known IPv4, there is also the sixth version (IPv6)

  • View history on computer
    View history on computer

    Today you can learn about almost all the actions taken on the computer. It's about browsing history

  • Computer technology hardware: definition, description and types
    Computer technology hardware: definition, description and types

    Modern computers to ensure maximum performance and correct operation use hardware and software that are very interconnected and clearly interact in different directions. Now let's touch on the consideration of hardware, since initially it is they who occupy a dominant position in ensuring the operability of any computer or even mobile system

  • Keyboard shortcuts and the evolution of operating systems
    Keyboard shortcuts and the evolution of operating systems

    It takes about a minute to copy or move a file from one location to another using the menu system. The developers introduced a keyboard shortcut that replaced all these actions and made it possible to reduce the time of this common operation by 3 or more times. True, for this it was necessary to slightly modernize the keyboard: new keys were added - "Ctrl", and later - "Windows"

  • If the torrent does not download
    If the torrent does not download

    An article for those who consider the ability to download files from torrent trackers a convenience, not an echo of the past. Not only the unavailability of "high-speed" tariff plans is the reason for the choice, but also personal preferences