Java Techies - Solution for All
Java Techies - Solution for All
class Test
{
int value1;
int value2;
Test()
{
value1 = 10;
value2 = 20;
System.out.println("Inside Constructor");
}
public void display()
{
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[])
{
Test d1 = new Test();
d1.display();
}
}
Output
Inside Constructor Value1 === 10 Value2 === 20
class Test
{
int value1;
int value2;
Test()
{
value1 = 10;
value2 = 20;
System.out.println("First Constructor");
}
Test(int a)
{
value1 = a;
System.out.println("Second Constructor");
}
Test(int a,int b)
{
value1 = a;
value2 = b;
System.out.println("Third Constructor");
}
public void display()
{
System.out.println("Value1 "+value1);
System.out.println("Value2 "+value2);
}
public static void main(String args[])
{
Test d1 = new Test();
Test d2 = new Test(30);
Test d3 = new Test(30,40);
d1.display();
d2.display();
d3.display();
}
}
Output
First Constructor Second Constructor Third Constructor Value1 10 Value2 20 Value1 30 Value2 0 Value1 30 Value2 40
class Test
{
int value1;
int value2;
Test()
{
value1 = 1;
value2 = 2;
System.out.println("First Parent Constructor");
}
Test(int a)
{
value1 = a;
System.out.println("Second Parent Constructor");
}
public void display()
{
System.out.println("Value1 "+value1);
System.out.println("Value2 "+value2);
}
public static void main(String args[])
{
TestChild d1 = new TestChild();
d1.display();
}
}
class TestChild extends Test{
int value3;
int value4;
TestChild()
{
//super(1);
value3 = 3;
value4 = 4;
System.out.println("Child Constructor");
}
public void display()
{
System.out.println("Value1 "+value1);
System.out.println("Value2 "+value2);
System.out.println("Value1 "+value3);
System.out.println("Value2 "+value4);
}
}
Output
First Parent Constructor Child Constructor Value1 1 Value2 2 Value1 3 Value2 4
class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void show(){
System.out.println("X="+this.x);
System.out.println("Y="+this.y);
System.out.println("width="+this.width);
System.out.println("height="+this.height);
}
}
public class Test{
public static void main(String args[]){
Rectangle obj=new Rectangle();
Rectangle obj1=new Rectangle(1,2);
Rectangle obj2=new Rectangle(1,2,3,4);
obj.show();
obj1.show();
obj2.show();
}
}
Output
X=0 Y=0 width=0 height=0 X=0 Y=0 width=1 height=2 X=1 Y=2 width=3 height=4



