The main intention of this blog is to assist the job seekers and the beginners to clarify their doubts and explore their skill sets
Get link
Facebook
X
Pinterest
Email
Other Apps
i don't want to define all the methods from an implemented interface,Perhaps i want to define one but all as per my requirement!!!How to achieve this scenario in JAVA?
Here we r not defining the method m2() in class Ex2 bcoz we made the class as abstract ,So we don't need to define all the methods inplace its mandatory to define all the methods from the implemented interface in the extended class. e.g.:-
interface A { public void m1(); public void m2(); }
public abstract class Ex2 implements A {
public static void main(String[] args) { // TODO Auto-generated method stub
} public void m1(){ System.out.println("Ex2 m1"); }
} class Test extends Ex2 { public void m1() { System.out.println("Test m1"); } public void m2() { System.out.println("Test m2"); }
Basically we have to define all the methods for an implemented interface only in the case of non-abstract classes but incase of abstract classes we may or may not define the methods
Below is an example for this scenario:
ReplyDeleteHere we r not defining the method m2() in class Ex2 bcoz we made the class as abstract ,So we don't need to define all the methods inplace its mandatory to define all the methods from the implemented interface in the extended class.
e.g.:-
interface A
{
public void m1();
public void m2();
}
public abstract class Ex2 implements A
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
}
public void m1(){
System.out.println("Ex2 m1");
}
}
class Test extends Ex2
{
public void m1()
{
System.out.println("Test m1");
}
public void m2()
{
System.out.println("Test m2");
}
}
Basically we have to define all the methods for an implemented interface only in the case of non-abstract classes but incase of abstract classes we may or may not define the methods
ReplyDelete