Week 8 Recap - PowerPoint PPT Presentation

About This Presentation
Title:

Week 8 Recap

Description:

Concrete subclass inherits all public methods from abstract class and they ... One more use of extends. Concrete classes can also extend other concrete classes. ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 18
Provided by: cseBu
Learn more at: https://cse.buffalo.edu
Category:
Tags: and | concrete | more | recap | week

less

Transcript and Presenter's Notes

Title: Week 8 Recap


1
Week 8 Recap
  • CSE 115 Spring 2007

2
Composite Revisited
  • Once we create a composite object, it can itself
    refer to composites.
  • Therefore, we have a structure which can grow
    infinitely large depending on what we add to it.

3
Inheritance
  • Between Interfaces
  • Between Classes

4
Interface -gt Interface
  • Formal name for this relationship is
    generalization
  • Informal name extends
  • Code
  • public interface InterfaceA extends InterfaceB

5
What does extends do?
  • All public methods declared in InterfaceB become
    part of InterfaceA and remain public.

6
Important to classes that implement InterfaceA
  • Now those classes must provide implementations
    for all methods from InterfaceB as well as those
    methods declared in InterfaceA.

7
New type Abstract Classes
  • Classes that have an abstract method
  • Abstract method A method that is undefined
  • Provides partial implementation (of an interface
    perhaps)

8
Relationships with Abstract Classes
  • Abstract Classes can implement interfaces.
  • Abstract Classes can extend other Abstract
    Classes.
  • Concrete Classes can extend Abstract Classes.

9
Abstract Class implements Interface
  • Can provide some or all of the implementation for
    the methods declared in the interface.
  • At least one method in the abstract class still
    remains undefined. This can be an interface
    method or a completely new method.

10
Abstract Class extends Abstract Class
  • The subclass inherits all public methods of the
    superclass and they remain public in the
    subclass.
  • The subclass can implement some or all of the
    abstract methods from the superclass, however,
    once again at least one method in the subclass
    remains abstract.

11
Restriction on extends
  • Only one class name can appear after the keyword
    extends in a Java file.
  • Therefore, a class can only extend one other
    class, whether that class is abstract or concrete.

12
Concrete Class extends Abstract Class
  • Concrete subclass inherits all public methods
    from abstract class and they remain public in the
    subclass.
  • Concrete class MUST provide implementation for
    all abstract methods from the superclass.

13
One more use of extends
  • Concrete classes can also extend other concrete
    classes.
  • The subclass once again inherits all public
    methods of the superclass and they remain public
    in the subclass.

14
Constructors and Inheritance
  • When you create an instance of a class that
    extends another class, the constructor of the
    subclass must make a call to the constructor of
    the superclass.
  • By default, the no argument constructor of the
    superclass is called.

15
Constructors and Inheritance
  • If no no-argument constructor exists in the
    superclass, the subclass must explicitly call
    another constructor from the superclass using the
    keyword super and passing the appropriate
    parameters.

16
Method Overriding
  • When we use inheritance, we inherit all the
    public methods from the superclass.
  • What if we dont like the way a particular method
    is defined in the superclass and want to change
    it.
  • We can use method overriding.

17
Method Overriding
  • Defining a method in the subclass that has the
    same method header as one in the superclass.
  • Two types of overriding
  • Partial Calls the superclass method and adds on
    functionality.
  • Total Completely ignores superclass
    functionality and provides totally new definition
    for the method.
Write a Comment
User Comments (0)
About PowerShow.com