Constructor

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Constructor

•Constructor having same name as class itself.


•Constructor initializes an object when it is
created.
•Constructor is invoked at the time of object
creation.
• It constructs the values i.e. provides data for the
object that is why it is known as constructor.
• constructors never return a value.
 simple example that uses a constructor:

// A simple constructor.
class MyClass
{
int x;
//Following is the constructor MyClass()
{
x = 10;
}
}
call constructor to initialize objects as follows:
public class ConsDemo
{
public static void main(String args[])
{
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.x + " " + t2.x);
}
}
• Types of constructor:

• 1.Default constructor.
• 2.Parameterized constructor.
 Default constructor:-
• A constructor that have no parameter is known as default
constructor.
• Default Constructor is also called as Empty Constructor which has
no arguments.
• Automatically called when we creates the object of class.
• Syntax of default constructor:
• <class_name>()
• { //constructor body }  
• class Bike1
• {  
• Bike1()
• {
• System.out.println("Bike is created");}  
• public static void main(String args[])
• {  
• Bike1 b=new Bike1();  
• }  
• }  
• Output:
• Bike is created
Rule: If there is no constructor in a class, compiler automatically
creates a default constructor.
• purpose of default constructor :
• Default constructor provides the default values to the object
like 0, null etc. depending on the type.
• class Student{  
• int id;  
• String name;  
•   void display(){System.out.println(id+" "+name);}  
•   public static void main(String args[]){  
• Student s1=new Student3();  
• Student s2=new Student3();  
• s1.display();  
• s2.display();  }  }  
• Output:
• 0 null
• 0 null
 Parameterized Constructor :-
• Constructor which has some Arguments.
• We have to create object of Class by passing some Arguments at
the time of creating object with the name of class.

class MyClass
{
int x;
MyClass() //Default
{ X=10;}
MyClass(int i ) // Parameterized
{
x = i;
}
}
Class Rectangle
{
int length;
int breath;
Rectangle()
{
length = 5;
breath = 6;
}
int area()
{
Int rectArea = length * breath;
Return rectArea;
}
}
Class Constructordemo
{
Public static void main(String args[])
{
  Rectangle firstRect = new
Rectangle();
System.out.println(“Area of
Rectangle = ”+ firstrect.area());
}
}
 

Output: Area of rectangle =30


class OverLoad
 {
    int age, num1,num2,num3,sum,e,b,c;
    String name;
    OverLoad(String n,int a) //constructor
    {
      name=n;
      age=a;
    }
    void disp()
     {
      System.out.println("Name of the person  :"+name);
      System.out.println("age  of the person  :"+age);
     }
   
  OverLoad(int e,int b,int c)
    {
     num1=e;
     num2=b;
     num3=c;
    
    }
   
     void show()
     {
       int sum=num1+num2+num3;
       System.out.println("sum  =:"+sum);
     }
 public static void main(String args[])
    {
       OverLoad ob=new OverLoad(“Manasi" ,19);

  ob.disp();
       OverLoad ab=new OverLoad(4,5,6);
       ab.show();
    }
 }
     
• class Student4{  
•     int id;  
•     String name;  
•       
•     Student4(int i,String n){  
•     id = i;  
•     name = n;  
•     }  
•     void display(){System.out.println(id+" "+name);}  
•    
•     public static void main(String args[]){  
•     Student4 s1 = new Student4(111,"Karan");  
•     Student4 s2 = new Student4(222,"Aryan");  
•     s1.display();  
•     s2.display();  
•    }  
• }  
• Test it NowOutput:
• 111 Karan 222 Aryan
Method Overloading:-
• Two methods with same name in one class with
different signature/parameter is called method overloading.

public  class MethodOverloading


{
        public  void add(int a, int b)

            System.out.println(a  + b);
    }
            public void add(String x,String y){
                System.out.println(x  + y);

        }
  public void add( char c, char d)
{
      System.out.println(c + d);
}

            public static void main(String [] args){


                Calculation c = new Calculation();

                   c.add(7,8);
                   c.add("he", "llo");
                   c.add('n','k');

    }

 }
Output
15
hello
217
• overloaded methods:-
• Method overloading appear in the same class.
• Method having the same name but, have
different parameter lists.
• can have different return types.
• Static variable:-

• It is a variable which belongs to the class and not to object(instance).

• Static variables are initialized only once , at the start of the execution


. These variables will be initialized first, before the initialization of any
instance variables.

• A single copy to be shared by all instances of the class.

• A static variable can be accessed directly by the class name and


doesn’t need any object.

• Syntax : <class-name>.<variable-name>
public class Svariables
{
private static int x = 0;
private int y = 3;
public SVariables()
{
x++;
y += 5;
System.out.println(y + " -- Welcome -- " + x);
}
public static void main(String args[])
{
SVariables s1 = new SVariables();
SVariables s1a = new SVariables();
SVariables s1b = new SVariables();
}
}
Output:-
8 -- Welcome – 1
8 -- Welcome -- 2
8 -- Welcome -- 3
• static method:-
• It is a method which belongs to the
class and not to the object(instance).
• A static method can access only static data. It can
not access non-static data (instance variables).
• A static method can call only other static
methods and can not call a non-static method
from it.
• A static method can be accessed directly by
the class name and doesn’t need any object.
• Syntax : <class-name>.<method-name>
• A static method cannot refer to “this” or “super”
keywords in anyway.

You might also like