Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance & Polymorphism (Part 3)

Similar presentations


Presentation on theme: "Inheritance & Polymorphism (Part 3)"— Presentation transcript:

1 Inheritance & Polymorphism (Part 3)
STIA1123 : Programming 2

2 Objectives To define abstract parent classes and methods
To define interface To differentiate between abstract class and interface. To write programs that are easily extensible and modifiable by applying polymorphism in program design.

3 Introduction Sometimes, we perhaps do not need to create any instances of the superclass. E.g.: If a Student can be only a Graduate or an Undergraduate student, then there is no need to create an instance of Student. Sometimes, it is also desirable to force programmers to redefine the methods of the superclass. That happens when there is no good default for the superclass, and only the subclass programmer can know how to implement the method properly. Thus, declare a method that has no implementation (an abstract method).

4 Abstract Method An abstract method is a method that has only the heading with no body. The heading of an abstract contains the reserved word abstract and ending with semicolon(;). Example: public abstract void print(); public abstract object larger(object,object); abstract void insert(int insertItem);

5 Abstract Method UML Notation:
GeometricObject (abstract) - color : String - filled : boolean + getColor() : String + setColor(color: String): void + isFilled(filled: boolean): void + findArea(): double + findPerimeter():double UML Notation: The abstract class name and the abstract method names (italic) Rectangle - length : double - height : double + getWidth : double + setWidth(width: double): void + getHeight() : double + setHeight(height : double) : void + findArea(): double + findPerimeter():double Circle - radius : double + getRadius : double + setradius(radius: double): void + findArea(): double + findPerimeter():double

6 Abstract Method The methods of findArea() and findPerimeter() cannot be implemented in GeometricObject class because their implementations are dependent on the specific type of geometric object. These methods are referred to as abstract methods. An abstract method is a method signature without implementation. Its implementation is provided by the subclass. A class that contains abstract methods must be declared abstract. Based on the example, the findArea() and findperimeter() are defined as abstract methods in GeometricObject class. Both method are implemented (overridden) in Circle class and Rectangle class.

7 Abstract Method Abstract method declaration must be ended with semicolon (;) Abstract method cannot be private type because a private members cannot be accessed. Constructor and static method cannot be used as abstract method. Method redefinition (in subclass) must be similar in all aspect such as returning value, method name, number of parameter, and parameter type.

8 Abstract Class A class is abstract if the class contains an abstract method or does not provide an implementation of an inherited abstract method. An abstract class is a class that is declared with the reserved word abstract in its heading. An abstract class can contain instance variables, constructors, the finalizer, and non-abstract methods. An abstract class can contain abstract method(s). If a class contains an abstract method, then the class must be declared abstract. We cannot instantiate an object of an abstract class. We can only declare a reference variable of an abstract class type. We can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass.

9 Abstract Class & its subclasses
public abstract class Shape{ ………… public abstract double calculateArea(); } public class Triangle extends Shape{ public double calculateArea(){ ………. public class Rectangle extends Shape{ …………… ………..

10 Abstract Class: Sample 1
class Rectangle extends Shape { ……… public double calculateArea() { return width * length; } } //end Rectangle public class Test{ public static void main(String[] args) { Rectangle rec = new Rectangle (4.0,3.0); Triangle tri = new Triangle(2.0,4.0); ……….. System.out.println(rec. calculateArea()); System.out.println(tri. calculateArea()); } // end Test public abstract class Shape { public abstract double calculateArea(); } class Triangle extends Shape { ………. public double calculateArea() { return (height *base)/2.0; } //end Triangle

11 Abstract Class: Sample 1
12.0 4.0

12 Abstract class & subclasses
public abstract class Shape { public abstract void draw(); } public class Circle extends Shape { public void draw (){ System.out.println("I am drawing Circle"); public class Rectangle extends Shape { System.out.println("I am drawing Rectangle"); A variable of class X may not refer to an object from the superclass or sibling classes of X. Sibling classes are those that share the common ancestor class. For example, the following statements are invalid: Dog myDog = new Cat(); Cat myCat = new Pet();

13 Abstract class & subclasses
public class Star extends Shape { public void draw (){ System.out.println("I am drawing Star"); } public class Triangle extends Shape { System.out.println("I am drawing Triangle"); A variable of class X may not refer to an object from the superclass or sibling classes of X. Sibling classes are those that share the common ancestor class. For example, the following statements are invalid: Dog myDog = new Cat(); Cat myCat = new Pet();

14 Interface An interface type is similar to a class.
Use interface types to make code more reusable. A Java interface type declare a set of methods and their signatures. Unlike a class, an interface type provides no implementation. Features of the interface type: All methods in an interface type are abstract; that is they have a name, parameters, and return type, but they don’t have an implementation, All methods in an interface type are automatically public, An interface type does not have instance fields. Interfaces can reduce the coupling between classes. A variable of class X may not refer to an object from the superclass or sibling classes of X. Sibling classes are those that share the common ancestor class. For example, the following statements are invalid: Dog myDog = new Cat(); Cat myCat = new Pet();

15 Interface Defining an interface in Java:
public interface InterfaceName{ method signatures } OR abstract method signatures } E.g.: public interface Measureable { public abstract double getMeasure(); } OR double getMeasure(); A variable of class X may not refer to an object from the superclass or sibling classes of X. Sibling classes are those that share the common ancestor class. For example, the following statements are invalid: Dog myDog = new Cat(); Cat myCat = new Pet();

16 Interface Implementing an interface in Java:
public className implements InterfaceName, InterfaceName, … { data fields methods } E.g.: public class BankAccount implements Measureable { //Other BankAccount data fields and methods public double getMeasure(){ //Method implementation return balance; A variable of class X may not refer to an object from the superclass or sibling classes of X. Sibling classes are those that share the common ancestor class. For example, the following statements are invalid: Dog myDog = new Cat(); Cat myCat = new Pet();

17 Interface vs Inheritance
BankAccount Coin <<interface>> Measurable SavingAccount CurrentAccount Both BankAccount class and Coin class implement Measurable interface. A dotted arrow with triangular tip denotes the “is-a” relationship between a class and an interface. Both SavingAccount class and CurrentAccount class are subclasses for BankAccount. A solid arrow with triangular tip denotes the “is-a” relationship between a superclass and a subclass. A variable of class X may not refer to an object from the superclass or sibling classes of X. Sibling classes are those that share the common ancestor class. For example, the following statements are invalid: Dog myDog = new Cat(); Cat myCat = new Pet();

18 Conclusion Q & A Session


Download ppt "Inheritance & Polymorphism (Part 3)"

Similar presentations


Ads by Google