*An abstract class cannot be used to create objects * In UML the italicized name of a class means that this class is an abstact class *In UML the # sign indicates protected modifier *In UML the abstract methods are italicized (Çáåä ÇÓã ãÇÆá) *Abstract classes are like regular classes, but you cannot create instances of abstract classes using the new operator. An abstract method is defined without implementation. Its implementation is provided by the subclasses. A class that contains abstract methods must be defined as abstract. *The constructor in the abstract class is defined as protected.---------------------?? *Why Abstract Methods? The program creates two geometric objects, a circle and a rectangle, invokes the equalArea method to check whether they have equal areas, and invokes the displayGeometricObject method to display them. 1 public class TestGeometricObject { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Create two geometric objects 5 GeometricObject geoObject1 = new Circle(5); 6 GeometricObject geoObject2 = new Rectangle(5, 3); 7 8 System.out.println("The two objects have the same area? " + 9 equalArea(geoObject1, geoObject2)); 10 11 // Display circle 12 displayGeometricObject(geoObject1); 13 14 // Display rectangle 15 displayGeometricObject(geoObject2); 16 } 17 18 /** A method for comparing the areas of two geometric objects */ 19 public static boolean equalArea(GeometricObject object1, 20 GeometricObject object2) { 21 return object1.getArea() == object2.getArea(); 22 } 23 24 /** A method for displaying a geometric object */ 25 public static void displayGeometricObject(GeometricObject object) { 26 System.out.println(); 27 System.out.println("The area is " + object.getArea()); 28 System.out.println("The perimeter is " + object.getPerimeter()); 29 } 30 } ------ Notes: *An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined as abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented. Also note that abstract methods are nonstatic. *An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class. *A class that contains abstract methods must be abstract. However, it is possible to define an abstract class that doesn’t contain any abstract methods. In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining subclasses. *A subclass can override a method from its superclass to define it as abstract. This is very unusual, but it is useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined as abstract. *A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract. *You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of the GeometricObject type, is correct. GeometricObject[] objects = new GeometricObject[10]; You can then create an instance of GeometricObject and assign its reference to the array like this: objects[0] = new Circle(); *Why this is not legal----------------------?? public class abstract A { abstract void unfinished(); } *An abstract method must be nonstatic. --------- Interfaces: *The interface name and the method names are italicized. The dashed lines and hollow triangles are used to point to the interface. *When a class implements an interface, it implements all the methods defined in the interface with the exact signature and return type. * all data fields are public static final and all methods are public abstract in an interface. *JDK 8 allows default methods in the interface. *JDK 8 allows static methods in the interface. EX: interface A { default void print() { } } interface A { static int get() { return 0; } } both are correct. *When a class implements the interface, the method must be declared public. * // Interface for comparing objects, defined in java.lang package java.lang; public interface Comparable { public int compareTo(E o); } The compareTo method determines the order of this object with the specified object o and returns a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than o. *all are true: n instanceof Integer n instanceof Object n instanceof Comparable and if it's true for string, date, double, ..... etc -------- Exceptions: InputMismatchException: ex: int number = input.nextInt(); and you entered a character rather than a number ArithmeticException: ex: divide by zero -------------------------------