Java Techies - Solution for All
Java Techies - Solution for All
// 1. Code Reuse
class Account
{
int acno;
float bal;
void deposit()
{
System.out.println("Accout Class Deposit Call ");
}
void withdraw()
{
System.out.println("Accout Class W Draw Call ");
}
}
class SavingAc extends Account
{
void roi()
{
System.out.println("Saving Ac ROI Call ");
}
}
class CurrentAc extends Account
{
void roi()
{
System.out.println("Current Ac ROI Call ");
}
}
class TestBank
{
public static void main(String agrs[])
{
CurrentAc obj = new CurrentAc();
obj.deposit();
obj.withdraw();
obj.roi();
SavingAc obj1 = new SavingAc ();
obj1.deposit();
obj1.withdraw();
obj1.roi();
}
}
Output
Accout Class Deposit Call Accout Class W Draw Call Current Ac ROI Call Accout Class Deposit Call Accout Class W Draw Call Saving Ac ROI Call
class person
{
int age;
String name;
String address;
}
class Customer extends person
{
int custId;
int custno;
void customerInput()
{
name="shalabh";
age=20;
address="mumbai";
custno=02;
custId=1001;
}
void print()
{
System.out.println("customer name = "+name);
System.out.println("customer age = "+age);
System.out.println("customer address = "+address);
System.out.println("customer no = "+custno);
System.out.println("customer id = "+custId);
}
}
public class Test
{
public static void main(String s1[])
{
Customer customer=new Customer();
customer.input();
customer.print();
}
}
Output
customer name = shalabh customer age = 20 customer address = mumbai customer no = 2 customer id = 1001
class Loan
{
public void emi()
{
System.out.println("inside Loan emi...");
}
public void details()
{
System.out.println("inside details...");
}
}
class HomeLoan{
Loan loan=new Loan();
public void emi()
{
loan.emi();
}
}
public class Test {
public static void main(String[] args)
{
HomeLoan homeLoan=new HomeLoan();
homeLoan.emi();
}
}
Output
inside Loan emi...
class Ac
{
void deposit()
{
System.out.println("Account Class Deposit");
}
void withdraw()
{
System.out.println("Account Class Withdraw");
}
}
class HDFC1 extends Ac
{
void withdraw() // Overriding
{
System.out.println("HDFC Class Withdraw");
}
void roi()
{
System.out.println("HDFC ROI ");
}
}
class TestHDFC
{
public static void main(String args[])
{
HDFC1 obj = new HDFC1();
obj.deposit();
obj.withdraw();
obj.roi();
}
}
Output
Account Class Deposit HDFC Class Withdraw HDFC ROI
class A
{
int temp=10;
void show()
{
System.out.println("A Show");
}
}
class B extends A
{
int temp=20;
void show(){
super.show(); //Use of super to call parent class method
System.out.println("B Show");
this.temp=super.temp+this.temp;
}
}
class Test
{
public static void main(String args[])
{
B obj=new B();
obj.show();
}
}
Output
A Show B Show
class A
{
A()
{
System.out.println("A Class Default Constructor Call");
}
A(int a)
{
System.out.println("A class Parameterized Constructor call ");
}
}
class B extends A
{
B()
{
System.out.println("B Class Default Constructor Call ");
}
B(int t)
{
super(10); //Use of super to call parent class parameterized constructor and it must be the first line.
System.out.println("B Class Parameterized Constructor call");
}
}
class Demo
{
public static void main(String args[])
{
B b=new B();
A a=new A(10);
}
}
Output
A Class Default Constructor Call B Class Default Constructor Call A class Parameterized Constructor call
class Add
{
public int add(int x, int y)
{
return x + y;
}
// Overload the add() to add doubles instead of ints
public double add(double x, double y)
{
return x + y;
}
}
// From another class, invoke the add() method
public class Demo
{
public static void main (String [] args)
{
Add a = new Add();
int b = 27;
int c = 3;
int result = a.add(b,c); // Which add is invoked?
double doubleResult = a.add(22.5,9.3); // Which add?
System.out.println("result"+" "+result);
System.out.println("doubleResult"+" "+doubleResult);
}
}
Output
result 30 doubleResult 31.8
// UpCasting
// DownCasting
class S
{
int x = 100;
void disp()
{
System.out.println("Disp of S ");
}
}
class T extends S
{
int x = 200;
/*void disp()
{
System.out.println("Disp of T ");
}*/
void show()
{
System.out.println("Show of T ");
}
}
public class CastingDemo
{
public static void main(String[] args)
{
S obj = null; // obj is a Reference Variable of type S
//obj = new S();
obj = new T(); // Create T object and pass the Reference to the obj
//S obj = new T(); // Upcasting
System.out.println("Upcasting Result is ");
System.out.println("Value of X is "+obj.x);
obj.disp();
System.out.println("DownCasting Result is ");
T obj2 = (T)obj; // DownCasting
obj2.show();
obj2.disp();
}
}
Output
Upcasting Result is Value of X is 100 Disp of S DownCasting Result is Show of T Disp of S
public class Demo
{
public Demo()
{
System.out.println("In default constructor...");
}
public Demo(int i)
{
this();
System.out.println("In single parameter constructor...");
}
public Demo(int i,int j)
{
this(j);
System.out.println("In double parameter constructor...");
}
public static void main(String a[])
{
Demo ch = new Demo(10,20);
}
}
Output
In default constructor... In single parameter constructor... In double parameter constructor...


