Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Inheritance

Description:

public class RockBand extends Band{ // these do not need to be declared (they're inherited) ... public RockBand(){ super(); public void makeNoise ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 18
Provided by: mccl8
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
  • Inheritance

2
Inheritance
  • Reusing code is great
  • Inheritance is a great way to reuse code
  • Result We want to do inheritance

Super class
Sub class1
Sub class2
3
Inheritance
  • Inheritance is a mechanism used to design two or
    more classes that are different, but share common
    features
  • We start with a class that defines the common
    features the super class
  • Then we define classes as extentions of the super
    class the sub classes

4
Inheritance
  • Defining the super class is just like any other
    class
  • public class Band
  • int volume
  • Instrument InstList
  • public Band()
  • volume 10
  • public void makeNoise( )

5
Inheritance
  • Each subclass uses the extends clause
  • public class RockBand extends Band
  • // these do not need to be declared (theyre
    inherited)
  • // int volume
  • // Instrument InstList
  • public RockBand()
  • super()
  • public void makeNoise( )
  • // inside of this is different than in Band

6
Inheritance
  • Each subclass uses the extends clause
  • public class Orchestra extends Band
  • // these do not need to be declared (theyre
    inherited)
  • // int volume
  • // Instrument InstList
  • public Orchestra()
  • super()
  • InstList new Instrument250
  • public void makeNoise( )
  • // inside of this is different than in Band

7
Inheritance
  • Several classes can be defined using the same
    base class
  • They can have the same data members and slightly
    different functionality
  • Or the same functionality with slightly different
    data members
  • Or slightly different data members and slightly
    different functionality

8
Inheritance
  • How are data members and methods inherited?
  • If the super class has a public data member or
    method
  • Inherited
  • If the super class has a protected data member or
    method
  • Inherited
  • If the super class has private data member
  • Not inherited

9
Inheritance
  • Why use protected?
  • From classes that do not inherit from this class,
    protected looks like private.
  • Make it visible to only special classes!
  • All other normal rules apply for all of these
    variables.

10
Inheritance
  • Questions???

11
Inheritance - Constructors
  • public class Base
  • public Base()
  • System.out.println("Base Class")

12
Inheritance - Constructors
  • public class DerivedClass extends Base
  • public DerivedClass()
  • System.out.println("DerivedClass")
  • If a call to super() isnt made, its
    automatically inserted at the top of the
    constructor

13
Inheritance - Constructors
  • public class MoreDerivedClass extends
    DerivedClass
  • public MoreDerivedClass()
  • System.out.println("MoreDerivedClass")
  • public static void main(String args)
  • Base c new MoreDerivedClass()
  • MoreDerivedClass g new MoreDerivedClass()

14
Inheritance - Constructors
  • What happens when this main is exectued?
  • public static void main(String args)
  • Base c new MoreDerivedClass()
  • MoreDerivedClass g new MoreDerivedClass()

15
Inheritance - Constructors
  • What happens when this main is exectued?
  • public static void main(String args)
  • Base c new MoreDerivedClass()
  • MoreDerivedClass g new MoreDerivedClass()
  • Base Class
  • DerivedClass
  • MoreDerivedClass
  • Base Class
  • DerivedClass
  • MoreDerivedClass

16
Inheritance
  • Questions??

17
Inheritance
  • Abstract classes and methods
  • Often we want to create a super class that
    contains all the common information of subclasses
  • But we dont want to create instances of the
    super class
  • This type of super class is an abstract class
  • It is common for an abstract class to have
    abstract methods that must be implemented by the
    sub classes
  • If a class has an abstract method, it must be an
    abstract class
Write a Comment
User Comments (0)
About PowerShow.com