/* By now you should understand the difference between classes and interfaces and what interfaces are used for. An *abstract class* is another structure that is somewhat in between a class and an interface: 1. It can contain method declarations without bodies, such as public abstract int f(); This is similar to what's found in an interface. 2. It can also contain full methods with both declaration and implementation: public String g() { return "hello"; } 3. Although it's possible to write a construction in an abstract class, you cannot create an instance of an abstract class just as you cannot create an instance of an interface. An abstract class is incomplete. Only concrete (non-abstract) classes can have instances. The constructor of an abstract class is only called from the constructor of its subclasses, not from 'new'. 4. A class, abstract or not, can extend an abstract class. When a non-abstract (conrete) class extends an abstract class, it must provide implementations of all method declarations marked "abstract". Thus an abstract class has the characteristics of both a class and an interface. The following is considered a classic example of object oriented programming using an abstract class. */ abstract class shape implements Comparable { public int x, y; // position of shape /* abstract methods are like declarations in an interface:*/ public abstract double area(); public abstract double circumference(); // classes that extend shape must implement these methods. // an abstract class can also contain regular methods: public double distanceto(shape other) { double dx = x-other.x, dy = y-other.y; return Math.sqrt(dx*dx + dy*dy); } public int compareTo(shape other) // compare objects based on area. { double a = this.area(), b = other.area(); if (a==b) return 0; else if (a0) S[3] = new circle(5,5,5); else S[3] = new square(4,4,4); // What's the 'type' of S[3]? Speak up! System.out.println(S[0].distanceto(S[2])); System.out.println(S[1].compareTo(S[0])); double totalarea = 0; for(shape s:S) totalarea += s.area(); // which area() is being called? System.out.println( totalarea ); // System.out.println(S[0].radius) // compiler error? why? // The above line will not compile because to the compiler, // the type of S[0] is "shape", which does not have a radius. // The compiler is not aware of the dynamic/runtime type of // S[0], which is a circle. System.out.println("S[0] has radius" + ((circle)S[0]).radius); // OK System.out.println("S[1] has radius" + ((circle)S[1]).radius); // ??? // the above line will compile but will result in a runtime exception. }//main } /* ***************** Let's summarize what you know should know about interfaces, abstract classes, and inheritance. An interface defines the *type* of objects. That is, it specifies to the rest of the program what (public) functions your objects offer. A class "implements" an interface by providing code for all the functions specified in the interface. You cannot put variables in an interface, and you can't create an instance of an interface. A superclass contains common code shared by subclasses. A subclass "extends" a superclass. A subclass has access to all the variables and methods of the superclass that are not marked "private". Private variables are inherited but may not be accessed from the subclass. A class can extend only one superclass, but can implement multiple interfaces. An interface can extend another interface. Every class has an implicit interface, which consists of all the public methods of the class. That is, if a class is not declared to "implement" an interface, then its default interface consists of all of its public methods. Unless an "extends" specification is provided, every class extends the superclass "Object" by default. The default constructor of a class is always of the form: public classname() { super(); } The superclass constructor is called implicitly at the beginning of a constructor. You normally dont' need to call super() explicitly unless the superclass constructor requires arguments (see the square class) The default constructor is NOT automatically provided if you implement another constructor (taking different arguments). This is a java quirk. A construct similar to an interface is called an "abstract class". an abstract class can contain data structures and methods, as well as abstract method declarations like the kind found inside interfaces. These abstract methods must be implement by its non-abstract subclasses. A abstract class is a combination of a class and an interface. You also cannot create an instance of an abstract class. Quiz: What's wrong with the following program: abstract class A { protected int x; public A(int x0) {x = x0; } public abstract int f(int y); public int g() { return x + f(1); } public void h() {} }//A class B extends A { public B(int x0) { super(x0); } public int f(int y) { return y*x; } } public class goodquestion { public static void main() { A n = new B(1); System.out.println(n.f(3)); System.out.println(n.g()); A m = new A(2); m.h(); } } ANSWER: The only thing wrong is at the end of main: A m = new A(2); tries to create an instance of an abstract class. Of course m.h() also can't be called. */