Docsity

Abstract Classes and Interfaces - Web Design and Development - Lecture Handouts, Lecture notes of Web Design and Development

Toggle sidebar
bg1
Handout 9–2
Web Design & Development CS-506
- 108 -
Abstract Classes and Interfaces
Problem and Requirements
Before moving on to abstract classes, first examine the following class hierarchy shown
below:
• 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 provides us two solutions to handle such problem
– Abstract Classes
– Interfaces
Abstract Classes
Abstract classes are used to define only part of an implementation. Because, information
is not complete therefore an abstract class cannot be instantiate. However, like regular
classes, they can also contain instance variables and methods that are full implemented.
The class that inherits from abstract class is responsible to provide details.
Any class with an abstract method (a method has no implementation similar to pure
virtual function in C++) must be declared abstract, yet you can declare a class abstract
that has no abstract method.
If subclass overrides all abstract methods of the superclass, than it becomes a concrete (a
class whose object can be instantiate) class otherwise we have to declare it as abstract or
we can not compile it
Circle Square
pf3
pf4
pf5

Partial preview of the text

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

Web Design & Development CS-

Abstract Classes and Interfaces

Problem and Requirements

Before moving on to abstract classes, first examine the following class hierarchy shown

below:

  • 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 provides us two solutions to handle such problem
  • Abstract Classes
  • Interfaces

Abstract Classes

Abstract classes are used to define only part of an implementation. Because, information

is not complete therefore an abstract class cannot be instantiate. However, like regular

classes, they can also contain instance variables and methods that are full implemented.

The class that inherits from abstract class is responsible to provide details.

Any class with an abstract method (a method has no implementation similar to pure

virtual function in C++) must be declared abstract, yet you can declare a class abstract

that has no abstract method.

If subclass overrides all abstract methods of the superclass, than it becomes a concrete (a

class whose object can be instantiate) class otherwise we have to declare it as abstract or

we can not compile it

Circle Square

Web Design & Development CS-

The most important aspect of abstract class is that reference of an abstract class can point

to the object of concrete classes.

Code Example of Abstract Classes

The Shape class contains an abstract method calculateArea() with no definition.

public abstract class Shape{ public abstract void calculateArea(); }

Class Circle extends from abstract Shape class, therefore to become concrete class it

must provides the definition of calculateArea() method.

public class Circle extends Shape { private int x, y; private int radius; public Circle() { x = 5; y = 5; radius = 10; }

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

The Test class contains main method. Inside main, a reference s of abstract Shape class is

created. This reference can point to Circle (subclass of abstract class Shape) class object

as it is a concrete class. With the help of reference, method calculateArea() can be

invoked of Circle class. This is all shown in the form of code below

public class Test { public static void main(String args[]){ //can only create references of A.C. Shape s = null;

//Shape s1 = new Shape(); //cannot instantiate

//abstract class reference can point to concrete subclass s = new Circle();

Web Design & Development CS-

Interfaces

As we seen one possible java’s solution to problem discussed in start of the tutorial. The

second possible java’s solution is Interfaces.

Interfaces are special java type which contains only a set of method prototypes, but doest

not provide the implementation for these prototypes. All the methods inside an interface

are abstract by default thus an interface is tantamount to a pure abstract class – a class

with zero implementation. Interface can also contains static final constants

Defining an Interface

Keyword interface is used instead of class as shown below:

public interface Speaker{ public void speak(); }

Implementing (using) Interface

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.

Relationship between a class and interface is equivalent to “responds to” while “is a”

relationship exists in inheritance.

Code Example of Defining & Implementing an Interface

The interface Printable contains print() method.

public interface Printable{ public void print(); }

Class Student is implementing the interface Printable. Note the use of keyword

implements after the class name. Student class has to provide the definition of print

method or we are unable to compile.

The code snippet of student class is given below:

Web Design & Development CS-

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

public String toString () { return "name:"+name +" address:"+address; }

//providing definition of interface’s print method public void print() { System.out.println("Name:" +name+" address"+address); } }//end of class

Interface Characteristics

Similar to abstract class, interfaces imposes a design structure on any class that uses the

interface. Contrary to inheritance, a class can implement more than one interfaces. To do

this separate the interface names with comma. This is java’s way of multiple inheritance.

class Circle implements Drawable , Printable { ………. }

Objects of interfaces also cannot be instantiated.

Speaker s = new Speaker(); // not compile

However, a reference of interface can be created to point any of its implementation class.

This is interface based polymorphism.

Code Example: Interface based polymorphism

Interface Speaker is implemented by three classes Politician, Coach and Lecturer. Code

snippets of all these three classes are show below:

public class Politician implements Speaker{ public void speak(){ System.out.println(“Politics Talks”); } }

public class Coach implements Speaker{ public void speak(){ System.out.println(“Sports Talks”); } }

  • Web Design & Development CS-