access method from super interface

I have the following classes.

interface Interface1{

interface1Method();

}

interface Interface2{

interface2Method();

}

class MyTableModelextends AbstractTableModel{

void myTableModelMethod();

}

class Firstextends MyTableModelimplements Interface1{

void interface1Method();

}

class Secondextends Firstimplements Interface2{

void interface2Method();

}

class MyListModelextends AbstractListModel{

void myListModelMethod();

}

class Firstextends MyListModelimplements Interface1{

void interface1Method();

}

class Secondextends Firstimplements Interface2{

void interface2Method();

}

class MainClass{

Interface2 impl;

public MainClass(Interface2 impl){

this.impl = impl;

}

void methodInMainClass(){

impl.interface1Method();

}

}

can I call the inteface1 or am i restricted to the interface2 method only. Would I have to do something like....

interface Interface1And2implements Interface1, Interface2{}

class MainClass{

Interface1And2 impl;

public MainClass(Interface1And2 imp){

this.impl = impl;

}

void methodInMainClass(){

impl.interface1Method();

}

}

Message was edited by:

sand_samurai

[3258 byte] By [sand_samuraia] at [2007-11-27 11:06:45]
# 1

A few problems,

1) you have dupicate class names, so you don't have any way to distinguish which class you refer to.

2) You don't ever call the methodInMainClass() method, so it won't be run.

Other than that, no, you can't call interface1 methods on an interface 2 reference. Since none of your classes implement both interfaces, it's not possible to call both interface's methods on any of those classes.

hunter9000a at 2007-7-29 13:17:32 > top of Java-index,Java Essentials,New To Java...
# 2

interface Interface1{

interface1Method();

interface Interface2{

interface2Method();

class MyTableModel extends AbstractTableModel {

void myTableModelMethod();

class FirstTable extends MyTableModel implements

Interface1 {

void interface1Method();

class SecondTable extends First implements Interface2 {

void interface2Method();

class MyListModel extends AbstractListModel {

void myListModelMethod();

class FirstList extends MyListModel implements Interface1

{

void interface1Method();

class SecondList extends First implements Interface2 {

void interface2Method();

class MainClass {

Interface2 impl;

public MainClass(Interface2 impl) {

this.impl = impl;

void methodInMainClass() {

impl.interface1Method();

}

}

can I call the inteface1 or am i restricted to the

interface2 method only. Would I have to do something

like....

interface Interface1And2 implements Interface1,

Interface2{}

class MainClass {

Interface1And2 impl;

public MainClass(Interface1And2 imp) {

this.impl = impl;

void methodInMainClass() {

impl.interface1Method();

}

}

Apologies - I typed it quick, i've changed the class names. does it make sense what i'm trying to achieve. is that possible. I didnt put the static main method in, but what i'm trying to find out is do i need to have a 3rd interface that implements both?

Message was edited by:

sand_samurai

sand_samuraia at 2007-7-29 13:17:32 > top of Java-index,Java Essentials,New To Java...
# 3

Ignoring the typos in the code, then yes, if you have a class that implements both interfaces 1 and 2, or if you have a sub interface that extends both of those, then you can call either interface's methods.

hunter9000a at 2007-7-29 13:17:32 > top of Java-index,Java Essentials,New To Java...
# 4

OP: Do you realize the best answer to many questions of the form "can I do this?" is "Try it and see"?

Hippolytea at 2007-7-29 13:17:32 > top of Java-index,Java Essentials,New To Java...