1 programming ii - university of wisconsin–madisoncs300- · inheritance and class hierarchy uin...

20
Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM 1 [email protected] Spring 2019

Upload: others

Post on 07-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Programming II (CS300)

Chapter 05: Inheritance and Interfaces

MOUNA KACEM

1

[email protected] 2019

Page 2: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Inheritance and Interfaces

u Introduction

u Inheritance and Class Hierarchy

u Polymorphism

u Abstract Classes

u Interfaces

u Practice Examples

u Keep in Mind

2

Page 3: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Introduction

u Fundamental principles of OOP (1/2)u Encapsulation

uGrouping the data (fields, variables) and the operations that apply to them (methods) into one unit called a class, while hiding the implementation details

u Abstractionu Providing the essential (relevant) features of an object while hiding

the implementation details

3

Page 4: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Introduction

u 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

u InheritancePrinciple used to reuse code among related classes

Inheritance models the IS-A relationship

u Polymorphism refers to the

Ability of a reference variable to take different forms

Provision of the same interface for objects of different forms/shapes

4

Page 5: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Inheritance and Class Hierarchy

u In JAVA, the class Object, is the root of the class hierarchy u Every class has Object as the superclass.

u The extends clause is used to declare that a class is derived from another class.u Inheritance allows us to derive classes from a base class without

disturbing the implementation of the base class.

u In an IS-A relationship, the derived class (or sub-class) is a (variation of the) base class (or super-class)

u Multiple inheritance is not allowed in Java

5

Page 6: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Inheritance and Class Hierarchy

public class B extends A {

... <changes and additions>

}

6

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

Page 7: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Inheritance and Class Hierarchyu General description of a subclasspublic 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]}

7

Page 8: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Inheritance and Class Hierarchyu Super()

u used to call the super-class constructoru The super method can be called with parameters that match a super-class's

constructoru 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

u Access Modifiersu private

u Visible to the class, where they are defined, only u protected

u Visible to the package and all subclassesu public

u Visible to the world

8

Page 9: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Polymorphism

u Polymorphism literally means “many forms.”u Polymorphism includes overriding and overloading

u Overloadingu creating methods with same name but different parameters

u Overridingu re-defining the body of a method of superclass in a subclass to

change the behavior of that method

9

Page 10: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Inheritance and Class Hierarchy

u Overriding a methodu Methods in the superclass can be overridden in the subclasses

u The overridden subclass method shouldu have the same signature as it is defined in the super-class

u have the same return type

u not add exceptions to the throws list declared in the super-class method using throws clause

u 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

10

Page 11: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Inheritance and Class Hierarchy

u Partial Overridingu Partial overriding involves calling a base class method by using super

11

public class GoodWorker extends Worker {

@Overridepublic void doWork( ){

super.doWork( ); // Work like a WorkerdrinkCoffee( ); // Take a breaksuper.doWork( ); // Work like a Worker some more

}}

Page 12: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Inheritance and Class Hierarchyu Final Method

u A final method is invariant over the inheritance hierarchy and cannot be overridden

u It is a good practice to declare a method as final if it should not be overridden by a subclass

u Private methods declared in a super class are by default final and cannot be overridden

u Final Classu A final class cannot be extendedu Examples: String, System are final classes

12

Page 13: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Abstract Classes

u An abstract class cannot be constructed, even though it may declare and implement constructors

u A class with at least one abstract method must be an abstract class.

u An abstract method is a method that declares functionality that all derived class objects must eventually implement

u An abstract class may contain zero or many abstract methods.

13

Page 14: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Interfaces

u The interface is an abstract class that contains no implementation details

u An interface can be implemented by a class

u A class that implements an interface provides implementation details for all the abstract methods declared in that interface

u We do not use the keyword abstract to declare the abstract methods of an interface. It is implicit.

u A class that implements an interface behaves as if it had extended an abstract class specified by that interface

u An interface can extend another interface

14

Page 15: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Interfaces

u An interface should NOT be declared as private. Otherwise, it won’t be used.u An interface is a collection of constants and method declarations

u 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}

15

Page 16: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Interfaces

u Difference between an interface and an abstract classu The interface is not allowed to provide any implementation

details either in the form of data fields or implemented methods

u An abstract class can provide implementation details in the form of data fields or implemented methods even though it cannot be instantiated.

u A class can implement different interfaces. But can extend only one abstract class

16

Page 17: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Practice Example #1

u Relationship between derived, base classes, and implemented interfaces

u @see Pet class (Pet.java)u @see Animal interface (Animal.java)u @see Cat class that extends Pet class and implements

Animal interface (Cat.java)

17

Page 18: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Practice Example #2

u Practice Example #2 Object Cloningu Shallow Copy versus Deep Copy

u Cloning is a process of creating an exact copy of an existing object in the memory.

@see ShallowCopyInJava and DeepCopyInJava classes

u Optional Material Noteu In java, clone() method of java.lang.Object class is used for cloning an object

u Only objects which implement Cloneable interface are eligible for cloning process.

18

Page 19: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Keep in Mind

u Classes should hide their data and implementation of their methods

u A derived class inherits all data members from the base class and may add more data members.

u In java, a class can extend only one super-class

u In java, a class can implement many different interfaces

u Inheritanceu IS-A relationship: the derived class is a (variation of the) base class.

u Compositionu HAS-A relationship: a class A has an instance of the another class B.

19

Page 20: 1 Programming II - University of Wisconsin–Madisoncs300- · Inheritance and Class Hierarchy uIn JAVA, the class Object, is the root of the class hierarchy uEvery class has Object

Keep in Mindu The derived class inherits all methods from the base class. It may accept or

redefine them. It also can define new methods.

u A protected class variable field is visible to the derived class and also classes in the same package.

u Declaring data members as protected or public violates the spirit of encapsulation and information hiding

u It would better to declare data members as private and write accessor and mutator methods.

u An abstract class cannot be constructed. It serves to specify the functionality of derived classes

u Final methods may not be overridden.

u Final classes may not be extended

20