Inheritance - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Inheritance

Description:

Inheriting Methods. Subclasses inherit superclass methods declared as public or protected (or not ... Subclasses don't inherit a superclass method if the ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 22
Provided by: richardu
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Inheritance
2
Overloading
  • Overloading refers to the ability to allow
    different methods or constructors of a class to
    share the same name.
  • The legality of overloading depends on the
    signature of the methods and constructors.

3
Rule
  • Two methods or constructors in a class can share
    the name if they have different signatures.

4
Extending Classes
  • Inheritance defines a relationship between
    classes.
  • superclass, subclass
  • All public and protected members of the
    superclass are accessible in the subclass.

5
Example - Conceptual
Account
Checking Account
Savings Account
6
Example - Source
  • public class Account
  • protected float minimumbalance100.0f
  • public class SavingsAccount extends Account
  • private float minimumbalance250.0f
  • public class CheckingAccount extends Account

7
Protected
  • public makes a member accessible by everyone.
  • private makes a member only accessible by
    instances of the class.
  • protected makes a member accessible by instances
    of the class and instances of any subclass.

8
Inheriting Methods
  • Subclasses inherit superclass methods declared as
    public or protected (or not specified).
  • Subclasses dont inherit a superclass method if
    the subclass defines a method using the same
    name. The subclass method is said to override
    the one in the parent.
  • Subclasses dont inherit private methods.
  • Subclasses dont inherit constructors.

9
Constructors
  • The initialization of extended classes requires
  • the initialization of the fields inherited from
    the super class
  • the initialization of the fields declared in the
    extended class.

10
Example (1)
  • public class CheckingAccount extends Account
  • CheckingAccount ()
  • minimumbalance250.0f
  • public class SavingsAccount extends Account
  • SavingsAccount ()
  • public class Account
  • float minimumbalance
  • Account()
  • minimumbalance100.0f

11
Example (2)
  • public class TrivialApplication
  • public static void main(String args)
  • SavingsAccount myAcctnew SavingsAccount()
  • System.out.println( myAcct.minimumbalance )

12
Default
  • If no constructor is defined in the extended
    class, the default constructor is provided by
    default.
  • public class Extended extends Super
  • public Extended ()
  • super()

13
Abstract Class
  • An abstract class can be created using the
    modifier abstract in the declaration. No
    instances of an abstract class can be created.

14
Example
  • abstract class Student
  • protected final static int num_tests3
  • protected String name
  • protected int test
  • protected String courseGrade
  • public Student()
  • this(No Name)
  • public Student(String name)
  • this.namename
  • testnew intnum_tests
  • courseGrade
  • class GraduateStudent extends Student
  • class UndergraduateStudent extends Student

15
Polymorphism
  • In its simplest form, polymorphism allows a
    variable to refer to objects from different
    classes.
  • Student s
  • snew GraduateStudent()
  • snew UndergraduateStudent()

16
Rule of Assignment
  • The type of the expression on the right-hand
    side of an assignment must be a subtype of the
    type of the variable on the left-hand side.

17
Example
  • Student s1, s2
  • s1new Undergraduate()
  • s2new Graduate()
  • Graduate s3
  • s3s2//compilation error
  • s3(Graduate)s2

18
Casting
  • Casting a variable to a subtype is called
    downcasting. Java allows explicit casting of any
    reference type to any other reference type at
    compile time.
  • The validity of an explicit cast is checked at
    run time. If it is invalid, a ClassCastException
    will be thrown.

19
Example
  • Student s1, s2
  • s1new Undergraduate()
  • s2new Graduate()
  • Graduate s3
  • s3(Graduate)s1//run time error, exception
    thrown

20
Proper Form
  • Use instanceof before downcasting.
  • if (student1 instanceof Graduate)
  • else
  • Catch the ClassCastException
  • try
  • Graduate g(Graduate) s1
  • catch (ClassCastException e)

21
Overriding Methods
  • Overriding refers to methods of different classes
    that have an inheritance relationship.
  • Methods have the exact same signature, name, and
    return type.
Write a Comment
User Comments (0)
About PowerShow.com