Sie sind auf Seite 1von 15

Constructors

• When you create a new instance (a new


object) of a class using the new keyword,
a constructor for that class is called.
• Constructors are similar to methods, but
with some important differences.
• Constructor name is class name. A
constructors must have the same name as
the class its in.
• Default constructor. If you don't define a
constructor for a class, a default
parameterless constructor is automatically
created by the compiler. The default
constructor calls the default parent
constructor (super()) and initializes all
instance variables to default value (zero
for numeric types, null for object
references, and false for booleans).
• Default constructor is created only if there are no
constructors. If you define any constructor for your
class, no default constructor is automatically created.

Differences between methods and constructors.


• There is no return type given in a constructor signature
(header). The value is this object itself so there is no
need to indicate a return value.
• There is no return statement in the body of the
constructor.
• The first line of a constructor must either be a call on
another constructor in the same class (usingthis), or a
call on the superclass constructor (using super). If the
first line is neither of these, the compiler automatically
inserts a call to the parameterless super class
constructor.
• this(...) - Calls another constructor in same
class. Often a constructor with few parameters
will call a constructor with more parameters,
giving default values for the missing parameters.
Use this to call other constructors in the same
class.
• super(...). Use super to call a constructor in a
parent class. Calling the constructor for the
superclass must be the first statement in the
body of a constructor. If you are satisfied with
the default constructor in the superclass, there is
no need to make a call to it because it will be
supplied automatically.
Box class with Constructor
class Box
{
double width;
double height;
double depth;

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;

Box(double w,double h,double d) {


width=w;
heigth=h;
depth=d;
}

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
{}

Class Child extends Parent


{}
Method Overloading
class Parent
{
void calc(){
System.out.println("This is
Addition");
}

int calc(int x){


return x/2;
}

Void calc(int x, int y) {


System.out.println("This is
multiplying");
}
}
Method Overriding
class Parent
{
void calc(){
System.out.println("This is parent Addition");
}
}

class Child extends Parent {


void calc(){
System.out.println("This is child Addition");
}
public static void main(String a[])
{
Parent p=new Parent();
Child c=new Child();
p.calc();
c.calc();
}
}
Overloading Constructor
class Parent
{
parent(){
System.out.println("This is default
Costructor");
}
parent(int x){
System.out.println("Costructor
overloaded"+x);
}}

class Child extends Parent {

public static void main(String a[])


{
Parent p1=new Parent();
Parent p1=new Parent(10);
Child c1=new Child();
}
}
How to invoke parent default
Constructor by child class
class Parent
{
parent(){
System.out.println("This is parent Costructor");
}

parent(int x){
System.out.println("parent Costructor with args"+x);
}

class Child extends Parent {

Child(int y){
System.out.println("Child Costructor with args"+y);
}

public static void main(String a[])


{
Child c1=new Child(10);
}
}
How to use super() to invoke Constructor
class Parent
{
parent(){
System.out.println("This is parent Costructor");
}

parent(int x){
System.out.println("parent Costructor with args"+x);
}

class Child extends Parent {

Child(int y){
super(y);
System.out.println("Child Costructor with args"+y);
}

public static void main(String a[])


{
Child c1=new Child(10);
}
}
How to use this() to invoke Constructor

class Parent
{
parent(){
System.out.println("This is parent Costructor");
}

parent(int x){
this();
System.out.println("parent Costructor with args"+x);
}

class Child extends Parent {

public static void main(String a[])


{
Parent p1=new Parent(10);
}
}

Das könnte Ihnen auch gefallen