70 likes | 178 Vues
Learn about defining object behavior with interfaces, abstract classes, their differences, and implementation in Java programming.
E N D
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 • }
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); } }
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); } }
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
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
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.
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