Inheritance and Polymorphism - ABC programming lesson ppt

37 422 0
Inheritance and Polymorphism - ABC programming lesson ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Inheritance and Polymorphism ABC programming lesson Inheritance and Polymorphism  Welcome to the module Inheritance and Polymorphism.  Inheritance is a process of creating a new class from an existing class.  Inheritance allows you to inherit attributes and methods of the base class in the newly created class.  Polymorphism is a feature of OOP that allows the data members of a class to behave differently based on their parameters and data types.  In this module, you will learn about:  Implementing Inheritance  Method Overriding  Sealed Classes  Polymorphism #1 - Implementing Inheritance  Explain the concept of inheritance  Describe how to implement inheritance in C#.  State and explain the protected access modifier.  State and explain the base keyword.  State and explain the new keyword.  Describe how to inherit constructors. Inheritance  Inheritance specifies an “is a kind of" relationship  Inheritance is a class relationship  New classes specialize existing classes Musician Musician Violin Player Violin Player Base class Derived class Generalization Specialization Is this a good example of inheritance ? Purpose of Inheritance  Reusability  Reuse common methods and attributes among classes without recreating them.  Use the same code in different applications with little or no changes.  Generalisation  Specialisation  Extension Implementing Inheritance  Just need to insert a colon after the name of the derived class followed by the name of the base class.  The derived class can now inherit all non-private methods and attributes of the base class. Example of Inheritance Animal Animal Cat Cat "protected" Access Modifier  Protected variables or methods are accessed only by:  The class in which they are declared  Derived class  Specified using the protected keyword. "base" Keyword  Like “super” in Java  Access the variables and methods of the base class from the derived class.  Cannot use the base keyword for invoking the static methods of the base class. Example of Using "base" Keyword [...]... different situations  Polymorphism allows methods to function differently based on the parameters and their data types  Implement polymorphism in C# through:   Method overloading (Compile-time Polymorphism) Method overriding (Run-time Polymorphism) Example of polymorphism in C# A A B B C C Example of polymorphism in C# Compile-time and Run-time Polymorphism  Compile-time Polymorphism     Implemented... Copyright Problems  Prevent Inheritance Using Sealed Class Guidelines  If overriding the methods of a class might result in in unexpected functioning of the class  To prevent any third party from modifying your class #4 - Polymorphism  Define polymorphism in C#  Explain how to implement polymorphism  Describe compile and run-time polymorphism Define polymorphism in C#  Polymorphism means existing... Compile-time and Run-time Polymorphism  Compile-time Polymorphism     Implemented through method overloading Executed at the compile-time Referred to as static polymorphism Run-time Polymorphism    Implemented through method overriding Executed at run-time Referred to as dynamic polymorphism ... signature as the virtual method  Cannot override a non-virtual or static method  Both the override method and the virtual method must have the same access level modifier Declaring new Methods Calling the Base Class Method #3 - Sealed Classes  Define sealed classes in C#  Describe the purpose of sealed classes Sealed Classes  Class that prevents inheritance  Like “final” class in Java  To declare,... call to the base class’s default constructor before executing the code in the derived class constructor Compiler-Generated Constructor Code Can you see the Bugs? How to fixed Explicitly invoke the base class constructor #2 - Method Overriding  Define method overriding  Explain virtual and override keywords Method Overriding  Derived class to override or redefine the methods of the base class ... declaration Constructor Chaining Parent Parent F1 F1 F2 F2 Constructor Chaining Rules for Constructors (MUST be clear and remember)  A default constructor will be automatically generated by the compiler if no constructor is specified within the class  The default constructor is ALWAYS a no-arg constructor  If there is a constructor defined in the class, the default constructor is no longer used  If..."new" Keyword  As a modifier to hide the methods or variables of the base class that are inherited in the derived class Example of "new" Keyword Constructor Inheritance  In C#, cannot inherit constructors like inherit methods  The instance of the derived class will always first invoke the constructor of the base class followed by the constructor of the derived... overridden is called a virtual method  The method implemented in the derived class from the base class is known as the Overridden Base Method Implementing Method Overriding  Using appropriate "virtual" and "override" Keywords  To override a particular method of the base class in the derived class:   In the base class, declare the method using the “virtual” keyword In the derived class, declare the . Inheritance and Polymorphism ABC programming lesson Inheritance and Polymorphism  Welcome to the module Inheritance and Polymorphism.  Inheritance is a process of. on their parameters and data types.  In this module, you will learn about:  Implementing Inheritance  Method Overriding  Sealed Classes  Polymorphism #1 - Implementing Inheritance  Explain. Inheritance  Explain the concept of inheritance  Describe how to implement inheritance in C#.  State and explain the protected access modifier.  State and explain the base keyword.  State and explain the new

Ngày đăng: 09/07/2014, 13:21

Từ khóa liên quan

Mục lục

  • Inheritance and Polymorphism

  • Slide 2

  • #1 - Implementing Inheritance

  • Inheritance

  • Purpose of Inheritance

  • Implementing Inheritance

  • Example of Inheritance

  • "protected" Access Modifier

  • "base" Keyword

  • Example of Using "base" Keyword

  • "new" Keyword

  • Example of "new" Keyword

  • Constructor Inheritance

  • Constructor Chaining

  • Slide 15

  • Rules for Constructors (MUST be clear and remember)

  • Compiler-Generated Constructor Code

  • Can you see the Bugs?

  • How to fixed

  • Explicitly invoke the base class constructor

Tài liệu cùng người dùng

Tài liệu liên quan