Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance & Method Overriding BCIS 3680 Enterprise Programming.

Similar presentations


Presentation on theme: "Inheritance & Method Overriding BCIS 3680 Enterprise Programming."— Presentation transcript:

1 Inheritance & Method Overriding BCIS 3680 Enterprise Programming

2 Overview  Inheritance  How to create a subclass  Methods in super- and subclasses  Constructors  Method overriding  Abstract methods  Interfaces 2

3 Inheritance  If we are dealing with a group of objects that share certain common traits while in the mean time each group sports some uniqueness of its own, then we may consider creating a class hierarchy, e.g.,  Different types of vehicles;  Different types of students.  A superclass is a general class that defines traits common to a set of related classes (subclasses).  A subclasses inherits those common variables and methods defined in the superclass. Each subclass has its own unique traits in addition to the shared ones. 3

4 A Sample Vehicle Hierarchy  This hierarchy is depicted using a Unified Modeling Language (UML) diagram.  In UML diagrams, hollow arrows point from the subclass to the superclass. 4

5 Superclasses and Subclasses  A superclass can have multiple subclasses.  A subclass, in turn, can be the superclass of other subclasses.  A subclass can inherit directly from only one superclass.  All classes in Java ultimately inherit from the Object class.  The object class contains a toString() method. Therefore, we can call the toString() method on all classes in Java, including those we define.  For an object, this method typically returns the class name and the object’s memory location. 5

6 Inheritance  The extends keyword class extends public class Graduate extends Student  A subclass inherits the variables and methods of a superclass. 6

7 Constructors  The constructor in the superclass initializes variables that are defined in the superclass.  Constructors are not inherited.  However, a subclass may have its own constructor(s).  Typically, a subclass constructor…  May call the constructor in the superclass to initialize inherited variables, i.e., those defined in the superclass; and then it has additional code to initialize those variables defined in the subclass.  May even initialized all variables (inherited or non-inherited) in its own way. 7

8 Accessing Superclass Members  To access a variable or method in the superclass, use the super keyword.  To access a variable – super.variableName  To access a method – super.methodName([args])  To access a constructor – super([args])  Notice when accessing a superclass constructor, no method name or class name is needed, e.g, if Graudate inherits from Student :  Correct: super(i, n, g, m, gpa);  Incorrect: super.Student(i, n, g, m, gpa); 8

9 Adding New Members 9  It would be pointless if a subclass didn’t need any unique variables and/or methods of its own.  Therefore, the subclass often adds its own variables.  It may add new methods and/or modify methods defined in the superclass.  How a subclass modify superclass-defined methods:  It may add to the behaviors defined in the superclass version.  It may even change the behaviors entirely.  This is called method overriding. In other words, the version in the subclass “overrides” the one defined in the superclass.

10 Method Overriding  When a method in a subclass has the same return type and signature as a method in its superclass (in other words, they have identical header), then the method in the subclass is said to override the method in the superclass.  Note that even if a method is not overidden, it still is accessible in the subclass by virtue of inheritance.  When called from an object of a subclass, (e.g., undergradObj.retrieveInfo() ):  If the method is overridden, the version of that method that is defined in the subclass is run.  If the method is not overriden, the version of that method that is defined in the superclass is run. 10

11 Rules for Overriding  The return type must be the same for both the overriding and overridden methods.  The parameter list must be exactly the same.  Static methods cannot be overridden. 11

12 Review: Overloading 12

13 Overloading vs. Overriding 13 OverloadingOverriding WhereWithin the same classBetween superclass and subclass PurposeTo have various versions of a method so that callers of the method have flexibility in using the method To change or add to an action (method) that is defined in the superclass so that the method defines how the subclass acts in its own way (as opposed to how the superclass or other subclass would act) Method NameMust be the same Parameter ListMust be differentMust be the same Return Data TypeNot relevantMust be the same

14 Overriding 14

15 Polymorphism 15  A reference variable can refer to and be used to store objects of types different from its own, as long as those types are subclasses of its type.  For example, Undergrad and Graduate are subclasses of Student. Therefore, all the following are legal:  Student s1 = new Student();  Student s2 = new Undergrad();  Student s3 = new Graudate();

16 Polymorphism 16  If you want to store 100 students sequentially in an array but won’t be able to tell until run time whether a student is undergraduate or graduate, then creating an array for undergraduate and another for graduate…  Functionally fails the business requirements (sequential storage while u/g status is random);  Coding wise is awkward.  You can declare the array as a Student array. Then in run time, regardless of whether a student is undergraduate or graduate, the object can be stored in the next available array element.

17 17

18 Polymorphism 18  With each element, you can call a method (defined in the superclass and possibly overriden in the subclasses) with the same statement, regardless of the type of the object (undergrad or graduate). for (int i = 0; i < sa.length; i++) { sa[i].retrieveInfo(); }

19 Abstract Methods 19  Sometimes, each subclass carries out a particular behavior in very different ways, but for some reasons (e.g., “polymorphism”), we want to define the behavior as a method at the superclass level, even though no specific code is possible due to the substantial variation among the subclasses.  In this case, we simply place a method header in the superclass, without method body.  Instead of the pair of curly braces and code contained within, we add only a semicolon to the end of the parameter list.  Such a method is called an abstract method.  Each subclass is responsible for fleshing out how the method works. In this case, the subclass implements (instead of “overrides”) the abstract method.

20 Defining an Abstract Method  To declare a method as abstract, include the abstract keyword in the method header, and end the header with a semicolon: accessModifier abstract returnType methodName( [parameter list] ); public abstract writeThesis();  The semicolon at the end of the header indicates that the method contains no code.  There are no opening and closing curly braces. 20

21 Abstract Classes 21  As long as there is one abstract method in a class, this class becomes an abstract class.  To make a class abstract, add the abstract keyword in the class header, accessModifier abstract class ClassName { // class body } public abstract class Student {... }  You cannot instantiate (create) an object of an abstract class. However, you can declare an object of that class.

22 Interface 22  If a subclass wants to be a concrete class (a class that can be instantiated), then it must implement all abstract methods in the abstract superclass.  This can be a bit rigid in scenarios where we want an abstract method or a set of abstract methods to be implemented:  By only some but not all subclasses in an inheritance hierarchy;  By classes from various inheritance hierarchies. Despite being from different hierarchies, these classes all display certain capabilities as suggested by the abstract methods.  The solution is to group these abstract methods into a special class called interface.  A interface name often contains the –able or –er / -or suffix.

23 Interface 23  If a class implements an interface, it fleshes out the behaviors by implementing abstract methods contained in the interface.  With interfaces, we can say, “This, this, and this class should be able to perform this set of actions, but we will let each of them decide how it actually performs those actions.”  For example, a dog, a cat, and a parrot are very different species, but they all can be a pet (being “petable”). However, how each is actually “petable” differs.  Similarly, a dog, a cat, and a baby all are “adorable”. But we will let each be “adorable” in his/her own ways.

24 24

25 25

26 Defining an Interface 26  An interface includes only abstract methods.  Syntax [public] interface InterfaceName { [public abstract] returnType1 methodName1 ([parameterList1]); [public abstract] returnType2 methodName2 ([parameterList2]); … }  The applicable access modifier is either public or none.

27 Implementing Interfaces 27  A class can implement one or more interfaces.  An interface can be implemented by any number of classes.  Syntax: class ClassName [extends SuperClassName] implements InterfaceName1, [interfaceName2, …]


Download ppt "Inheritance & Method Overriding BCIS 3680 Enterprise Programming."

Similar presentations


Ads by Google