Method overloading,overriding and need of it,Concept of object

Method overloading:
Let me get this straight,method overloading simply means same method with different parameters either of different data-type or different parameter size.Return type must be same.
Here's the example:
Valid overloading of methods:
void foo():nothing in the parameters list
void foo(int num):one parameter of type int
void foo(int a,int b):two parameters of type int
void foo(float a,float b):two parameters of type float
void foo(int num,float tot):two parameters of type float and int
In-Valid overloading of methods:
If the parameters of the at-least one method is exactly the same invalid overloading.
Need of overloading methods??
Method overloading is needed because there are situations where a single method have to do several kind of work.Suppose there is a method name calc_area that calculates area of different figures such as:
  1. Circle:single parameters with float value calc_area(float r).
  2. Triangle:Two parameters(base and ht,) with float values calc_area(float b,float ht).
In this case,we have to create 2 methods separately for calculating area that will take more memory.Why not create a single one and choose which to call??
Method Overriding
Quite simple concept:same method name,parameters,parameters size but one method is defined in the base class while other in child class.Although,the definition of both methods can be same.
For example:
class Base
{
void foo()
{
System.out.println("I am a base class method.");
}
}
class Child extends Base
{
void foo()
{
System.out.println("I am a child class method.");
}
public static void main(String args[])
{
Child c=new Child();
c.foo();//this will call foo method of Base class
Base b=new Base();
b.foo();//this will call foo method of Child class
}
}

Concept of object
It is necessary to know what what the object is,how the object is created,where it is stored and what is its lifetime?
Object:Smallest real-time entity that makes up the class.
Class:Collection of similar objects.
Note that this is not creation of object:A a;
This is just a reference variable with no value(or you may say address) to refer to,this process is also called declaration.
A a;
B b=a;
This too is not the object creation too as a has no value either.
This is how actually object is created:
A a=new A();
It's the second part where the magic begins(the "new A()" part).New memory location is created and referred by variable a.This process is also called instantiation and initialization.
Whenever new keyword is added to the object default constructor is called which does not have any parameters.
Properties of an object:Every object has 2 properties:member variables(what it knows) and methods(what is does?).Objects are building blocks of the program.
Consider an object named Human.So the properties of human would be:
member variables or what it knows:name,age,country,caste etc are properties of a human.
methods or what it does:walks(),dances(),talk(),eats() etc are properties of what human does.
So in code,the example will be shown as:
Human syed=new Human();//creation of object in the highlight,syed is a reference variable
syed.name="Syed zainul abedin";//member variables
syed.age=21;
syed.country="India";
syed.walk();//methods of objects
syed.dance();
syed.talk();
syed.eat();

Comments

Popular posts from this blog

Four building blocks of Object oriented programming language

Lists in java

Concept of access specifiers,Simple programs in java