230 likes | 380 Views
Inheritance & Method Overriding. BCIS 3680 Enterprise Programming. Overview. Inheritance How to create a subclass Methods in super- and subclasses Constructors Method overriding Abstract methods Interfaces. Inheritance.
E N D
Inheritance & Method Overriding BCIS 3680 Enterprise Programming
Overview • Inheritance • How to create a subclass • Methods in super- and subclasses • Constructors • Method overriding • Abstract methods • Interfaces
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 subclassesinherits those common variables and methods defined in the superclass. Each subclass has its own unique traits in addition to the shared ones.
A Sample Vehicle Hierarchy • This hierarchy is depicted using a Unified Modeling Language (UML) diagram. • In UML diagrams, arrows point from the subclass to the superclass.
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 onesuperclass. • 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.
Inheritance • The extends keyword <access modifier> class <subclassName> extends <superclassName> public class Graduate extends Student • A subclass inherits the variables and methods of a superclass.
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), inside which, • It may call the constructor in the superclass to initialize inherited variables, i.e., those defined in the superclass. • In addition, it may initialize those variables defined in the subclass. • It may even initialized all variables (inherited or non-inherited) in its own way.
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);
Adding New Members • 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.
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.
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.
Abstract Methods • 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.
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: accessModifierabstract returnTypemethodName( [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.
Abstract Classes • 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.
Interface • 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.
Interface • 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.
Defining an Interface • 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.
Implementing Interfaces • A class can implement one or more interfaces. • An interface can be implemented by any number of classes. • Syntax: <access modifier> class ClassName [extends SuperClassName] implements InterfaceName1, [interfaceName2, …]