java - Abstract class and methods, Why? -


hope can help, learning java , comparable else in forum guess newbie programming well.

i have come across chapter on abstract class , methods don't understand used , why, , thought explanation experienced programmer.

below have example code have been working on, book, not sure why in class dims have have abstract double area() when each sub class uses area method anyway, or calling area method super class, why have have methods override?

    // using abstract methods , classes     package training2;      abstract class dims {     double dim1;     double dim2;      dims(double a, double b) {         dim1 = a;         dim2 = b;     }     // area abstract method     abstract double area(); } class rectangles extends dims {     rectangles(double a, double b) {         super(a, b);     }      // override area rectangles     double area() {         system.out.println("inside area rectangle.");         return dim1 * dim2;     } }  class triangles extends dims {     triangles(double a, double b) {         super(a, b);     }      // override area right triangles     double area() {         system.out.println("inside area triangle.");         return dim1 * dim2 /2;     } } public class abstractareas {     public static void main(string args[]) {         // dims d = new dims(10, 10); // illegal         rectangles r = new rectangles(9, 5);         triangles t = new triangles(10, 8);         dims dimref; // ok, no object created          dimref = r;         system.out.println("area " + dimref.area());          dimref = t;         system.out.println("area " + dimref.area());}  } 

apologies waffling on need guidance.

thanks in advance.

we "this class has complete (ish) api, not have complete implementation." among other things, means can this:

public void dosomething(dims d) {     system.out.println("the area " + d.area()); }  // ...using somewhere:  dosomething(new triangle(4.0, 6.0)); 

in dosomething, though reference have object dims, not triangle, can call area (and end calling triangle#area) because it's defined in dims api (sometimes called "contract"). it's dims defers implementation subclasses. why can't create instances of abstract classes. dosomething method doesn't have idea whether given triangle or rectangle, it's some kind of dims.

whereas if dims didn't define area, dosomething wouldn't compile. we'd have declare dosomething accepting triangle , 1 accepting rectangle , on. couldn't benefit of being able operate on dims things in common code.

there's lot of crossover between interfaces , abstract classes in java. fundamentally, can think of abstract class interface partial implementation (with caveat class can implement more 1 interface, can derive single abstract class). line's gotten blurrier interfaces can define "default" implementations of methods. (some argue interfaces can have default methods, they're "new" abstract classes , make true abstract classes obsolete language feature, there still arguments, syntactic, around using abstract classes sometimes.)


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -