intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Inheritance and Polymorphism - ABC programming lesson

Chia sẻ: Trần Công Chính | Ngày: | Loại File: PPT | Số trang:37

83
lượt xem
7
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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

Chủ đề:
Lưu

Nội dung Text: Inheritance and Polymorphism - ABC programming lesson

  1. Inheritance and Polymorphism ABC programming lesson
  2. 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 
  3. #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. 
  4. Inheritance Inheritance specifies an “is a kind of" relationship   Inheritance is a class relationship  New classes specialize existing classes Generalization Musician Base class Is this a Violin Specialization Derived class good Player example of inheritance ?
  5. 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 
  6. 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.
  7. Example of Inheritance Animal Cat
  8. "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. 
  9. "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.
  10. Example of Using "base" Keyword
  11. "new" Keyword As a modifier to hide the methods or variables of the base  class that are inherited in the derived class.
  12. Example of "new" Keyword
  13. 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 class. Can explicitly invoke the base class constructor by using the  base keyword in the derived class constructor declaration
  14. Constructor Chaining Parent F1 F2
  15. Constructor Chaining
  16. 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 you don’t explicitly call a base class constructor in a derived  class constructor, the compiler attempts to silently insert a call to the base class’s default constructor before executing the code in the derived class constructor
  17. Compiler-Generated Constructor Code
  18. Can you see the Bugs?
  19. How to fixed
  20. Explicitly invoke the base class constructor
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2