Objectives - PowerPoint PPT Presentation

About This Presentation
Title:

Objectives

Description:

Extend using super(parameter list) and the same parameter list as the ... super() allows the base class to initialize its private members, without ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 20
Provided by: manasi48
Category:
Tags: objectives | super

less

Transcript and Presenter's Notes

Title: Objectives


1
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

2
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

3
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.

4
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)

5
Inheritance Heirarchy
  • Superclass or base class
  • Subclass or derived class

Shape
Rectangle
Circle
Square
6
Inheritance in Java
  • To implement inheritance
  • public class Square extends Rectangle
  • // implements class Square

7
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

8
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 its inherited classes

9
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)

10
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()

11
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

12
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

13
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

14
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

15
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

16
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)

17
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

18
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

19
Use of an interface
  • In Java a class implements an interface
  • Within the class implementing the interface all
    methods in the interface must be defined
  • public 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
Write a Comment
User Comments (0)
About PowerShow.com