310 likes | 322 Views
This chapter provides an introduction to the concept of inheritance in programming. Learn how to extend classes, use the protected access specifier, override superclass methods, and access superclass methods from a subclass. Understand the benefits of inheritance and how to work with superclasses that have constructors.
E N D
Chapter Seven Introduction to Inheritance
Objectives • Learn about the concept of inheritance • Learn inheritance terminology • How to extend classes • How to use the protected access specifier • How to override superclass methods
Objectives • How to access superclass methods from a subclass • How a subclass object “is an” instance of the superclass • About the Object class • How to work with superclasses that have constructors • How to work with superclass constructors that require arguments
Objectives • How to create and use abstract classes • How to create and use interfaces • Learn the benefits of inheritance
Understanding the Concept of Inheritance • Inheritance is the principle that states you can apply your knowledge of a general category to more specific objects • When you create a class by making it inherit from another class, you are provided with data fields and methods automatically • There are many advantages to using inheritance
Understanding the Concept of Inheritance • An Employee class
Understanding the Concept of Inheritance • The ability to use inheritance makes programs easier to write, less error-prone, and easier to understand
Understanding Inheritance Terminology • A class that is used as a basis for inheritance is called a base class • When you create a class that inherits from a base class, it is a derived class or extended class • You can use the terms superclass and subclass as synonyms for base class and derived class • You usually can distinguish base classes from their subclasses by size
Understanding Inheritance Terminology • A derived class can be further extended • Inheritance is transitive; that means a child inherits all the members of all its ancestors • When you create your own transitive inheritance chains, you want to place fields and methods at their most general level
Extending Classes • When you create a class that is an extension or child of another class, you use a single colon between the derived class name and its base class name • Inheritance works in one direction
Using the protected Access Specifier • Occasionally, you want to allow a subclass to access parent class data, while still abiding by the principle of information hiding • The keyword protected provides you with an intermediate level of security • A protected data field or method can be used within its own class or in any classes extended from that class, but it cannot be used by “outside” classes
Using the protected Access Specifier • Declaring empSal as protected and accessing it within CommissionEmployee
Overriding Superclass Methods • When you create a subclass by extending an existing class, the new subclass contains data and methods that were defined in the original superclass • Sometimes the superclass data fields and methods are not appropriate for the subclass objects • Using the same method name to indicate different implementations is called polymorphism
Overriding Superclass Methods • In the above child class, the SetCredits() method is declared as new because it has the same name and argument list as a method in its parent class—it overrides its counterpart
Overriding Superclass Methods • DemoStudents program and Output
Accessing Superclass Methods from a Subclass • A subclass can contain a method with the same name and arguments as a method in its parent class • When you want to use the parent class method within a subclass use the base keyword to access the parent class method • A method that calls itself is a recursive method
Understanding a Subclass Object “is an” Instance of the Superclass • Every subclass object “is a” specific instance of both the subclass and the superclass • You can assign a subclass object to an object of any of its superclass types. When you do so, C# makes an implicit conversion from subclass to superclass • C# also makes implicit conversions when casting one data type to another
Using the Object Class • Every class you create in C# derives from a single class named System.Object • The object (or Object) class type in the System namespace is the ultimate base class for all other types • Every class descends from Object
Using the Object Class • The Four public Instance Methods of the Object class
Using the Object Class • The GetType() method returns an object’s type, or class • Object class methods are usually overridden • The ToString() method can be useful for debugging • The Object class’s Equals() method returns true if two Objects have the same memory address
Working with Superclasses that have Constructors • When you instantiate an object that is a member of a subclass, you actually call two constructors • When you create any subclass object, the base class constructor must execute first
Using Superclass Constructors that Require Arguments • When you use a class as a superclass, and the class has a constructor that requires arguments, then you must make sure that any subclasses provide the superclass constructor with the proper arguments • The format of the statement that calls a superclass constructor is base(list of arguments) • C# does not allow you to call the superclass constructor by name, it must be called using the base keyword
Creating and Using Abstract Classes • An abstract class is one from which you cannot create any concrete objects, but from which you can inherit • An abstract method has no method statements; any class derived from a class containing an abstract method must override the abstract method by providing a body for it • When you create an abstract method, you provide the keyword abstract and the intended method type, name, and argument • When you create a subclass that inherits an abstract method from a parent, you must use the override keyword
Creating and Using Abstract Classes • Animal class
Creating and Using Abstract Classes • Dog and Cat classes
Creating and Using Abstract Classes • DemoAnimals program and Output
Creating and Using Interfaces • The ability to inherit from more than one class is called multiple inheritance • Multiple inheritance creates many complicated problems, and as a result is prohibited in C# • C# does provide an alternative to multiple inheritance in the form of interfaces
Recapping the Benefits of Using Inheritance • The benefits of inheritance are as follows: • Subclass creators save development time because much of the code that is needed for the class has already been written • Subclass creators save testing time • Programmers who create or use new subclasses already understand how the superclass works, so the time it takes to learn the new class feature is reduced • The superclass maintains its integrity
Chapter Summary • Inheritance is the principle that you can apply your knowledge of a general category to more specific objects • A class that is used as a basis for inheritance is called a base class • When you create a class that is an extension or child of another class, you use a single colon between derived class name and its base class name • If you could use private data outside of its class, the principle of information hiding would be destroyed
Chapter Summary • You can declare a child class method with the same name and argument list as a method within its parent class • When a subclass overrides a parent class method and you want to use the parent class version, you can use the keyword base to access the parent class method • Every subclass object “is a” specific instance of both the subclass and the superclass • Every class you create in C# derives from a single class named System.Object
Chapter Summary • When you instantiate an object that is a member of a subclass, you actually call two constructors • When you use a class as a superclass, and the class has a constructor that requires arguments, then within the header of the subclass constructor you must provide values for any arguments required by the base class constructor • An abstract class is one from which you cannot create any concrete objects • C# provides an alternative to multiple inheritance known as the interface