Inheritance in C#. Abstract class. Polymorphism презентация

Слайд 2

AGENDA Implementation inheritance Abstract class Virtual methods Sealed classes and methods

AGENDA

Implementation inheritance
Abstract class
Virtual methods
Sealed classes and methods

Слайд 3

TYPES OF INHERITANCE Implementation inheritance means that a type derives

TYPES OF INHERITANCE

Implementation inheritance means that a type derives from a

base type, taking all the base type’s member fields and functions.
Interface inheritance means that a type inherits only the signatures of the functions and does not inherit any implementations.
Слайд 4

IMPLEMENTATION INHERITANCE Inheritance enables us to create new classes that

IMPLEMENTATION INHERITANCE

Inheritance enables us to create new classes that reuse, extend,

and modify the behavior that is defined in other classes.
The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.
The idea of inheritance implements the IS-A relationship. 

Animal

Dog

Shape

Circle

Doctor

Cardiology

class Animal {…}
class Dog: Animal
{ …}

class Shape{…}
class Circle: Shape
{ …}

class Doctor{…}
class Cardiology: Doctor
{ …}

Слайд 5

IMPLEMENTATION INHERITANCE A derived class can have only one direct

IMPLEMENTATION INHERITANCE

A derived class can have only one direct base class.


Inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

Person

Student

Staff

Developer

Teacher

Слайд 6

EXAMPLE

EXAMPLE

Слайд 7

THE base keyword IS USED TO ACCESS MEMBERS OF THE

THE base keyword IS USED TO ACCESS MEMBERS OF THE BASE CLASS FROM

WITH IN A DERIVED CLASS
Слайд 8

ABSTRACT CLASS An abstract class cannot be instantiated. The purpose

ABSTRACT CLASS

An abstract class cannot be instantiated.
The purpose of an

abstract class is to provide a common definition of a base class that multiple derived classes can share.
Abstract classes may define abstract methods.
Derived classes of the abstract class must implement all abstract methods.
Слайд 9

EXAMPLE

EXAMPLE

Слайд 10

INTERFACE vs ABSTRACT CLASS What is the difference between an

INTERFACE vs ABSTRACT CLASS

What is the difference between an abstract

class and an interface?
An abstract class can have fields and implementation of methods. 
An abstract class is essentially the same thing as an interface except it is an actual class, not just a contract.
abstract classes with virtual methods have better performance than interface implementation

Car

Plane

Boat

Labrador

Husky

German Shepherd Dog

Implements

Extends

Vehicles

Dog

Interface

Abstraction

Слайд 11

INTERFACE vs ABSTRACT CLASS

INTERFACE vs ABSTRACT CLASS

Слайд 12

VIRTUAL METHODS Virtual method - a method that can be

VIRTUAL METHODS

Virtual method - a method that can be overridden in

a derived class.
Overriding method - a change of its implementation in derived classes.
Static method can not be virtual
Слайд 13

VIRTUAL AND ABSTRACT METHODS Abstract method is a method that

VIRTUAL AND ABSTRACT METHODS

Abstract method is a method that does not

have its implementation in the base class, and it should be implemented in the derived class. Abstract method can be declared only in abstract class.
What is the difference between the virtual and the abstract method?
The virtual method can have its implementation in the base class, abstract - no (body is empty);
An abstract method must be implemented in the derived class, the virtual method is not necessary to override.
Announcement of the abstract method:
[модифікатор доступу] abstract [тип] [ім'я методу] ([аргументи]);
The implementation of the abstract method in the derived class occurs in the same way as the override of the method - using the keyword override:

[модифікатор доступу] override [тип] [ім'я методу] ([аргументи])
{
  // Реалізація методу
}

Слайд 14

ABSTRACT PROPERTIES Creating abstract properties is not very different from

ABSTRACT PROPERTIES

Creating abstract properties is not very different from the methods:
Realization

in the derived class:

protected [тип] [поле, яким управляє властивість];
[модифікатор доступу] abstract [тип] [ім'я властивості]{get; set;}
[модифікатор доступу] override [тип] [ім'я властивості]
{
  get {тіло аксессор get}
  set {тіло аксессор set}
}

Слайд 15

EXAMPLE

EXAMPLE

Слайд 16

EXAMPLE

EXAMPLE

Слайд 17

TASK 8 1. Add two classes Persons and Staff (use

TASK 8

1. Add two classes Persons and Staff (use the presentation

code)
2. Create two classes Teacher and Developer, derived from Staff.
Add field subject for class Teacher;
Add field level for class Developer;
override method Print for both classes.
3. In Main, specify a list of Person type and add objects of each type to it. Call for each item in the list method Print ().
4. Enter the person's name. If this name present in list - print information about this person
5. Sort list by name, output to file
6. Create a list of Employees and move only workers there. Sort them by salary.
Имя файла: Inheritance-in-C#.-Abstract-class.-Polymorphism.pptx
Количество просмотров: 65
Количество скачиваний: 0