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.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
Last edited by sagar474; September 18th, 2011 at 02:11 PM.
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.
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!
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.