self-java
self-java
Method Overriding:
If a child class has the same method which is already defined in its parent class,
then it is known as method overriding in Java.
Note: -
1. A static method cannot be overridden. Because static method is bound with
class whereas instance method is bound with object. Static belongs to class
area and instance belongs to heap area.
2. We cannot override main() method because main() is a static method.
Overloading Methods:
• The Java programming language supports overloading methods, and Java can
distinguish between methods with different method signatures. This means
that methods within a class can have the same name if they have different
parameter lists.
• In the Java programming language, you can use the same name for all the
drawing methods but pass a different argument list to each method. Thus, the
data drawing class might declare four methods named draw, each of which has
a different parameter list.
• Overloaded methods are differentiated by the number and the type of the
arguments passed into the method.
• You cannot declare more than one method with the same name and the
same number and type of arguments, because the compiler cannot tell them
apart.
• The compiler does not consider return type when differentiating methods, so
you cannot declare two methods with the same signature even if they have a
different return type.
• Overloaded methods should be used sparingly, as they can make code much
less readable.
Constructors:
1.A constructor is a special member function.
2.A constructor is similar to class name.
3.A constructor is invoked automatically whenever an object is created.
4.A constructor has no return type; not even void.
5.It cannot be abstract, final, native, static or synchronized.
Constructors are classified into 3 types:
They are:
1.Default con/ No argument con.
2.Parameterized con/ argument con.
3.Overloaded con.
Default constructor:
A constructor function without having parameters, those constructors are
called as default constructors or non-parameterized constructor.
Ex:
Class example
{
Int p,q;
Example()
{
P=10;
Q=20;
}
}
Parameterized constructor:
A constructor function having parameters, those constructors are called as
parameterized constructors.
Ex:
Class example
{
Int p,q;
Example(int x, int y)
{
P=x;
Q=y;
}
}
overloaded constructor:
A constructor contain more then one constructor function, those constructors
are called as overloaded constructors.
Ex:
Class example
{
Int p,q;
Example()
{
P=10;
Q=20;
}
Example(int x, int y)
{
P=x;
Q=y;
}