Вы находитесь на странице: 1из 2

Java Constructor Java Method

Method is used to expose behaviour


Constructor is used to initialize the state of an object.
of an object.
Constructor must not have return type. Method must have return type.
Constructor is invoked implicitly. Method is invoked explicitly.
The java compiler provides a default constructor if you Method is not provided by compiler
don't have any constructor. in any case.
Method name may or may not be
Constructor name must be same as the class name.
same as class name.

Abstract class Interface


1) Abstract class can have abstract and Interface can have only abstract methods. Since Java
non-abstract methods. 8, it can have default and static methods also.
2) Abstract class doesn't support
Interface supports multiple inheritance.
multiple inheritance.
3) Abstract class can have final, non-
Interface has only static and final variables.
final, static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
5) The abstract keyword is used to
The interface keyword is used to declare interface.
declare abstract class.
6) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

No. Method Overloading Method Overriding


Method overriding is used to provide
Method overloading is used to increase the the specific implementation of the
1)
readability of the program. method that is already provided by its
super class.
Method overriding occurs in two
2) Method overloading is performed within class. classes that have IS-A (inheritance)
relationship.
In case of method overloading, parameter must be In case of method overriding,
3)
different. parameter must be same.
Method overloading is the example of compile time Method overriding is the example of
4)
polymorphism. run time polymorphism.
In java, method overloading can't be performed by
changing return type of the method only. Return type Return type must be same or
5)
can be same or different in method overloading. But covariant in method overriding.
you must have to change the parameter.

Вам также может понравиться