August 7, 2008 at 1:33 PM
Hi friend,
As per your problem
public class ConstructorOverloading{
public static void main(String args[]){
Rectangle rectangle1=new Rectangle(2,4);
int areaInFirstConstructor=rectangle1.first();
System.out.println(" The area of a rectangle in first constructor is : " + areaInFirstConstructor);
Rectangle rectangle2=new Rectangle(8,9,10);
int areaInTwoConstructor=rectangle2.first();
System.out.println(" The area of a rectangle in first constructor is : " + areaInTwoConstructor);
}
}
class Rectangle{
int l, b;
float p, q;
public Rectangle(int x, int y){
l = x;
b = y;
}
public int first(){
return(l * b);
}
public Rectangle(int i,int j,int k){
this(i,j);
}
}
---------------------------------------------------------------------
Ans 2.
Polymorphism :-
Polymorphism means the ability to take more than one form. Polymorphism means 'any forms.' In OOP, it refers to the capability of objects to react differently for the same method.
In OOP methodoverloading refers to the capability of objects to react differently for the same method. Polymorphism can be implemented in Java language in form of multiple methods having the same method name. Java code uses a late-binding for supporting polymorphism, the method which is invoked is decided at runtime.
In polymorphism by using a same function name with the different signature we can perform the multiple task this is called method overloading & using a same function name with the same signature we can extend the functionality of the superclass functions according to our requirement.
In java Polymorphism can be used in two ways :
1. Method Overloading
2. Method Overriding
1. Method Overloading :-
Overloaded methods are methods have the same name, but different argument lists.
Overloaded methods have the same names but different argument lists. The arguments may differ in type or number, or both. However, the return types of overloaded methods can be the same or different.
An example of the method overloading is given below:
class methodOverloading{
int add( int a,int b)
{
return(a+b);
}
float add(float a,float b)
{
return(a+b);
}
double add( int a, double b,double c)
{
return(a+b+c);
}
}
class mainClass extends methodOverloading
{
public static void main( String arr[] )
{
mainClass temp = new mainClass();
System.out.println(temp.add(10,20));
System.out.println(temp.add(1.5f,2.3f));
System.out.println(temp.add(10,20.4,25.6));
}
}
The output of the above program is given below:
30
3.8
56.0
2. Method Overriding :-
Overriding means when a subclass method has the same name, same return type, and same argument list as the superclass method.
class methodOverriding{
String showMessage(){
return("THIS IS SUPERCLASS METHOD");
}
}
class mainClass extends methodOverriding
{
public static void main( String arr[] )
{
String showMessage()
{
System.out.println("THIS IS SUBCLASS METHOD");
}
mainClass temp = new mainClass();
System.out.println(temp.showMessage());
}
}
The output of the above program is given below:
THIS IS SUBCLASS METHOD
Related Tutorials/Questions & Answers:
Question in Java ?? Question in
Java ?? Welcome every One ,I have
Q in
Java :
Write aprogram that print the falewing table using SQRT method in the Math Class?
Number squrfoot
Question in Java ??Question in
Java ?? Welcome every One ,I have
Q in
Java :
Write aprogram that print the falewing table using SQRT method in the Math Class?
Number
0 ,2,
.
.
.
,18,20
squrfoot
00000
1.4142
5.2426
5.4721
Advertisements
Question in Java ??Question in
Java ?? Welcome every One ,I have
Q in
Java :
Write aprogram that print the falewing table using SQRT method in the Math Class?
Number
0 ,2,
.
.
.
,18,20
squrfoot
00000
1.4142
5.2426
5.4721
Question on javaQuestion on java why
java is not purely oop?
java is not purely OOP because
1.) Multiple Inheritance is not supported.ADS_TO_REPLACE_1
2.) It uses primitive data type.
3.) static method can be called without
Question Java>?Question Java>? **ïâ??· Create a new class called Bank and in the main method use class Account (lab 4) to create an array of 10 accounts.
ïâ??· Enter data for all accounts (account number,name, balance).
ïâ??· Add
java question java question Create a class called Complex to perform arithmetic operations with complex numbers.
1- Use double variables to represent the fields of the class.
2- Provide a no-argument constructor with default values in case
java question :)java question :) write
java program to use vector in ArrayList with add, remove,sort
import java.util.Vector;
public class... Element MUAYAD");
vc.add("Vector Element
JAVA");
vc.add("Vector Element
java questionjava question the purpose of defining multiple constructor in
java JAVA QUESTIONJAVA QUESTION How to view image on Frame in swing(or)awt in
Java JAVA QUESTIONJAVA QUESTION How to view image on Frame in swing(or)awt in
Java JAVA QUESTIONJAVA QUESTION How to view image on Frame in swing(or)awt in
Java java questionjava question what is the difference between path and classpath in
java java questionjava question How to find the longest and shortest line from the file in case of
java and
java program should read the filename from the keyboard
java questionjava question Please, give me sample program for insert Audio(sound) on Frame in swing in
java java questionjava question how to write a
java program that, given a friend name , display the phone number and the email address for that friend
java questionjava question anyone provide the code to call c++ function from
java
Here you will find details how to implement the c++ into the
java.
http://www.javaworld.com/javatips/jw-javatip23.html
Java QuestionJava Question can u give me one expample in which conditions we... in
java includes only abstract methods i.e. methods have signatures only (or we... in
java defines that it must be implemented by any class to make use of it.
One
java questionjava question input any number and check if sum of alternate digit is palindrome
java questionjava question what are the exactly J2EE concepts..
struts and hybernates coming under which catagiry
java questionjava question i wrote to
java code to get data populated in the Revision field of table InventDim
2.Failing to successfully do the above, i tried to use the ConfigId field of table InventDim for the same purpose, by labeling
java questionjava question how to create date and time based pgm in
java... in db table if date and time crossed automatically update in the db and inform to login user as pop ups
http://www.roseindia.net/jsp/loginstatus.shtml
java questionjava question Can any one send me the source code to get confirmation after the successful registration to our mobile
java questionjava question when we use the Abstract class and interface?
Please visit the following link:
Abstract class and Interface
java questionjava question sir, can you give me the code for online banking using netbeans and mysql in j2ee with its database connectivity
java questionjava question sir, can you give me the code for online banking using netbeans and mysql in j2ee with its database connectivity
java questionjava question I am converting a .net website into
java one.
Can you help me how to code things step by step.
project name
- src
-
java
-main
-webapp
- common
- jsp
- css
- images
- javascripts
I
java Questionjava Question write a program that reads an integer and check whether it is even. for example,
if your input is 25, the should be: is 25 an even number? false
if your input is 2000, the should be: is 2000 an even number?true
Java questionJava question Write a program that converts a (C to F Converter from 0 - 20) and writes the output to a file instead of the screen. Open the file in Notepad to confirm the output
java Questionjava Question write a program that reads an integer and check whether it is even. for example,
if your input is 25, the should be: is 25 an even number? false
if your input is 2000, the should be: is 2000 an even number?true
java questionjava question i have a string like "My name is arvind.i live in bangalore.i study in college.".the problem is that i hav to break this string into three seperate lines
java questionjava question comparator and comparable
Differences:
a)A comparable object is capable of comparing itself with another object while...:
http://www.roseindia.net/help/
java/c/comparable-interface.shtml
http
java Questionjava Question use nested loops that print the following patterns in four separate program.
Pattern1
1
12
123
1234
12345
123456
Pattern2
123456
12345
1234
123
12
1
Pattern3
1
21
321
4321
54321
654321
Pattern4
java Questionjava Question use nested loops that print the following patterns in four separate program.
Pattern1
1
12
123
1234
12345
123456
Pattern2
123456
12345
1234
123
12
1
Pattern3
1
21
321
4321
54321
654321
Pattern4
java Questionjava Question use nested loops that print the following patterns in four separate program.
Pattern1
1
12
123
1234
12345
123456
Pattern2
123456
12345
1234
123
12
1
Pattern3
1
21
321
4321
54321
654321
Pattern4
java Questionjava Question use nested loops that print the following patterns in four separate program.
Pattern1
1
12
123
1234
12345
123456
Pattern2
123456
12345
1234
123
12
1
Pattern3
1
21
321
4321
54321
654321
Pattern4
java Questionjava Question use nested loops that print the following patterns in four separate program.
Pattern1
1
12
123
1234
12345
123456
Pattern2
123456
12345
1234
123
12
1
Pattern3
1
21
321
4321
54321
654321
Pattern4
java questionjava question Hello sir/mam,
In collection there is method called toArray() which is used to convert collection into array at tat time array is used 2 store diffferent objects,how is it possible can you describe it briefly
java questionjava question Hello sir/mam,
In collection there is method called toArray() which is used to convert collection into array at tat time array is used 2 store diffferent objects,how is it possible can you describe it briefly
java Questionjava Question use nested loops that print the following patterns in four separate program.
Pattern1
1
12
123
1234
12345
123456
Pattern2
123456
12345
1234
123
12
1
Pattern3
1
21
321
4321
54321
654321
Pattern4
java Questionjava Question use nested loops that print the following patterns in four separate program.
Pattern1
1
12
123
1234
12345
123456
Pattern2
123456
12345
1234
123
12
1
Pattern3
1
21
321
4321
54321
654321
Pattern4
java questionjava question the DTO classes and the relation between the classes is given in the below Diagram.
Please create a method which will compare two ServiceDefinitionDTO and returns those fields which are not equal.
You can write
java questionjava question Hello sir/mam,
In collection there is method called toArray() which is used to convert collection into array at tat time array is used 2 store diffferent objects,how is it possible can you describe it briefly
java questionjava question Hello sir/mam,
In collection there is method called toArray() which is used to convert collection into array at tat time array is used 2 store diffferent objects,how is it possible can you describe it briefly
java questionjava question what r the Comparator vs Comparable
Differences:
a)A comparable object is capable of comparing itself with another... the following links:
http://www.roseindia.net/help/
java/c/comparable-interface.shtml
http
Java QuestionJava Question how can we increment the value of database SQL by using the
java servlet program ? plz help me
Hi Friend,
Try the following code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.
JAVA QUESTIONJAVA QUESTION HI I CAN WRITE A STRUTS APPLICATION THROUH FORM I CAN ENTER THE DATA
THAT WILL BE SUCCESSFULLY SAVED INTO DATA BASE
WITH OUT WRITING ACTION FORM I CAN WRITE
IN STRUTS COFNFIG FILE USE
java questionjava question i created a form in one jsp page and in another jsp page i retrieved all the details which the user had entered ,in second jsp in a tabular format and added a delete button to every row of dat table now i want
java Questionjava Question Design a class named rectangle to represent a rectangle. The Class contain:
Two double data fielda named width and heigth that specify the width and height of the rectangle.the default values are 1 for both