0% found this document useful (0 votes)
20 views

Exampleinterface String: Public Interface Public Void Do Public Int

An interface defines methods but does not provide implementations, allowing classes to implement multiple interfaces and inherit their method signatures. The document demonstrates an ExampleInterface with two methods that the sub class implements, and a SuperClass that is extended by a SubClass which overrides one of the parent's methods. Interfaces allow classes to inherit method signatures without multiple inheritance of classes.

Uploaded by

Vallam Ramesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Exampleinterface String: Public Interface Public Void Do Public Int

An interface defines methods but does not provide implementations, allowing classes to implement multiple interfaces and inherit their method signatures. The document demonstrates an ExampleInterface with two methods that the sub class implements, and a SuperClass that is extended by a SubClass which overrides one of the parent's methods. Interfaces allow classes to inherit method signatures without multiple inheritance of classes.

Uploaded by

Vallam Ramesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

The difference between an interface and a regular class is that in an interface you can

not specify an specific implementation (only its "interface"). More specific, this means
you can only specify methods, but not implement them.
Also java doesn't support multiple inheritance for classes. This is solved by using
multiple interfaces.
public interface ExampleInterface{
public void do();
public String doThis(int number);
}
public class sub implements ExampleInterface{
public void do(){
//specify what must happen
}

public String doThis(int number){


//specfiy what must happen
}

now extending a class


public class SuperClass{
public int getNb(){
//specify what must happen
return 1;
}
public int getNb2(){
//specify what must happen
return 2;
}
}
public class SubClass extends SuperClass{
//you can override the implementation
@Override
public int getNb2(){
return 3;
}
}

in this case

Subclass s = new SubClass();


s.getNb(); //returns 1
s.getNb2(); //returns 3
SuperClass sup = new SuperClass();
sup.getNb(); //returns 1
sup.getNb2(); //returns 2

An interface can contain way more than method declarations: Constant fields, annotations, interfaces
and even classes

You might also like