inheritance java permits you to use your user defined classes to create programs using inheritance

29
Inheritance • Java permits you to use your user defined classes to create programs using inheritance.

Upload: darrell-dennis

Post on 18-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• Java permits you to use your user defined classes to create programs using inheritance.

Page 2: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance• All classes in Java are organized into a

class hierarchy.• The highest level classes are very general

and the lower level classes are more specific.• The lower level classes are based upon the

higher level classes and inherit the higher level class’ public and protected instance variables and methods. They also contain their own instance variables and methods beyond the higher level class definition.

Page 3: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance• The higher level classes are called

Superclasses and the new or lower level classes are called the subclasses.• A subclass can always become a superclass

• Inheritance allows you to define certain behaviours once and then to reuse those behaviours over and over again in the subclasses. This is called reusability.

Page 4: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors and extending them with the capabilities that the new class requires.

Page 5: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• When a class definition inherits instance variables and methods from another class:– The other class with the original instance

variables and methods is the super class.– The new class being defined is a subclass of

the super class.– An example is to think of your parents as a

super class and you as the subclass.

Page 6: Inheritance Java permits you to use your user defined classes to create programs using inheritance

InheritanceCommunityMember

AlumniStudentEmployee

Faculty Staff

Administrator Professor

Freshman Sophomore Junior Senior

Page 7: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• public class CommunityMember {}

• public class Employee extends CommunityMember {}

• public class student extends CommunityMember {}

• public class faculty extends Employee {}

Page 8: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance• The superclass should always contain the

common and generic instance variables and methods of the subclasses.

• The subclass should always contain the class specific instance variables and methods.– This may include more specific versions of

the methods which reside in the super class.

Page 9: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance• Subclasses

– Subclasses are usually bigger and more specific than the super class.

– Inherit all public and protected members of the super class even if the subclass does not use them.

– Methods and variables in the super class that should not be accessed by a subclass need to be defined as private.

– A method in a super class may be overridden in the subclass.

– A super class should never access a method or variable in a subclass.

Page 10: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• Subclasses– Subclasses can access the public, protected, and

package access members of it’s super class.• Package access is only available if both the super class and

the subclass are in the same package.

• Protected methods and variables can only be accessed by methods of a super class, a subclass, and a class in the same package.

• Page 668 of Wu states “A protected member is accessible only to the methods that belong to the same class or to the descendant classes. It is inaccessible to the methods of an unrelated class.” This is only true if the unrelated classes are not in the same package as the super and descendant classes.

Page 11: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

– Subclasses can not access the private members of it’s super class.

– A problem with inheritance is that the subclass may inherit methods that it doesn’t need or needs to use differently. The required implementation can be provided by overriding the method in the subclass.

Page 12: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• Overriding a super class method:– The method in the subclass must have the

same signature as the method in the super class.

– The subclass version is used when it is mentioned in the subclass or is specifically called in another class using the className.methodName().

Page 13: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• Direct inheritance occurs when subclass explicitly inherits from a super class.

• Indirect inheritance occurs when a subclass inherits from a super class two or more levels up in the class hierarchy.

Page 14: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance• When an object of a subclass is instantiated, the

superclass’s constructor is called to do any necessary initialization of the superclass instance variables. – This will be done automatically when the subclass

constructor is called.• When a subclass constructor is called:

– First the superclass constructor is executed then the subclass constructor is executed.

– Super class constructors are not inherited by subclasses.

• Subclass constructors can call Super class constructors.

Page 15: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• To access a method resident in the superclass while in the subclass use super.– super.methodName();

Page 16: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• Typically there will only be one finalize method in the highest level Super class. – The purpose of a finalize method is to do

any necessary clean up when the object is no longer needed.

– The finalize method should be defined as protected in the super class.

– The subclass can access it using:• super.finalize();

Page 17: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

– If a subclass defines a finalize method, then the last statement of the method should always be:

• super.finalize();

Page 18: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance vs. Composition• An is a relationship is implemented by

inheritance.– A Junior is a Student

• A has a relationship occurs when a class creates an instance of another class object and this is called composition.– A Junior has a Birthdate.

• Makes sense

– A Student has a Junior. • Makes no sense

Page 19: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Inheritance

• Example: – Animals

Page 20: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Final Methods

• A method may be defined as final just as a variable may be defined as final. – This means that a subclass is not able to

override a final method in the superclass.– Methods that are defined as static or

private are instances of final methods.

Page 21: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Final Classes

• A class may also be defined as final.– Such a class can not be a superclass.– All methods in a class that is defined to be

final are also considered final.

Page 22: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Abstract Classes• An abstract class is a class that never has

an object instantiated from it.– Abstract classes are only used as superclasses.– It’s purpose is to provide an appropriate general

superclass from which other classes may inherit.– An abstract class needs to be declared as such:

• public abstract class className{}

Page 23: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Concrete Classes

• A concrete class is a class from which objects may be created. – Concrete classes provide specific methods

and variables that make it reasonable to instantiate objects.

• These are the types of classes we have used thus far.

Page 24: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Polymorphism

• Polymorphism means that different objects respond distinctly to the same message.– Through polymorphism, one method call

can cause different actions to occur depending on the type of the object receiving the call.

Page 25: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Example

• Example: Shape, Point, Circle, Cylinder

Page 26: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Interfaces• An Interface is a special kind of class that

defines the specifications of a set of methods. – It is a way to obtain abstraction.– It begins with the keyword interface.– It contains a set of public abstract methods or a

set of constants to be used in many class definitions.

– To use in a class, the class must specify that it implements interfaceName.

Page 27: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Interfaces

– An interface is typically used instead of an abstract class when there is no default implementation to inherit.

– When a class implements an interface, the same is a relationship is provided by inheritance.

Page 28: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Interfaces

• Benefits– A class can implement as many interfaces

as it needs in addition to extending a class.• This is done by:

public class className2 implements interfaceName1, interfaceName2, interfaceName3 extends className1 {}

Page 29: Inheritance Java permits you to use your user defined classes to create programs using inheritance

Interfaces

• Example:– Inventory– Queues