Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inheritance Java permits you to use your user defined classes to create programs using inheritance."— Presentation transcript:

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

2 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.

3 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.

4 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.

5 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.

6 Inheritance CommunityMember Alumni Student Employee Faculty Staff Administrator Professor FreshmanSophomoreJuniorSenior

7 Inheritance public class CommunityMember {} public class Employee extends CommunityMember {} public class student extends CommunityMember {} public class faculty extends Employee {}

8 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.

9 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.

10 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.

11 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.

12 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().

13 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.

14 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.

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

16 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();

17 Inheritance –If a subclass defines a finalize method, then the last statement of the method should always be: super.finalize();

18 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

19 Inheritance Example: –Animals

20 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.

21 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.

22 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{}

23 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.

24 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.

25 Example Example: Shape, Point, Circle, Cylinder

26 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.

27 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.

28 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 {}

29 Interfaces Example: –Inventory –Queues


Download ppt "Inheritance Java permits you to use your user defined classes to create programs using inheritance."

Similar presentations


Ads by Google