Friday, June 19, 2009

Difference between Constructors and Methods

Difference b/w Constructors and Methods

Question

What does a constructor meant for? And how it differs from methods?

Answer

Constructor is to create instance of a class, you may call it as "creating an object". While methods are just to execute a piece of java code. Constructors and methods differ in three aspects of the signature: modifiers, return type, and name.

--> Constructors dont have return types, not even void.

--> Like methods, Constructors can have any of the access modifiers: public, protected, private or none. But unlike methods, they cannot be abstract, final, native, static or synchronized.

--> Name of the constructor is always same as that of the Class to which it belongs to, while methods normally have names other than their class name.



For Further Reading:

http://www.ccnyddm.com/AdvJava/java_constructor_tutorial.htm

Thursday, June 18, 2009

Difference Between Abstract Class and Interface

Difference between abstract Class and Interface

a) All the methods declared inside an interface are abstract whereas abstract class must have at least one abstract method and others may be concrete or abstract.

b) In abstract class, key word abstract must be used for the methods
Whereas interface we need not use that keyword for the methods.

c) Abstract class must have subclasses whereas interface can't have subclasses.

d) By default what ever variables we decalre in interface are public static final,where as in abstract class they can be default and instance variables also


Question

What does the keyword 'static' mean?

Answer

The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.

Welcome !!!!!

Hi

This blog is for those who are looking for quick learning on various technology related topics. Since I work on Java, most of the posts would contain information on Java...
I hope this would be useful .....Wish you good luck..

Viswanath