I am 5 years experienced in programming in C but Till I cant understand the languages like Java.
what is the use of using interfaces and abstract class since they do nothing.
Printable View
I am 5 years experienced in programming in C but Till I cant understand the languages like Java.
what is the use of using interfaces and abstract class since they do nothing.
Interfaces are a way to give a data type to an object. Java is strict on data typing compared to c.
They allow the compiler to verify that a class object has the method(s) required for that data type.
A common used interface is a listener. The compiler guarantees that any object that says it is a listener will have all the required methods defined by the interface.
Code :public class Main { public static void main(String[] args) { shape circleshape=new circle(); circleshape.Draw(); } } interface shape { public String baseclass="shape"; public void Draw(); } class circle implements shape { public void Draw() { System.out.println("Drawing Circle here"); } }
both programs do the same!
Code :public class HelloWorld { public static void main(String[] args) { circle circleshape=new circle(); circleshape.Draw(); } } class circle { public void Draw() { System.out.println("Drawing Circle here"); } }
What's the point?
Where is the method that takes an argument of type shape?
void SomeMethod(shape aShape) {
}
You could NOT pass an object of the circle class from your second code example to this method. It is not a shape.
Is interface shape can used as a data-type?
and where as a circle can not be use as a data type when since it is not implemented shape?
Both shape and circle are data types.
And so are Main and HelloWorld data types.