aspose file tools*
The moose likes Beginning Java and the fly likes Query in abstract classes and interfaces. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query in abstract classes and interfaces." Watch "Query in abstract classes and interfaces." New topic
Author

Query in abstract classes and interfaces.

Roopa Prabhu
Greenhorn

Joined: Feb 05, 2007
Posts: 8
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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Roopa Prabhu
Greenhorn

Joined: Feb 05, 2007
Posts: 8
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

Joined: Mar 06, 2001
Posts: 13459

"Roopa" please change your display name back or your account will get deleted.

thanks,
Dave.
Roopa Prabhu
Greenhorn

Joined: Feb 05, 2007
Posts: 8
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

Joined: May 03, 2004
Posts: 334
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

You're being notified now.

We require your display name to be a real-sounding first and last name. Please change it here. Thanks.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query in abstract classes and interfaces.