Hi everyone,
consider below code:
interface i{
void test();
}
class A implements i{
void test(){//code goes here}
}
class B implements i{
void test(){//code goes here}
}
here,we can implement method straightaway in class itself, without using interface like,
class A{
void test(){//code goes here}
}
why we want to define method in interface and implementing in class?
what is the use of interface to define method there?
Kindly Correct if i'm wrong.
i searched for reason all over the internet, but couldnt find out exact answer.
Thanks in advance.
surya55
0
Newbie Poster
Recommended Answers
Jump to PostHi Surya55!
interface is used for not only reusability and also shold implement the mandatory fields.
for example if you develop a software for a Hospital.
In operation theater you should check all the safety wears (should weare coat, gloves, sleepers, mask, headcover).if you forgot any one means you …
Jump to PostThink about this example.
TheComparable
interface defines a methodcompareTo
that compares two objects so they can be sorted etc. Many classes implement this interface - eg Integer, String etc, and each implementation is different because the way you sort Integers is different from how you sort Strings (etc).
…
All 5 Replies
raj.mscking
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
stultuske
1,116
Posting Maven
Featured Poster
softDeveloper
0
Junior Poster in Training
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.