Method Overloading In Java

In Method overloading a class have multiple methods with same name but different parameters.
Method overloading increases readability of the program.
Suppose we have to do addition of two numbers but those two numbers can be integers or floating point numbers.
If we have to write methods to add two integers then it will be like abc(int , int ) and for float it will be like
xyz(float , float ) . It makes makes difficult to understand the behavior . Same is the issue when we want methods to
add two integers and three integers.
Method overloading allows us to declare multiple methods with same name and different parameter list.
So methods to add two integers ,two floats and three integers will have same name
add(int ,int )
add(float ,float )
add(int ,int ,int )

There are two ways to implement method overloading in java
1. By having different number of arguments.
2. By having arguments with different data types

Method overloading with different number of arguments

Here we will create two methods with same name add. First method will have two two arguments while second method will have three arguments

class Calculation {
void add(int a, int b) {
System.out.println(a + b);
}

void add(int a, int b, int c) {
System.out.println(a + b + c);
}

public static void main(String[] args) {
Calculation obj = new Calculation();
obj.add(10, 20);
obj.add(10, 20, 30);

}
}

Output

30                                                                                                                                                             60

Method overloading with arguments with different data types

Advertisements

Difference between method Overloading and Overriding in java

In this article we will see the difference between method overloading and method overriding.

Overloading Vs. Overriding is one the confusing thing in Java. So for to understand difference clearly it is nice to go through following articles

Method Overloading Method Overriding
Overloading happens in same class Overriding happens in base and child class
Overloading happens at compile time Overriding happens at run time
We can overload static methods We can not override static Method
We can overload static methods We can not override static Method
We can overload private & final methods We can not override private & final Method
Argument list must be different Argument list may or may not be same
Return type should be same Return type can be same

Method Overriding in Java

In method overriding sub class or child class has same method as that of parent class If a child class requires a different definition for an inherited method, then that method can be redefined in the child class.

Except definition an overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class..

Method overriding is best example of run time Polymorphism.

 

Ex :- A class animal can hold object of class animal and object of class cow  which is subclass of animal.

Following statements are correct

  • Animal obj1 = New Animal();
  • Animal obj2 = New cow();
  • In case of method overriding both classes have  same method so at compile time it is not clear which method should be called. So at run time JVM decides which method to call . This is why this concept is know as Runtime polymorphism.
  • Here is example for detailed understanding.
public class Animal
{
    public void sound() //Base class method
    {
        System.out.println ("hello, this is sound from class Animal");
    }
}

public class Cow extends Animal
{
    public void sound() //Derived Class method
    {
        System.out.println ("hello, this is sound from class Cow");
    }
}
public class A
{
   public static void main (String args []) {
       Animal obj1 = new Animal(); // Reference and object Animal
       Animal obj2 = new Cow(); // Animal reference but Cow object
       obj1.sound();
       obj2.sound();
   }
}

Output

hello, this is sound from class Animal
hello, this is sound from class Cow

					

Types of polymorphism in java

In this post I will simplify the concepts of types of polymorphism in java  with the help of  method overloading and method overriding.
In java- Runtime polymorhism( Dynamic polymorphism) and Compile time polymorphism (static polymorphism).

Runtime polymorphism( Dynamic polymorphism)

  • Best example for Runtime polymorphism is method overriding.
  • In runtime polymorphism a reference class can hold object of itself and object of any class which extends reference class.
    • Ex :- A class animal can hold object of class animal and object of class cow  which is subclass of animal.
  • Following statements are correct
    • Animal obj1 = New Animal();
    • Animal obj2 = New cow();
  • In case of method overriding both classes have  same method so at compile time it is not clear which method should be called. So at run time JVM decides which method to call . This is why this concept is know as Runtime polymorphism.
  • Here is example for detailed understanding.
public class Animal
{
    public void sound() //Base class method
    {
        System.out.println ("hello, this is sound from class Animal");
    }
}

public class Cow extends Animal
{
    public void sound() //Derived Class method
    {
        System.out.println ("hello, this is sound from class Cow");
    }
}
public class A
{
   public static void main (String args []) {
       Animal obj1 = new Animal(); // Reference and object Animal
       Animal obj2 = new Cow(); // Animal reference but Cow object
       obj1.sound();
       obj2.sound();
   }
}

output:-
hello, this is sound from class Animal
hello, this is sound from class Cow
 

 

 

Compile time Polymorphism( Static polymorphism)

In java Compile time polymorphism is synonym to method overloading . In method overloading a class can have more than one methods with same name but different number of arguments or different types of arguments or both.

Following Example will help to understand the idea

class Area
{
   void calculateArea(int length, int breadth)
   {
       System.out.println ("Area of rectangle :" + (lengh * breadth));
   }
   void calculateArea(int radius)
   {
       System.out.println ("Area of Circle is:" +(3.142*r*r));
   }
   double calculateArea(double a) {
       System.out.println("Surface area of cube is :" + (6*a*a));
      }
}

class A
{
   public static void main (String args [])
   {
       Area Obj = new Area();
      
       Obj.calculateArea(20,30);
       Obj.calculateArea(7.7);
       Obj.calculateArea(7.12)
   }
}


Output
Area of rectangle is: 600
Area of Circle is : 186.27

Surface area of cube is : 304.17

In above code class Area has 3 methods with same name (calculateArea) but with different number of argument or different type of arguments. Here compiler can figure out which method to call at compile time hence this concept is called as Compile time polymorphism.

Oracle Interview Experience

Hi Friends I have gone through four rounds of Interview for Oracle India Private Limited.
All four rounds were on coding.
Here is my experience
ROUND 1

1.Write a program to reverse a string without using temporary variable.
2.Write a program to print given number in word format.
example :- input 107020
output

one lack seven thousand twenty

ROUND 2

1.Write a program to read a file and print total number of words,line and alphabets in that file.
2.Write a program to add a new node at appropriate place in a sorted linked list.

ROUND 3

1.Write a program using pointers to print string with in range given by user as input.
2.Write a program to parse a quadratic equation and give its solution.
3.Tell algorithm to solve following
There are 10 duplicate files numbered from 1 to 10 and one of then is having extra character .
Find that particular file.
4. Write a code for sorting.
5. Solve following puzzle
There are 9 coins out of those 9 coins one is having more weight .
Tell how you will get coin with more weight in optimal weight.

ROUND 4

1.Write sql query to find people who are having top 5 salaries.
2.Solve following two puzzles
i.There are three items with value 1 rupee ,5 rupee and 20 paisa .
You have hundred rupee get 100 items such that you take atleast one item of each type.
ii.you have four sevens and following operators +,-,*,/ and decimal.
Using four 7 and above operators get output as 100. You are allowed to use operators mentioned multiple times but only four 7 are available.
3.Write a program to get Bangalore railway station time table .
Take start and end time from user .
Display the difference between actual and scheduled time with in the limit given by user.

Give your best NEVER SAY NO
ALL THE BEST
Please chant and be happy
Maha-mantra

Adobe C and Aptitude test

Hi Friends I just wrote my adobe written test .
Here is what they asked

George adores classical music. He always prefers Beethoven to Bartok and Mahler to Mozart. He prefers
Haydn to Hindernith and Hlitdeiulth to Mozart. He always prefers Mahler to any whose name begins with B,
except Beethoven, and he always chooses to listen to a he prefers.

George’s brother gives him one recording by each of the composers mentioned. Which of the following correctly states the
order in which George must play some of the records?

Pick one of the choices
Beethoven, Bartok, Mozart
Haydn, Hindemith, Mozart
Beethoven, Mahler, Bartok
Hindemith, Mahier, Mozart
Haydn, Hindemith, Mahler
Clear Selection;1]

George adores classical music. He always prefers Beethoven to Bartok and Mahler to Mozart. He always
prefers Haydn to Hindemith and Hindeniith to Mozart. He always prefers Mahler to any composer whose
nanie begins with B, except Beethoven, and he always chooses to listen to a composer he prefers.

Which of the following can not be true?

Pick one of the choices
George prefers Mahler to Bartok
O George prefers Beethoven to Mahler
O George prefers Bartok to Mozart
O George prefers Mozart to Beethoven
0 George prefers Mahler to Haydn

George adores classical music. He always prefers Beethoven to Bartok and Mahler to Mozart. He prefers
Haydn to Hindemith and Hindemith to Mozart. He always prefers Mahler to any whose name begins with B,
except Beethoven, and he always chooses to listen to a he prefers.

George’s mother also gives him one recording by each composer mentioned. Which of the following can not occur?
Pick one of the

George plays the Beethoven first
George plays the Haydn first
George plays the Mahler third
George plays the Beethoven fifth
George plays the Bartok last

George adores classical music. He always prefers Beethoven to Bartok and Mahler to Mozart. He prefers
Haydn to Hindeniith and Hindernith to Mozart. He always prefers Mahler to any whose name beçjins with B,
except Beethoven, and he always chooses to listen to a he prefers.

George’s father gives him several records. If the first record he plays is by Berlioz, which of following must be true?
Pick one of the choices

There Is no record by Hindemith
There is no record by Bartok
One of the records may be by Haydn
If there Is a record by Haydn, George will play It second
There is no record by Beethoven

If MILK is written as 1391211 How would you write CURD

Pick one of the choices
311073
3121184
221194
223173
None of the above

If you rearrange the letters ITORSHC you’ll get the name of a famous.

Pick one of the choices
Cty
Animal
Sportsperson
Movie Star

1. At a baseball game, five nien – L, M, N, 0 and P, and five women – S, T, U, V and W occupy a row of
ten seats. The men are in odd numbered seats, starting from the left, each women sits to the right of
the man she is dating
2. OisV’sdate
3. W is not at the right end of the row and the man she is dating is not at the left end of the row
4. N Is one seat froiii the right end of the row
5. The man dating W likes T best among the other women and insists on being seated to the right of T
6. M and his date occupy the middle pair of seats
7. V sits next to M

Which of the following could be determined exactly if the position of either L or P were given
pick one of the choices
The identity of L’s date
The identity of P’s date
The identity of L’s date or P’s date, but not both
Which woman Is not seated between two men
Who is sitting to 0’s left

1. 0 is V’s date
2. W is not at the right end of the row and the man she is dating is not at the left end of the row
3. N is one seat from the right end of the row
4. At a baseball game, five men – L, M, N, 0 and P, and five women – S, T, U, V and W occupy a row of
ten seats. The men are in odd numbered seats, starting from the left, each women sits to the right of
the man she is dating
5. The man dating W likes T best among the other women and insists on being seated to the right of T
6. M and his date occupy the middle pair of seats
7. V sits next to M
In order to determine the position of T, it is necessary to use how many of the numbered statements?
Pick one of the choices
3
4
6
7

Both B and D are prerequisites for E
Both B and C are prerequisites for F
E may occur without B only if A precedes D
A in combination with B is an alternative prerequisites for F
H will occur if E or F occurs, but not if both occur
H in combination with any two unused prerequisites for E or F will yield J
If two sets of prerequisites is sufficient two yield a result both occur, the result will not occur
Any item acts simultaneously as part of all sets of prerequisites of which it is a member and which of the
other members are present

J cannot occur if
Pick one of the choices
A is the first item to occur
D is the first item to occur
B precedes D
0 A follows BorC

Both B and D are prerequisites for [
Both B and C are prerequisites for F
E may occur without B only if A precedes D
A in combination with B is an alternative prerequisites for F
H will occur if E or F occurs, but not if both occur
H in combination with any two unused prerequisites for E or F will yield J
If two sets of prerequisites is sufficient two yield a result both occur, the result will not occur
Any Item acts simultaneously as part of all sets of prerequisites of which It Is a iiiember and wtiicli of the
other members are present
B precedes D. Which of the following condition is (are) sufficient for H to occur?
I. A occurs but not before D
II. C occurs
III. A also precedes D

Both B and D are prerequisites for E
Both B and C are prerequisites for F
E may occur without B only if A precedes D
A in combination with B is an alternative prerequisites for F
H will occur if E or F occurs, but not if both occur
H in combination with any two unused prerequisites for E or F will yield J
If two sets of prerequisites is sufficient two yield a result both occur, the result will not occur
Any item acts simultaneously as part of all sets of prerequisites of which it is a member and which of the
other members are present
C occurs. J will occur if
Pick one of the choices
B occurs
B and D occur
A precedes D
B and D occur, followed by A

A trapezoid ABCD is formed by adding the isosceles right triangle BCE with base 5 inches to the rectanqh
ABED where DE Is t Inches. What Is the area of the trapezoid In square Inches?
Pick one of the choices
5t+ 12.5
5t + 25
2.5t + 12.5
(t + 5)2
t2+25

A car travels 75% of the way from town A to town B by travelling for T hours at an average speed of V mph.
The car travels at an average speed of S mph for the remaining part of the trip. Which of the following
expressions represents the time the car travelled at S mph?
Pick one of the thoIct
VT/S
VS/4T
4VT/3S
3S/VT
VT/3S

If x/y = 3/z then 9y2 equals
Pick one of the choices
x2/9
x3z
x2z2
3×2
(1/9)x2z2

The operation * applied to a number gives as its result 10 subtracted from twice the number. What is * (*9)?
Pick one of the choices

6
8
9
36

You are to compare the two quantities and choose correct option…. A, B, C or D
Choose A if the quantity in Column A is greater.
Choose B if the quantity in Column B is greater
Choose C if the two quantities are equal
Choose D if the relationship cannot be determined from the information given

Information Column A Column B
x>o 10/x 10/x

Pick one of the choices
Column A is greater
Column B is greater
Column A and B are equal
Relationship cannot be determined from the information given

You are to compare the two quantities and choose correct option…. A, B, C or D
Choose A if the quantity in Column A is greater.
Choose B if the quantity in Column B is greater
Choose C if the two quantities are equal
Choose D if the relationship cannot be determined from the information given

Information Column A Column B
compare-> 197+398+586 203+405+607

Pick one of the choices
Column A is greater
Column B is greater
Column A and B are equal
Relationship cannot be determined from the information given

two white O O inside big black o
What is the area of the shaded region? The radius of the outer circle is a and the radius of each of the circles inside the larger
circle is a/3
Pickone of the choices
0
(1/3)na2
(2/3)na2
(7/9)na2
(8/9)na2

It x2 — y2 =15, and x + y =3 then x — y is
Pick one of the choices
-3
0
3
5
Cannot be determined

The ratio of the chickens to pigs to horses on a farm can be expressed as the triple ratIo 20:4:6 If there are
120 chickens on the farm, then the number of horses on the farm Is
Pickone of the
4
6
24
36
60

A cylinder of radius 5 mm
and height 11 mm
(volume of a cylinder is pie-r2h)

A sphere of radius 6mm
(volume of a sphere is 4/3 pie r3)

A cube with edge 9mm
(volume of a cube is e3)

Which of the following solids has the largest volume? (figures are not drawn to scale)
Pickone of the choices
I
II
III
I &II
ii & iii

A pension fund has a total of $1 million invested in stock of the ABC company and the bonds of the DEE
Corporation. The ABC stocks yields 12% in cash each year, and the DEE bonds pay 10% in cash each year.
The pension fund received a total of $115,000 in cash from ABC stock and the DEE bonds last year. How much
money was invested in ABC stock?
Pickone of the choices
$750,000
$600,000
$500,000
$333,333.33
$250,000

36 identical chairs must be arranged in rows with the same number of chairs in each row. Each row must
contain at least three chairs and there must be at least three rows. The row is parallel to the front of the
room. How many different arrangements are possible?
Pick one of the choices
2
4
5
6
10

ABCD is a rectangle. The length of BE is 4 and the length of EC is 6. The area of the triangle BEA plus the
area of the triangle DCE minus the area of the triangle AFO is
Pick one of the choices
0
0.4 of the area of the triangle AEB
0.5 of the area of the triangle AED
0.5 of the area of ABCD
cannot be determined

In a group of people solicited by a charity, 30% contributed $40 each, 45% contributed $10 each and the
rest contributed $12 each. What is the percentage of the total contributed came from the people who gave
$40?
Pick one of the choices
0 25%
030%
040%
0 45%
0 50%

C white box profile Questions

Give the output of following
1.

#include

int main()

{

enum days {MON, TUE, WED=6, THU, FRI=-1, SAT};

printf(“%d, %d, %d, %d, %d, %d\n”, MON, TUE, WED, THU, FRI, SAT);

return 0;

}

2.

#include
#define max 10
void main()
{
int i;
i=++max;
clrscr();
printf(“%d”,i);
getch();
}

3.

#include
int main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf(“x = %d\n”, x);
else
printf(“y = %d\n”, y);
return 0;
}

4.

#include
int main()
{
#if 0
int _=10;
int __=5;
int ___;
___=_-__;
printf(“%i”,___);
#endif
double x = -3.5, y = 3.5;
printf( “%.0f : %.0f\n”, ceil( x ), ceil( y ));
printf( “%.0f : %.0f\n”, floor( x ), floor( y ));

return 0;
}

5.

#include
#include
int main()
{
double x = -3.5, y = 3.5;
printf( “%.0f : %.0f\n”, ceil( x ), ceil( y ));
printf( “%.0f : %.0f\n”, floor( x ), floor( y ));

return 0;
}

6.

#include
void main()
{
short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} };
printf( “%d\n”, sizeof( testarray ) );

}

7.

#include
void main()
{
int y[4] = {6, 7, 8, 9};

int *ptr = y + 2;

printf(“%d\n”, ptr[ 1 ] ); /*ptr+1 == ptr[1]*/
}

8.

#include
void main()
{
int z;
int i = 4;
int x = 6;
z = x / i;
printf(“z=%.2f\n”, z);
}

9.

#include
void func()
{
int x = 0;
static int y = 0;
x++; ++y;
printf( “%d — %d\n”, x, y );
}

int main()
{
func();
func();
return 0;
}

10.

WAP for following

In a program, using the ‘rand’ function you can generate a fixed sequence of random numbers every time the program is run. How do you make the sequence different in each run?

11.

WAP for multiplication of 2*2 matrix

12

WAP to print following

*********
*******
*****
***
*

There were 18 questions I do not remember them all. But they were simple
All the best

Sharing Folder between UBUNTU guest and WINDOWS host on virtual box

Hi all ,

There are lots of video’s and blogs regarding sharing folder but when I encountered this problem I did not find any thing which can give me a complete solution

I fought it out and after 3-4 hours research I got one complete solution of my own .

I thought to put a simple but complete process to achieve this complex work

Here are the steps

1. Install Guest Additions

  • Log in to your Ubuntu guest machine.
  • Go to Devices and there you will find Install Guest Additions .

installguestadditions

  • One pop up will come about virtual box addition click on OK button and then select Run option
  • Provide your password and things will run automatically.
  • Restart your Ubuntu Virtual Machine.

2. Share Folder from you host Windows machine

  • Open your virtual box Manager .
  • Select your guest virtual machine.
  • Go to settings there you will find share folder option

shared folder

Note :- Select Auto Mount and Make Permanent options.

3. Get Permissions

  • Log in to your host Ubuntu and navigate to media folder .
  • There you will find your folder is shared  but you don’t have permissions to access it
  • In case if try to access shared folder you will get following pop up

getting permission

  • open terminal and put following command

sudo usermod  -a -G vboxsf pravin

note:- instead of pravin put your username

  • Restart your Ubuntu virtual machine
  • That is it . You can go to “media” folder and access shared folder

I will upload one video to make this process more understandable shortly

Thanks . All kinds of doubts and comments are always welcome .