Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class inheritance details

 
mohan gavande
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new in JAVA. I have some difficulty regarding class inheritance
there r 2-3 types of java class extentions
1) class A interits class B {
}

2) class A extends class B {
}

3) class A impliments class b{
}

And might be many more
So plz tell me details of these differances.
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of the three classes you've mentioned only two are legal (also watch the spelling on implements)

class A extends class B {
}

class A implements class b{
}

In the cases above
extends denotes that you are inheriting from a class.
implements denotes that you are using an interface. Check out the java tutorial for more information on interfaces.

Also just to mix you up slightly more you can also make an interface extend another interface like this

interface A extends interface B {
}

Post back if you have any more questions!
 
David Milligan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say about an interface extending another, does the interface refer to a class.
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Milligan:
When you say about an interface extending another, does the interface refer to a class.


No, an interface is an interface, not a class. A class can extend a class, and an interface can extend an interface, but a class does not extend an interface nor does an interface extend a class. A class implements an interface.

The concept of interfaces can be tricky to those new to Java. I suggest taking a look at Sun's Java Tutorial especially this section on interfaces.

Hope that helps,
E
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way apologies for my posting which read

class A implements class b{
}

it should have read

class A implements interface b{
}

Also you should note that you do not include the class/interface word when you use them after extends/implements.

As already mentioned classes and interfaces can refer to very different things and it would be advisable to familiarise yourself with the concepts on the Java Tutorial.
Have fun!
Barry
 
mohan gavande
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I am new in JAVA & whatever u discuused is very good & details but as per it is my primary stage so I will read that toutorial & if Any diffuculty will occuer i will contact u again.
Thanks for replying me message.


------------
The main thing in Programming is that it can be do by verious ways
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic