à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday, 8 October 2017

OOPS in JAVA 6


OOPS in JAVA 6

Constructors in Java

Constructor in java is a special type of method that is used to initialize the object. 

Java 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.

Rules for creating java constructor:

  1. Constructor name must be same as its class name.

  2. Constructor must not have any explicit return type.

  3. Constructrs can not be abstract, static, final or synchronized.

Example:

class Car
{
 String name ;
 String model;
 Car( )     //Constructor 
 {
  name ="";
  model="";
 }
}

Two types of Constructor:

  • Default constructor (no-arg constructor) : Default constructor provides the default values to the object like 0, null etc.
  • Parameterized constructor : Default constructor provides the default values to the object like 0, null etc.

Each time a new object is created at least one constructor will be invoked.

Car c = new Car() //Default constructor invoked
Car c = new Car(name); //Parameterized constructor invoked

NOTE : If there is no constructor in a class, compiler automatically creates a default constructor.


Constructors Overloading

Like methods, a constructor can also be overloaded. Overloaded constructors are differentiated on the basis of their type of parameters or number of parameters.

Why do we Overload constructors ?

Constuctor overloading is done to construct object in different ways.

Example:

class Cricketer 
{
 String name;
 String team;
 int age;
 Cricketer ()   //default constructor.
 {
  name ="";
  team ="";
  age = 0;
 }
 Cricketer(String n, String t, int a)   //constructor overloaded
 {
  name = n;
  team = t;
  age = a;
 }
 Cricketer (Cricketer ckt)     //constructor similar to copy constructor of c++ 
 {
  name = ckt.name;
  team = ckt.team;
  age = ckt.age;
 }
 public String toString() 
 {
  return "This is " + name + " of "+team;
 }
}

public class test
{
 public static void main (String[] args)
 {
  Cricketer c1 = new Cricketer();
  Cricketer c2 = new Cricketer("Sachin", "India", 43);
  Cricketer c3 = new Cricketer(c2 );
 
  c1.name = "Virat";
  c1.team= "India";
  c1.age = 27;

  System.out.println(c1);
  System.out.println(c2);
  System.out.println(c3);
 }
}
Output:
This is Virat of India
This is Sachin of India
This is Sachin of India

Difference between constructor and method in java

Constructor Method
 Constructor is used to initialize the state of an object. Method is used to expose behaviour of an object.
 Constructor must not have return type.  Method must have return type.
 Object is a physical entity. Class is a logical entity.
 Constructor is invoked implicitly. Method is invoked explicitly.
 Object is created many times as per requirement. Class is declared once.
 The java compiler provides a default constructor if you don't have any constructor. Method is not provided by compiler in any case.
 Constructor name must be same as the class name. Method name may or may not be same as class name.

 

Q. What is constructor chaining in Java??

NOTE : Constructor chaining is a phenomena of calling one constructor from another constructor of same class. Since constructor can only be called from another constructor in Java, constructor chaining is used for this purpose.

class Test
{
 Test()
 {
  this(10);
 }
 Test(int x)
 {
  System.out.println("x="+x);
 }
}
Output:
x=10

Q. Does constructor return any value??

Ans:Yes, Constructor return current class instance (You cannot use return type yet it returns a value). 


No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website