Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Tuesday, January 27, 2009

Program to implement Method overloading and overriding

/**

* Write a program to implement Method overloading and overriding.

*

*

*/

public class MethodOverriding {

/**

* execution begin from here

*

* @param args

* commandline arguments

*/

public static void main(String[] args) {

methodOverloadingAndOverridingDemo();

}

/**

* use to demo. method overloading and method overriding

*

*/

public static void methodOverloadingAndOverridingDemo() {

// Method Overloading demo

System.out.println("Method Overloading Demo :: new Child(\"child\")");

Child chObj1 = new Child("child");

chObj1.displayAll();

System.out

.println("\nMethod Overloading Demo :: new Child(\"parent\",\"child\")");

Child chObj2 = new Child("parent", "child");

chObj2.displayAll();

// Method Overriding demo

System.out.println("\nMethod Overridining Demo ::");

System.out.println("\tchObj1.display() ::");

chObj1.display();

System.out.println("\n\tparObj.display() ::");

Parent parObj = chObj1;

parObj.display();

System.out.println("\n\tparObj.display() ::");

parObj = new Parent("parent");

parObj.display();

}

}

/**

* parent class with strParent string object

*

* @author Ritesh

*

*/

class Parent {

String strParent;

/**

* constructor to initialize member variable

*

* @param strParent

* input

*/

Parent(String strParent) {

this.strParent = strParent;

}

/**

* displays strParent value

*

*/

public void display() {

System.out.println("\t\tstrParent : " + strParent);

}

}

/**

* Class Child extends parent class and also does method overriding and method

* overloading

*

* @author Ritesh

*

*/

class Child extends Parent {

String strChild;

/**

* constructor with one argument

*

* @param strChild

* input

*/

Child(String strChild) {

super("DefaultValue");

this.strChild = strChild;

}

/**

* constructor with two arugments

*

* @param strParent

* input

* @param strChild

* input

*/

Child(String strParent, String strChild) {

super(strParent);

this.strChild = strChild;

}

/**

* displays values of chiild members

*/

public void display() {

System.out.println("\t\tstrChild : " + strChild);

}

/**

* display all the variables of child and parent class

*

*/

public void displayAll() {

System.out.println("\tstrChild : " + strChild);

System.out.println("\tstrParent: " + strParent);

}

}

How to access a hidden variable in corejava

/**

* Write a program which illustrates how to access a hidden variable. class G

* declares a static variable named x of type int. class H extends G and

* declares an instance variable named x of type int. a display() method in H

* displays both of these variables.

*

* @author Ritesh

*

*/

public class Inher3 {

/**

* execution begin from here

*

* @param args

*/

public static void main(String[] args) {

// creating object of T class and display variables

System.out.println("Display Values Of Static Member Variables : ");

new H().display();

}

}

/**

* class H extends G and has an instance variable named x of type int.

*

* @author Ritesh

*

*/

class H extends G {

// member variable

static int x = 2;

/**

* display all the variables of G and H classes

*

*/

public void display() {

System.out.println("\tFrom Class H -> display() :");

System.out.println("\t\tsuper.x : " + super.x);

System.out.println("\t\tG.x : " + G.x);

System.out.println("\t\tx : " + x);

System.out.println("\t\tthis.x : " + this.x);

System.out.println("\t\tH.x : " + H.x);

}

}

/**

* class G declares an instance variable named x of type int.

*

* @author Ritesh

*

*/

class G {

// static member variable of type int

static int x = 1;

}

Variable Hiding In Java

/**

* Write a program which ilustrates variable hiding.

* class S declares an instance variable named x of type Integer.

* class T extends S and has an instance variable named x of type StringBuffer.

* instantiate both of these classes.initialise and display the variable

* named x in each of these objects

*

* @author Ritesh

*

*/

public class Inher2 {

/**

* execution begin from here

*

* @param args

*/

public static void main(String[] args) {

// creating object of T class and display it

System.out.println("Display Values Of Member Variables : ");

new T(new Integer(1),new StringBuffer("Ram")).display();

}

}

/**

* class T extends S and has an instance variable named x of type StringBuffer.

* @author Ritesh

*

*/

class T extends S{

// member variable

StringBuffer x;

/**

* inialize members

* @param intObj Integer object

* @param strObj StringBuffer object

*/

T(Integer intObj,StringBuffer strObj) {

// calls the super class constructor

super(intObj);

// set the dbVar

x=strObj;

}

/**

* display all the variables of T and S

*

*/

public void display() {

System.out.println("\tFrom Class T -> display() :");

System.out.println("\t\tsuper.x : " + super.x);

System.out.println("\t\tx : " + x);

}

}

/**

* class S declares an instance variable named x of type Integer.

* @author Ritesh

*

*/

class S{

// member variable of type Integer

Integer x;

/**

* initialize the member variable

* @param intObj Integer object

*/

S(Integer intObj) {

// set the dbVar

x=intObj;

}

}

Demonstration of A Class Inheritance Heirarchy

/**

* Write a program which demonstrates a class inheritance heirarchy. class M

* extends java.lan.Object and has 2 instance varaibles of type float and

* String. class N extends M and has one instance variable of type double.

* Instantiate class N and display its variables.

*

* @author Ritesh

*

*/

public class Inher1 {

/**

* execution begin from here

*

* @param args

*/

public static void main(String[] args) {

// creating object of N class and display it

System.out.println("Display Values Of Member Variables : ");

new N(1.1f, "Ram", 1.1).display();

}

}

/**

* Class M which is parent of Class N

* @author Ritesh

*

*/

class M {

// member variable

private float flVar;

// member variable

private String strObj;

/**

* constructor to initialize variables

*

* @param flNo

* float input value

* @param strTemp

* string input value

*/

M(float flNo, String strTemp) {

flVar = flNo;

strObj = strTemp;

}

/**

* get value of flVar

*

* @return flVar

*/

public float getFlVar() {

return flVar;

}

/**

* sets value of flvar

*

* @param flVar

* input

*/

public void setFlVar(float flVar) {

this.flVar = flVar;

}

/**

* get value of strObj

*

* @return strObj

*/

public String getStrObj() {

return strObj;

}

/**

* set value of strTemp

*

* @param strTemp

* input to strObj

*/

public void setStrObj(String strTemp) {

this.strObj = strTemp;

}

}

/**

* It extends M with new variable and methods

* @author Ritesh

*

*/

class N extends M {

// member variable of type double

private double dbVar;

/**

* constructor that initialize the super constructor also

*

* @param flVar

* float input

* @param strObj

* string input

* @param dbVar

* double input

*/

N(float flVar, String strObj, double dbVar) {

// calls the super class constructor

super(flVar, strObj);

// set the dbVar

setDbVar(dbVar);

}

/**

* display all the variables of child and parent class

*

*/

public void display() {

System.out.println("\tflVar : " + getFlVar());

System.out.println("\tstrObj : " + getStrObj());

System.out.println("\tdbVar : " + getDbVar());

}

/**

* get dbVar value

*

* @return dbVar

*/

public double getDbVar() {

return dbVar;

}

/**

* set dbVar value

*

* @param dbVar

* input value

*/

public void setDbVar(double dbVar) {

this.dbVar = dbVar;

}

}

Difference Between equals() and ==

/**

* Please write a program to state the difference between .equals and ==

*

* @author Ritesh

*

*/

public class Equals {

// used for checking object

private int intNum;

/**

* constructor to initialize member variables

*

* @param no

* sets member variable

*/

Equals(int no) {

// sets intNum

setIntNum(no);

}

/**

* overriding method Object class for equal operation

*/

public boolean equals(Object obj) {

// check if input obj is null

if (obj == null)

return false;

// check if obj is instance of Equals

if (obj instanceof Equals) {

if (((Equals) obj).getIntNum() == getIntNum())

return true;

}

return false;

}

/**

* returns intNum value

*

* @return intNum

*/

public int getIntNum() {

return intNum;

}

/**

* sets intNum value

*

* @param intNum

* sets intNum

*/

public void setIntNum(int intNum) {

this.intNum = intNum;

}

/**

* execution begin from here

*

* @param args

* commandlline arguments

*/

public static void main(String[] args) {

equalsDemo();

}

public static void equalsDemo() {

// creating object 1 of Equals

Equals eqObj1 = new Equals(1);

// creating object 2 of Equals

Equals eqObj2 = new Equals(1);

// creating reference variables and sets it to reference object 1

Equals eqTemp = eqObj1;

// checking eqObj1 and eqObj2

System.out.println("Checking eqObj1 and eqObj2 :: ");

if (eqObj1 == eqObj2)

System.out.println("eqObj1 and eqObj2 references to single object");

else

System.out

.println("eqObj1 and eqObj2 references to different objects");

if (eqObj1.equals(eqObj2))

System.out.println("eqObj1 and eqObj2 have same values");

else

System.out.println("eqObj1 and eqObj2 have different values");

// checking eqObj1 and eqTemp

System.out.println("\nChecking eqObj1 and eqTemp :: ");

if (eqObj1 == eqTemp)

System.out.println("eqObj1 and eqTemp references to single object");

else

System.out

.println("eqObj1 and eqTemp references to different objects");

if (eqObj1.equals(eqTemp))

System.out.println("eqObj1 and eqTemp have same values");

else

System.out.println("eqObj1 and eqTemp have different values");

}

}

Copy One Object Array To Another Object Array

package day5;

/**

* Write a method to copy one object array to already defined object array.

*

* @author Ritesh

*

*/

public class CopyArray {

// used for checking object

private int intNum;

/**

* constructor to initialize no

*

* @param no

* input value

*/

CopyArray(int no) {

setIntNum(no);

}

/**

* copy array inputed and returns new copy of it

*

* @param cpTemp

* input array

* @return CopyArray[] output array, copy of input array

*/

public static CopyArray[] copyArray(CopyArray[] cpTemp) {

// check if input is null

if (cpTemp == null) {

System.out.println("Invalid Array Input : null value found!!");

return null;

}

// copy content

CopyArray[] cpCopy = new CopyArray[cpTemp.length];

for (int i = 0; i < cpTemp.length; i++)

cpCopy[i] = new CopyArray(cpTemp[i].getIntNum());

return cpCopy;

}

/**

* returns intNum value

*

* @return intNum

*/

public int getIntNum() {

return intNum;

}

/**

* sets intNum value

*

* @param intNum

* sets intNum

*/

public void setIntNum(int intNum) {

this.intNum = intNum;

}

/**

* execution begin from here

*

* @param args

* commandlline arguments

*/

public static void main(String[] args) {

copyArrayDemo();

}

/**

* demonstrates the array copy

*

*/

public static void copyArrayDemo() {

// creating source array

CopyArray[] cpSource = new CopyArray[5];

for (int i = 0; i < class="SpellE">i++)

cpSource[i] = new CopyArray(i);

// copy array

CopyArray[] cpCopy = CopyArray.copyArray(cpSource);

if(cpSource==cpCopy)

System.out.println("Both cpSource and cpCopy arrays references to single object array");

else

System.out.println("Both cpSource and cpCopy arrays references to different object arrays");

// displaying both the arrays

System.out.println("\nDisplaying Source Array : ");

displayArray(cpSource);

System.out.println("\nDisplaying Copied Array : ");

displayArray(cpCopy);

}

/**

* displays the content of inpted array

* @param cpTemp input array

*/

public static void displayArray(CopyArray[] cpTemp) {

if (cpTemp == null) {

System.out.println("Null input found!!");

return;

}

for (int i = 0; i < cpTemp.length; i++)

System.out.println("\t[" + i + "] : " + cpTemp[i].getIntNum());

}

}

Wednesday, January 21, 2009

Program For Type Casting

/**

* Write a program for type cast following int -> double double -> float float ->

* double String -> int double -> int float -> int

*

*

*/

public class TypeCasting {

/**

* typecasting with Exception handling

*/

public static void doTypeCasting() {

try {

int intNo = 11;

double dblNo = 1.1d;

float flNo = 2.2f;

String strNo = "11";

// int -> double

dblNo = intNo;

System.out.println("int : " + intNo + " to double :" + dblNo);

// double to float

dblNo = 1.1d;

flNo = (float) dblNo;

System.out.println("double : " + dblNo + " to float : " + flNo);

// float to double

flNo = 2.2f;

dblNo = flNo;

System.out.println("float : " + flNo + " to double : " + dblNo);

// string -> int

strNo = "11";

intNo = Integer.parseInt(strNo);

System.out.println("String : " + strNo + " to int : " + intNo);

// float to int

flNo = 1.9f;

intNo = (int) flNo;

System.out.println("float : " + flNo + " to int : " + intNo);

// double to int

dblNo = 1.9;

intNo = (int) dblNo;

System.out.println("double : " + dblNo + " to int : " + intNo);

} catch (Exception e) {

System.out.println("Error Occured!!");

e.printStackTrace();

}

}

/**

* execution begin from here

*

* @param args

* commandline arguments

*/

public static void main(String[] args) {

doTypeCasting();

}

}

Followers