Advertisements

Archive

Archive for January 11, 2009

Java Program to show Overriding Methods – Q22

January 11, 2009 Leave a comment

Q22: Java Program to show over-riding methods

Method Overriding allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes.

class base_cl{
	public void test(){
		System.out.println("Function test called of Base class");
	}
}

class derived_cl extends base_cl{
	public void test(){
		System.out.println("Function test called of Derived 
    class");
	}
}

class over_ride{
	public static void main(String args[]){
		
		System.out.println("\n Base class Instanciated.");
		base_cl Ob = new base_cl();
		Ob.test();
		
		System.out.println("\n Derived class Instanciated.");
		derived_cl Od = new derived_cl();
		Od.test();
	}
}

… from College notes (BCA/MCA assignments):


Advertisements

Java Program to show Overloading Constructors – Q21

January 11, 2009 Leave a comment

Q21: Java Program to show overloading constructors

Constructors are used to create instances of an object and can be overloaded by creating more than one Constructor in a class with same name but having different signatures.

class Emp_class{
	private String name;
	private int salary;
	
	public Emp_class(){
		name = "Manoj Pandey";
		salary = 10500;
	}
	
	public Emp_class(String n, int s){
		name = n;
		salary = s;
	}
	
	public Emp_class(Emp_class Oemp){
		name = Oemp.name;
		salary = Oemp.salary;
	}
	
	public void show(){
		System.out.println("\n Name: " + name);
		System.out.println(" Salary: " + salary);
	}
}

class ctor_ovld{
	public static void main(String args[]){
		Emp_class Oe1 = new Emp_class();
		Oe1.show();
		
		Emp_class Oe2 = new Emp_class("Nitin", 25050);
		Oe2.show();
		
		Emp_class Oe3 = new Emp_class("Bhuwanesh", 30050);
		Oe3.show();

		Emp_class Oe4 = Oe2;
		Oe4.show();
	}
}

… from College notes (BCA/MCA assignments):