8 Replies - 781 Views - Last Post: 29 June 2011 - 07:17 PM Rate Topic: -----

#1 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

comparing interfaces and sub classes

Posted 28 June 2011 - 10:25 PM

hi all,

I am new to java ...
please somebody explain what is the difference between subclass extending and interface implementing.

If I explain my problem further, I feel why I can not extends a interface instead of implement because both are classes and join with inheritance.

I think u all can understand my problem.

thx in advance,
menuka
Is This A Good Question/Topic? 0
  • +

Replies To: comparing interfaces and sub classes

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: comparing interfaces and sub classes

Posted 28 June 2011 - 10:53 PM

Quote

please somebody explain what is the difference between subclass extending and interface implementing

People have talk about Interface here:
http://www.dreaminco...stract-classes/

Quote

I feel why I can not extends a interface instead of implement because both are classes and join with inheritance.

You need to know that interface is a fully abstract class(no implemented method), this means you need to implement it so that you can give body to its methods. Also abstract implementation explains 'CAN DO' that is I implement something means I can do it. While extending means 'IS A', so if I extend class means I am of that type :)
Was This Post Helpful? 0
  • +
  • -

#3 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: comparing interfaces and sub classes

Posted 28 June 2011 - 11:04 PM

hi,

OK.Let's assume IF we have to implement abstract methods 'methodA' in interface in our main method, then why don't just write the method 'methodeA' ? because we have to do again A to Z of the method in our program main methode ?

i think u can understand my english..:-)


thx in advance,
menuka
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: comparing interfaces and sub classes

Posted 28 June 2011 - 11:15 PM

I dont get exactly what you said but in short: When method is put in interface class make sure that everyone who implemented it must have that method in his class. For example, if I have an interface of Programmers, then I put methods in it like workHard(), writeCode(), debug().... This makes sure that anyone who says I can program(implement that class) mus have those methods with him with the same signature. This helps in polymorphism, we make sure every class that is going to implement it must have those methods...
Was This Post Helpful? 2
  • +
  • -

#5 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: comparing interfaces and sub classes

Posted 28 June 2011 - 11:27 PM

thx dear,

that's what i wanted to know.
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: comparing interfaces and sub classes

Posted 28 June 2011 - 11:38 PM

you are Welcomed ;)
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: comparing interfaces and sub classes

Posted 29 June 2011 - 05:34 PM

View Postsmohd, on 29 June 2011 - 02:15 AM, said:

This helps in polymorphism, we make sure every class that is going to implement it must have those methods...

It is a lot more than this, it allows you to pass as parameter to a method an object as the implemented Interface
interface Abc {
   void xyz();
}

class A implements Abc {

    void xyz() {
       ... method code
    }
}

class B implements Abc {

    void xyz() {
       ... method code
    }
}

class Z {

     void aMethod(Abc anObject) {
          // here I can call xyz() without knowing if anObject is a A or a B
          // I don't even have to know that class A and class B exists
          anObject.xyz();
     }
}


That gives you a level of abstraction
Was This Post Helpful? 3
  • +
  • -

#8 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: comparing interfaces and sub classes

Posted 29 June 2011 - 07:10 PM

hi,

void aMethod(Abc anObject) {
 // here I can call xyz() without knowing if anObject is a A or a B
 // I don't even have to know that class A and class B exists
	          anObject.xyz();
	     }
	}



can anybody explain what result would print 'anObject.xyz();' and the reason ?
that is class A xyz() method or class B xyz method?

This post has been edited by menukadevinda: 29 June 2011 - 07:11 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: comparing interfaces and sub classes

Posted 29 June 2011 - 07:17 PM

View Postmenukadevinda, on 29 June 2011 - 10:10 PM, said:

hi,

void aMethod(Abc anObject) {
 // here I can call xyz() without knowing if anObject is a A or a B
 // I don't even have to know that class A and class B exists
	          anObject.xyz();
	     }
	}



can anybody explain what result would print 'anObject.xyz();' and the reason ?
that is class A xyz() method or class B xyz method?

xyz() will do what ever you put into it. Can be different in class A and in class B
the whole idea is to show that the method xyz() will be called.
If the Abc object passed as parameter to aMethod() is a A object it is the xyz() method of class A that will be called. If the Abc object passed as parameter to aMethod() it is the xyz() method of the class B that will be called.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1