1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018
Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes Interfaces Practice Examples Keep in Mind
Introduction 3 Fundamental principles of OOP (1/2) Encapsulation Grouping the data (fields, variables) and the operations that apply to them (methods) into one unit called a class, while hiding the implementation details Abstraction Providing the essential (relevant) features of an object while hiding the implementation details
Introduction 4 Fundamental Principles of OOP (2/2) Principles allowing classes to express the similarities among objects that share some, but not all, of their structure and behavior Inheritance Principle used to reuse code among related classes Inheritance models the IS-A relationship Polymorphism refers to the Ability of a reference variable to take different forms Provision of the same interface for objects of different forms/shapes
Inheritance and Class Hierarchy 5 In JAVA, the class Object, is the root of the class hierarchy Every class has Object as the superclass. The extends clause is used to declare that a class is derived from another class. Inheritance allows us to derive classes from a base class without disturbing the implementation of the base class. In an IS-A relationship, the derived class (or sub-class) is a (variation of the) base class (or super-class) Multiple inheritance is not allowed in Java
Inheritance and Class Hierarchy 6 public class B extends A { }... <changes and additions> Several classes can be declared as subclasses of the same superclass. Inheritance can also extend over several generations of classes. For instance class E is considered as a sub-class of class A.
Inheritance and Class Hierarchy 7 General description of a subclass public class SubClass extends SuperClass{ // Any members that are not listed are inherited unchanged // except for constructor. // public section (public members) // * Constructor(s) if default is not acceptable // * SuperClass's methods whose definitions will change in SubClass // These methods will be overridden. The new definition will be // applied to objects of the subclass class. // * Additional public methods // New Methods will be defined here } // private section (private members) // * Additional data fields (generally private) // * Additional private methods [optional]
Inheritance and Class Hierarchy 8 Super() used to call the super-class constructor The super method can be called with parameters that match a super-class's constructor If the super-class constructor is not explicitly called, the compiler will add super() as the first instruction in the constructor of the sub-class Access Modifiers private Visible to the class, where they are defined, only protected Visible to the package and all subclasses public Visible to the world
Polymorphism 9 Polymorphism literally means many forms. Polymorphism includes overriding and overloading Overloading creating methods with same name but different parameters Overriding re-defining the body of a method of superclass in a subclass to change the behavior of that method
Inheritance and Class Hierarchy 10 Overriding a method Methods in the superclass can be overridden in the subclasses The overridden subclass method should have the same signature as it is defined in the super-class have the same return type not add exceptions to the throws list declared in the super-class method using throws clause should not reduce visibility (for instance if the method in the super-class is declared as public, it should not be overridden as a protected or private helper class
Inheritance and Class Hierarchy 11 Partial Overriding Partial overriding involves calling a base class method by using super public class GoodWorker extends Worker { } @Override public void dowork( ){ super.dowork( ); // Work like a Worker drinkcoffee( ); // Take a break super.dowork( ); // Work like a Worker some more }
Inheritance and Class Hierarchy 12 Final Method A final method is invariant over the inheritance hierarchy and cannot be overridden It is a good practice to declare a method as final if it should not be overridden by a subclass Private methods declared in a super class are by default final and cannot be overridden Final Class A final class cannot be extended Examples: String, System are final classes
Abstract Classes 13 An abstract class cannot be constructed, even though it may declare and implement constructors A class with at least one abstract method must be an abstract class. An abstract method is a method that declares functionality that all derived class objects must eventually implement An abstract class may contain zero or many abstract methods.
Interfaces 14 The interface is an abstract class that contains no implementation details An interface can be implemented by a class A class that implements an interface provides implementation details for all the abstract methods declared in that interface We do not use the keyword abstract to declare the abstract methods of an interface. It is implicit. A class that implements an interface behaves as if it had extended an abstract class specified by that interface An interface can extend another interface
Interfaces 15 An interface should NOT be declared as private. Otherwise, it won t be used. An interface is a collection of constants and method declarations If a variable is defined in an interface, it should be initialized and will be final (constant) public interface Taxable { } double taxrate = 0.06; // taxrate will automatically be considered final ( a // constant) since it is declared in an interface //double taxdoublerate; // This declaration is incorrect since an interface // cannot contain data fields. It must contain either constants or signatures // of public methods public double calculatetax(); // methods that returns the current tax value
Interfaces 16 Difference between an interface and an abstract class The interface is not allowed to provide any implementation details either in the form of data fields or implemented methods An abstract class can provide implementation details in the form of data fields or implemented methods even though it cannot be instantiated. A class can implement different interfaces. But can extend only one abstract class
Practice Example #1 17 Relationship between derived, base classes, and implemented interfaces @see Pet class (Pet.java) @see Animal interface (Animal.java) @see Cat class that extends Pet class and implements Animal interface (Cat.java)
Practice Example #2 18 Practice Example #2 Object Cloning Shallow Copy versus Deep Copy Cloning is a process of creating an exact copy of an existing object in the memory. In java, clone() method of java.lang.object class is used for cloning an object Only objects which implement Cloneable interface are eligible for cloning process. @see ShallowCopyInJava and DeepCopyInJava classes
Keep in Mind 19 Classes should hide their data and implementation of their methods A derived class inherits all data members from the base class and may add more data members. In java, a class can extend only one super-class In java, a class can implement many different interfaces Inheritance IS-A relationship: the derived class is a (variation of the) base class. Composition HAS-A relationship: a class A has an instance of the another class B.
Keep in Mind 20 The derived class inherits all methods from the base class. It may accept or redefine them. It also can define new methods. A protected class variable field is visible to the derived class and also classes in the same package. Declaring data members as protected or public violates the spirit of encapsulation and information hiding It would better to declare data members as private and write accessor and mutator methods. An abstract class cannot be constructed. It serves to specify the functionality of derived classes Final methods may not be overridden. Final classes may not be extended