09-08-2012, 11:14 AM
Inheritance
1Inheritance.ppt (Size: 133.5 KB / Downloads: 221)
Terminology
Inheritance is a fundamental Object Oriented concept
A class can be defined as a "subclass" of another class.
The subclass inherits all data attributes of its superclass
The subclass inherits all methods of its superclass
The subclass inherits all associations of its superclass
The subclass can:
Add new functionality
Use inherited functionality
Override inherited functionality
What really happens?
When an object is created using new, the system must allocate enough memory to hold all its instance variables.
This includes any inherited instance variables
In this example, we can say that an Employee "is a kind of" Person.
An Employee object inherits all of the attributes, methods and associations of Person
Inheritance Hierarchy
Each Java class has one (and only one) superclass.
C++ allows for multiple inheritance
Inheritance creates a class hierarchy
Classes higher in the hierarchy are more general and more abstract
Classes lower in the hierarchy are more specific and concrete
There is no limit to the number of subclasses a class can have
There is no limit to the depth of the class tree.
The class called Object
At the very top of the inheritance tree is a class called Object
All Java classes inherit from Object.
All objects have a common ancestor
This is different from C++
The Object class is defined in the java.lang package
Examine it in the Java API Specification
Constructors and Initialization
Classes use constructors to initialize instance variables
When a subclass object is created, its constructor is called.
It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors so that the instance variables defined in the superclass are properly initialized
Superclass constructors can be called using the "super" keyword in a manner similar to "this"
It must be the first line of code in the constructor
If a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass.
Method Overriding
Subclasses inherit all methods from their superclass
Sometimes, the implementation of the method in the superclass does not provide the functionality required by the subclass.
In these cases, the method must be overridden.
To override a method, provide an implementation in the subclass.
The method in the subclass MUST have the exact same signature as the method it is overriding.
Polymorphism
In the previous slide, the two variables are defined to have the same type at compile time: BankAccount
However, the types of objects they are referring to at runtime are different
What happens when the withdraw method is invoked on each object?
anAccount refers to an instance of BankAccount. Therefore, the withdraw method defined in BankAccount is invoked.
account1 refers to an instance of OverdraftAccount. Therefore, the withdraw method defined in OverdraftAccount is invoked.
Polymorphism is: The method being invoked on an object is determined AT RUNTIME and is based on the type of the object receiving the message.
1Inheritance.ppt (Size: 133.5 KB / Downloads: 221)
Terminology
Inheritance is a fundamental Object Oriented concept
A class can be defined as a "subclass" of another class.
The subclass inherits all data attributes of its superclass
The subclass inherits all methods of its superclass
The subclass inherits all associations of its superclass
The subclass can:
Add new functionality
Use inherited functionality
Override inherited functionality
What really happens?
When an object is created using new, the system must allocate enough memory to hold all its instance variables.
This includes any inherited instance variables
In this example, we can say that an Employee "is a kind of" Person.
An Employee object inherits all of the attributes, methods and associations of Person
Inheritance Hierarchy
Each Java class has one (and only one) superclass.
C++ allows for multiple inheritance
Inheritance creates a class hierarchy
Classes higher in the hierarchy are more general and more abstract
Classes lower in the hierarchy are more specific and concrete
There is no limit to the number of subclasses a class can have
There is no limit to the depth of the class tree.
The class called Object
At the very top of the inheritance tree is a class called Object
All Java classes inherit from Object.
All objects have a common ancestor
This is different from C++
The Object class is defined in the java.lang package
Examine it in the Java API Specification
Constructors and Initialization
Classes use constructors to initialize instance variables
When a subclass object is created, its constructor is called.
It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors so that the instance variables defined in the superclass are properly initialized
Superclass constructors can be called using the "super" keyword in a manner similar to "this"
It must be the first line of code in the constructor
If a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass.
Method Overriding
Subclasses inherit all methods from their superclass
Sometimes, the implementation of the method in the superclass does not provide the functionality required by the subclass.
In these cases, the method must be overridden.
To override a method, provide an implementation in the subclass.
The method in the subclass MUST have the exact same signature as the method it is overriding.
Polymorphism
In the previous slide, the two variables are defined to have the same type at compile time: BankAccount
However, the types of objects they are referring to at runtime are different
What happens when the withdraw method is invoked on each object?
anAccount refers to an instance of BankAccount. Therefore, the withdraw method defined in BankAccount is invoked.
account1 refers to an instance of OverdraftAccount. Therefore, the withdraw method defined in OverdraftAccount is invoked.
Polymorphism is: The method being invoked on an object is determined AT RUNTIME and is based on the type of the object receiving the message.