COM 244 Object Oriented Programming Some parts of
COM 244 Object Oriented Programming Some parts of those slides are adapted on course materials by Stuart Reges http: //www. cs. washington. edu/331/
Interfaces • Consider the task of implementing classes to concrete 2 D shapes including, Circle, Square, and Triangle. • Certain processes are common to all shapes: • perimeter: • area: • For each shape, perimeter and area operations are computed differently 2
Interfaces • Circle (as defined by radius r ): area = r 2 perimeter = 2 r • Rectangle (as defined by edge area =axa perimeter = 4 xa a ): • Triangle (lengths a, b (base) , and c, height h) area = ½ (b x h) perimeter = a + b + c 3
Interfaces • Suppose there exists 3 classes Circle, Square, Triangle. • Each has common methods such as perimeter and area. • We'd like the code to be able to treat different kinds of shapes in the same way: • Write a method that prints any shape's area and perimeter. • Write a method that can return a rectangle, a circle, a triangle, or any other kind of shape. 4
Interfaces public interface name { public type name(type name, . . . , type name); . . . public type name(type name, . . . , type name); } Example: public interface Animal{ public int feeding(string type); public int get. Speed(); } 5
Interfaces public interface Shape { public double area(); public double perimeter(); } interfaces do not contain any constructors. All methods are abstract. An interface cannot contain instance fields. The only fields that can appear in an interface can be both static and final. 6
Interfaces (Example) Example: /* File name • : Animal. java * interface Animal { public void feed(); public void travel(); } /* File name : Mammalls */ public class Mammal. Is implements Animal { public void feed() { System. out. println("Mammal eats meat"); } public void travel() { System. out. println("Mammal travels"); } public int Legnumber() { return 0; } public static void main(String args[]) { Mammalls m = new Mammal. Is(); m. feed(); m. travel(); } } 7
Interfaces (A Case Study) • Write an interface called My. Own. Interface, which has the following method • Address. Interface get. Address(); • The Address. Interface is a Java interface that has the following methods. • int get. Street. Number(); • void set. Street. Number(int street. Number); • String get. Street. Name(); • void set. Street. Name(String street. Name); • String get. Country(); • void set. Country(String country); • Write Address. Impl class that implements Address. Interface • Make the Person class to implement My. Own. Interface. • Initialize a Person object with proper data and display it. 8
Interfaces (A Case Study) 9
Interfaces (A Case Study) 10
Interfaces (A Case Study) 11
Interfaces (A Case Study) 12
Interfaces (A Case Study) 13
Interfaces (Another Case Study) 14
Abstract Class in Java • A Java abstract class is a class which cannot be instantiated. This basically means that you cannot create new instances of an abstract class. In java you declare that a class is abstract by adding the abstract keyword to the class declaration. • //Example public abstract class Divide. Method{ public abstract void divide. Num(); //abstract method } public class Calculate extends Divide. Method { public void divide. Num() { System. out. println(" New Divide Method "); } } 15
Derived fun() calle Abstract Class in Java Output: Derived fun method is called … 16
- Slides: 16