• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query in abstract classes and interfaces.

 
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is exactly your question - what is the difference between what it does and what you expected it to do?

Did you expect a compile error because it isn't clear if the add() and subtract() methods in class Four are implementations of the interface methods or of the methods in the abstract class?

The answer to that is: There doesn't need to be a compiler error because there is no ambiguity - the methods add() and subtract() in class Four are implementations of the interface methods and the abstract class methods at the same time.

There is indeed no relationship between interface I1 and abstract class AbstractFourOne, but there doesn't need to be a relationship.
 
Roopa Prabhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper...
My doubt is: why is it not asking for an implementation of the subtract method for the interface I1?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Roopa" please change your display name back or your account will get deleted.

thanks,
Dave.
 
Roopa Prabhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why will it get deleted? i didn't get any notification after giving my display name as Roopa...
Let me know if there is any technical problem?
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Its because the subtract method of the Interface I1 has already implemented by the abstract class AbstractFourOne. The class Four extends AbstractFourOne which implements I1.

For your information, Javaranch has some naming policy. Please stick to this policy.

bye for now
sat
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're being notified now.

We require your display name to be a real-sounding first and last name. Please change it here. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic