i read adapter classes everything is write but i am confuse about Listener Interfaces and Adapter classes concept priblem is that when java create adapter classes it implements corresponding interface and privide defination with empty bodies of all methods that are present in interface this is declaration

public class WindowAdapter implements WindowListener

this is built in declaration of WindowAdapter.Why not java create directly adapter classes without implementing interface same like this

public class WindowAdapter

after this no need to create interfaces and no need to import interfaces my english is very poor plz try to understand my question
Moaz Amin

Recommended Answers

All 4 Replies

when u import an interface u need implement all methods inside (override),
when u import an class adapter maybe u need only implement a set of methods that really u need.

this is not answer of my question actually my question is that why not java provide directly adapter classes with this approach that was only 1 way for event handling only extend adapter classes

is because java want encapsulate fields, and when u use interfaces the aplication have less couple. then if u use everthing classes your application have more couple. then when an interface have an adapter class this means that adapter class implements all methos in the interface, and you only need override the methods that you want (about the class adapter), use too polymorphism. Use is:
WindowListener wl = new WindowAdapter();
then u can use all methods.
is normall in all Java Interfaces.

public interface MyInterface{
    public int count();
    public void setAnValue(String anValue);
    public String getAnValue();

//... then u implement this interface Is an example

public class MyInterfaceImpl implements myInterface{
    String anValue;
    public int count(){
     return 1;
    public void setAnValue(String anValue){
        this.anValue = anValue;
    public String getAnValue(){
        return anValue;

// now u can see the powerfull of this approach

public class MyClassNew{
    private MyInterface myIf = new MyInterfaceImpl;
    //here this class can add new methods...
    public int getValueZero(){
        myIf.count() - 1;
    public String getAnString(){
        return myIf.getAnValue();
    //how u can see, i use the Interface methods, and instantiate to MyInterfaceImpl - here is polymorphism with less couple.


its not cleare to me?????

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.