objectives introduction to inheritance and composition (subclasses and superclasses) overriding (and...
TRANSCRIPT
![Page 1: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/1.jpg)
Objectives
• Introduction to Inheritance and Composition (Subclasses and SuperClasses)
• Overriding (and extending), and inheriting methods and constructors from the SuperClass of an inherited class
• Understand what an interface class is and how to use it
• Introduction to abstract classes
![Page 2: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/2.jpg)
Encapsulation
• Combine data and operations in a single unit known as a class
• The class is a self-contained entity. The internal state of an object cannot be manipulated directly
![Page 3: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/3.jpg)
Inheritance and Composition
• When a new class is defined it may be a special case, or a generalization of an existing class
• Inheritance (is a) and composition (has a) can then be used to build a class that has the properties ( elements, methods) of the existing class with additions and/or changes.
![Page 4: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/4.jpg)
Inheritance• The object created by and Inherited class
“IS A” object that could be considered as an incidence of the super class (or base class)
• A dog “IS A” pet• A square “IS A” rectangle• BUT a wall “HAS A” rectangle
(composition, we will revisit this later)
![Page 5: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/5.jpg)
Inheritance Heirarchy
Superclass or base class
Subclass or derived class
Shape
Square
RectangleCircle
![Page 6: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/6.jpg)
Inheritance in Java
• To implement inheritance
public class Square extends Rectangle
{
// implements class Square
}
![Page 7: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/7.jpg)
Why Inherit?
• The base class already has elements constructors and methods to work with the object created by the class
• The inherited class will automatically have the default constructor, and the public or protected elements and methods of the super class
• NOTE: private elements and methods are not inherited.
• The class can then be extended by adding or overriding methods or adding variable elements
![Page 8: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/8.jpg)
protected
• What is the meaning of protected
• Protected– Protected elements can be accessed by inherited
classes– Protected elements cannot be accessed outside
the base class and it’s inherited classes
![Page 9: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/9.jpg)
Types of Inheritance
• Java supports single inheritance, that is inheritance from one class
• Some other languages also support multiple inheritance (inheriting from more than one class)
![Page 10: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/10.jpg)
Inheritance: constructors• Your derived class will automatically
inherit the default constructor from your base class
• Other constructors must be included in your derived class
• You can efficiently add to the constructors (including the default constructor) in the base class by implementing them in your derived class using super()
![Page 11: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/11.jpg)
Using super() to extend Constructors
• If you want the constructor in your derived class to do everything the constructor of your base class does, and a few extras, the constructor in your derived class should look like
• public derivedClassName( parameter list ){
super( parameter list );// added functionality if any
}• For the default constructor the parameter list will be empty
![Page 12: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/12.jpg)
super() and parameters
• When we build a constructor in an inherited class it can have– The same number and kind of parameters as a
constructor in the base class. • Extend using super(parameter list) and the same parameter list
as the constructor in the base class
– A different number or kind of parameters as a constructor in the base class
• This is a case of overloading. We have increased the overloading options for the constructors. The new overloaded constructor will be available to the derived class and not the base class
![Page 13: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/13.jpg)
super() and private members
• Inheritance does not allow direct access to private members of the base class
• private members of the base class need to be initialized by the constructors of the derived classes
• super() allows the base class to initialize its private members, without providing the derived class direct access to them
![Page 14: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/14.jpg)
Inheritance and Methods
• The public methods of the base class can be directly used on objects of the derived class.
• Any of the public methods in the base class can be extended, overridden, or overloaded in the derived class
• Additional methods can be added to the derived class that are not part of the base class
![Page 15: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/15.jpg)
Methods and the use of super• In the derived class a method to be overridden or overloaded has
the following syntax
public methodtype methodname( parameter list1 ){
super.methodname( parameter list2 );// added functionality if any
}
• Parameter list2 must always match the parameter list for an instance of this method in the base class
• To override (extend) a method from the base class the parameter list1 must match a parameter list of the method in the superclass.
• To overload parameter list1 does not match the parameter list in any occurrence of the method in the base class
![Page 16: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/16.jpg)
Abstract Methods
• An abstract method is a method that– Specifies a method name type and parameter
list– Provides no code to implement the method – For example:
• public void abstract print()
• Public abstract insert(int insertItem)
![Page 17: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/17.jpg)
Abstract Classes• Can contain
– instance variables – Constructors– abstract and non abstract methods
• Any class containing an abstract method must be an abstract class
• You cannot declare an instance of an abstract class• You can declare an instance of a class inherited
from an abstract class ONLY if all the abstract methods in the abstract class are implemented in the derived class
![Page 18: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/18.jpg)
Interfaces• An interface is a class that contains only
abstract methods and/or named constants
• Java does not support multiple inheritance, A derived class can inherit from only one base class
• A class can use more than one interface.
• A class can inherit from a single class and use interfaces
![Page 19: Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649d0b5503460f949dedc5/html5/thumbnails/19.jpg)
Use of an interface
• In Java a class implements an interface• Within the class implementing the interface all
methods in the interface must be definedpublic class ClassName implements InterfaceName{
//Define variable elements of class// Constructors for class ClassName//code defining each of the methods in the interface// Any other methods for class ClassName
}