Beruflich Dokumente
Kultur Dokumente
Box() {
System.out.println("Costructing a Box");
width=3;
heigth=5;
depth=6;
}
double volume()
{
return width*height*depth;
}
}
The usage of the Constructor
class BoxTest {
public static void main(String arg[]) {
Box b1=new Box();
Box b2=new Box();
double vol;
vol=b1.volume();
System.out.println("Volume is:"+vol);
vol=b2.volume();
System.out.println("Volume is:"+vol);
}
}
parameterized Constructor
class Box
{
double width;
double height;
double depth;
double volume()
{
return width*height*depth;
}
}
How to use parameterized Constructor
class BoxTest {
public static void main(String arg[]) {
Box b1=new Box(2,5,9);
Box b2=new Box(7,10,12);
double vol;
vol=b1.volume();
System.out.println("Volume is:"+vol);
vol=b2.volume();
System.out.println("Volume is:"+vol);
}
}
Sub classing and Inheritance
Class Parent
{}
parent(int x){
System.out.println("parent Costructor with args"+x);
}
Child(int y){
System.out.println("Child Costructor with args"+y);
}
parent(int x){
System.out.println("parent Costructor with args"+x);
}
Child(int y){
super(y);
System.out.println("Child Costructor with args"+y);
}
class Parent
{
parent(){
System.out.println("This is parent Costructor");
}
parent(int x){
this();
System.out.println("parent Costructor with args"+x);
}