Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Query in abstract classes and interfaces.  RSS feed

 
Roopa Prabhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take a look at this code.

interface I1 {
public void add();
public void subtract();
}

abstract class AbstractFourOne {
abstract public void add();

public void subtract() {
System.out.println("Subtract from abstract class...");
}

static public void printIt() {
System.out.println("Printed");
}
}

public class Four extends AbstractFourOne implements I1{

public void add() {
System.out.println("Add");
}
/*
public void subtract() {
System.out.println("Subtract");
}
*/
public static void main(String[] args) {
new Four().add();
new Four().subtract();
AbstractFourOne.printIt();
}
}

This code neither gives a compile time error nor a run time exception. Instead it shows the output:
Add
Subtract from abstract class...
Printed

Why doesn't it as for implementation code for the subtract method of the I1 interface although there is no relationship between I1 and AbstractFourOne.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!