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

Abstract Classes, Interfaces, Class Shape Hierarchy, Problem AND Requirements, Example of abstract class, Circle.java, Driver class, Compile, Execute, Interfaces Definition are terms you can learn in this lecture along with others.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

parasad
parasad 🇮🇳

4.5

(56)

131 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Web Design & Development
Lecture 9
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

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