1 / 7

Interfaces and Abstract Classes

Interfaces and Abstract Classes. The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class is similar to class specification Contains only a list of methods & their parameters Omitting the implementation of each method

Télécharger la présentation

Interfaces and Abstract Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interfaces and Abstract Classes • The ability to define the behavior of an object without specifying that behavior is to be implemented • Interface class is similar to class specification • Contains only a list of methods & their parameters • Omitting the implementation of each method • Contains NO data except constants • public interface Shape { • public static final double PI=3.14159; • public double area(); • public double circumference(); • public void whoAreYou(); //prints shape name • }

  2. Class Circle implements Shape When a class implements an interface, it must define the methods as specified by that interface public class Circle implements Shape { private double radius; public Circle(double r){ radius = r ; } public double area(){ return PI * radius * radius ; } public double circumference(){ return 2 * PI * radius ; } public void whoAreYou() { System.out.println("I am a circle of radius= “ +radius); } }

  3. Class Rectangle implements Shape public class Rectangle implements Shape { private double length, width; public Rectangle(double l, double w){ length = l; width = w; } public double area(){ return length * width; } public double circumference(){ return 2 * (length + width); } public void whoAreYou() { System.out.println( "I am a rectangle of length="+length+ " and width="+width); } }

  4. Class TestShapes public class TestShapes { public static void main(String args[]){ Shape shape1 = new Circle(3.5); Shape shape2 = new Rectangle(3, 4); displayInfo(shape1); displayInfo(shape2); } // Continue … on next slide

  5. Class TestShapes … Continued public static void displayInfo(Shape shape){ System.out.println("Checking the shape type:"); if(shape instanceof Circle ) System.out.println( "shape is a reference to a Circle object"); else if (shape instanceof Rectangle) System.out.println( "shape is a reference to a Rectangle object"); System.out.println("Printing shape information"); shape.whoAreYou(); System.out.println("My Area is "+ shape.area() ); System.out.println("My Circumference is " + shape.circumference( ) ); System.out.println(); } } // end class TestShapes

  6. Notes on Interface Class • interfaces can’t be instantiated. • An interface can contain constant declarations in addition to method declarations. • All methods declared in an interface are implicitly public, so the public modifier can be omitted. • All constant values defined in an interface are implicitly public, static, and final. Therefore, these modifiers can be omitted.

  7. Introduction to Abstract Classes • Methods in Shape considered abstract (i.e., not defined) • We could have written • public abstract double area(); in the interface • Abstract class • Similar to an interface • may contain both abstract methods and defined methods • In an interface, all methods must be abstract • To use abstract class, we extend the class using inheritance • Neither interfaces nor abstract classes can be instantiated

More Related