Docsity

Abstract Classes, Interfaces - Web Design and Development - Lecture Slides, Slides of Web Design and Development

Partial preview of the text

Download Abstract Classes, Interfaces - Web Design and Development - Lecture Slides and more Slides Web Design and Development in PDF only on Docsity!

Web Design & Development

Lecture 9

Abstract Classes a

and

Interfaces

Class Shape Hierarchy

  • Consider the following class hierarchy

Shape

Circle Square

Problem AND Requirements

  • Suppose that in order to exploit polymorphism, we
specify that 2-D objects must be able to compute
their area.
  • All 2-D classes must respond to area() message.
  • How do we ensure that?
  • Define area method in class Shape
  • Force the subclasses of Shape to respond area()
message
  • Java’s Solutions
    • Abstract Classes
    • Interfaces Docsity.com

Abstract Classes

  • If subclass overrides all abstract methods of the
superclass, than it becomes a concrete class
otherwise we have to declare it as abstract or we
can not compile it
  • Any subclass can override a concrete method inherited
from the superclass and declare them abstract
  • An abstract class cannot be instantiated
  • However references to an abstract class can be declared
    • Can point to the objects of concrete subclasses

Example of abstract class Shape.java

/* This is an example of abstract class. Note that
this class contains an abstract method with no
definition.
public abstract class Shape {
public abstract void calculateArea();

Circle.java

// providing definition of abstract method public void calculateArea () { double area = 3.14 * (radius * radius); System.out.println(“Area: ” + area); }

}//end of class

Test.java (Driver class)

public class Test { public static void main (String args[ ]){ //can only create references of abstract class Shape s = null; // Shape s1 = new Shape(); //cannot instantiate abstract class

//can point to the concrete subclass s = new Circle(); s.calculateArea(); } }

Interfaces

Interfaces

  • A special java type which
    • Defines a set of method prototypes, but does not
provide the implementation for the prototypes
  • Essentially all the methods inside an interface are
Abstract Methods or we can say that an interface
is like a pure abstract class (Zero Implementation)
  • Can also define static final constants

Implementing (Using) Interfaces

  • Classes Implement interfaces
    • Implementing an interface is like signing a contract.
    • A class that implements an interface will have to provide the definition of all the methods that are present inside an interface“
    • If the class does not provide definitions of all methods, the class would not compile. We have to declare it as an abstract class in order to get it compiled.
  • “Responds to” relationship
    • Relationship between a class and interface

Interface - Example

speak()

Politician Coach

<>

Speaker

speak() speak()

Lecturer

speak()

Example Code

public interface Printable { public void print(); }

public class Student implements Printable{ private String name; private String address;

public String toString () { return "name:"+name +" address:"+address; } // NOT providing implementation of print method }

Defining Interface

Implementing Interface

Compile