Java Programming

Dr.D. Shalinigayathri

Agurchand Manmull Jain College

Meenambakkam-Chennai



Java Programming

Difference Between:

1.

Java Constructor

Java Method

A constructor is used to initialize the state of an object.

A method is used to expose the behavior of an object.

A constructor must not have a return type.

A method must have a return type.

The constructor is invoked implicitly.

The method is invoked explicitly.

The Java compiler provides a default constructor if you don't have any constructor in a class.

The method is not provided by the compiler in any case.

The constructor name must be same as the class name.

The method name may or may not be same as class name.

 

2.

static or class method

instance method

1)A method that is declared as static is known as the static method.

A method that is not declared as static is known as the instance method.

2)We don't need to create the objects to call the static methods.

The object is required to call the instance methods.

3)Non-static (instance) members cannot be accessed in the static context (static method, static block, and static nested class) directly.

Static and non-static variables both can be accessed in instance methods.

4)For example: public static int cube(int n){ return n*n*n;}

For example: public void msg(){...}.

 

3.

Method Overloading

Method Overriding

1) Method overloading increases the readability of the program.

Method overriding provides the specific implementation of the method that is already provided by its superclass.

2) Method overloading occurs within the class.

Method overriding occurs in two classes that have IS-A relationship between them.

3) In this case, the parameters must be different.

In this case, the parameters must be the same.

 

4.

SN

compile-time polymorphism

Runtime polymorphism

1

In compile-time polymorphism, call to a method is resolved at compile-time.

In runtime polymorphism, call to an overridden method is resolved at runtime.

2

It is also known as static binding, early binding, or overloading.

It is also known as dynamic binding, late binding, overriding, or dynamic method dispatch.

3

Overloading is a way to achieve compile-time polymorphism in which, we can define multiple methods or constructors with different signatures.

Overriding is a way to achieve runtime polymorphism in which, we can redefine some particular method or variable in the derived class. By using overriding, we can give some specific implementation to the base class properties in the derived class.

4

It provides fast execution because the type of an object is determined at compile-time.

It provides slower execution as compare to compile-time because the type of an object is determined at run-time.

5

Compile-time polymorphism provides less flexibility because all the things are resolved at compile-time.

Run-time polymorphism provides more flexibility because all the things are resolved at runtime.

 

5.

Abstract class

Interface

An abstract class can have a method body (non-abstract methods).

The interface has only abstract methods.

An abstract class can have instance variables.

An interface cannot have instance variables.

An abstract class can have the constructor.

The interface cannot have the constructor.

An abstract class can have static methods.

The interface cannot have static methods.

You can extend one abstract class.

You can implement multiple interfaces.

The abstract class can provide the implementation of the interface.

The Interface can't provide the implementation of the abstract class.

The abstract keyword is used to declare an abstract class.

The interface keyword is used to declare an interface.

An abstract class can extend another Java class and implement multiple Java interfaces.

An interface can extend another Java interface only.

An abstract class can be extended using keyword extends

An interface class can be implemented using keyword implements

A Java abstract class can have class members like private, protected, etc.

Members of a Java interface are public by default.

Example:
public abstract class Shape{
public abstract void draw();
}

Example:
public interface Drawable{
void draw();
}

 

6.

throw keyword

throws keyword

1) The throw keyword is used to throw an exception explicitly.

The throws keyword is used to declare an exception.

2) The checked exceptions cannot be propagated with throw only.

The checked exception can be propagated with throws

3) The throw keyword is followed by an instance.

The throws keyword is followed by class.

4) The throw keyword is used within the method.

The throws keyword is used with the method signature.

5) You cannot throw multiple exceptions.

You can declare multiple exceptions, e.g., public void method()throws IOException, SQLException.

 

7.

No.

String

StringBuffer

1)

The String class is immutable.

The StringBuffer class is mutable.

2)

The String is slow and consumes more memory when you concat too many strings because every time it creates a new instance.

The StringBuffer is fast and consumes less memory when you cancat strings.

3)

The String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.

The StringBuffer class doesn't override the equals() method of Object class.

 

8.

No.

final

finally

finalize

1)

Final is used to apply restrictions on class, method, and variable. The final class can't be inherited, final method can't be overridden, and final variable value can't be changed.

Finally is used to place important code, it will be executed whether an exception is handled or not.

Finalize is used to perform clean up processing just before an object is garbage collected.

2)

Final is a keyword.

Finally is a block.

Finalize is a method.

 

9. Threads

wait()

sleep()

1) The wait() method is defined in Object class.

The sleep() method is defined in Thread class.

2) The wait() method releases the lock.

The sleep() method doesn't release the lock.

 

10.



Comments

Post a Comment

Popular posts from this blog