Sunday 26 October 2014

Constructor in java

Java constructors are the methods which are used to initialize objects. Constructor has the same name as that of class, they are called or invoked when an object of class is created and can't be called explicitly. 

Example of Constructor:

public class A
{
 public A()  // constructor method
 {
 
 }
}

Types of Constructor 

1. Default Constructor.
2. Parameterized Constructor.

1. Default Constructor.

If Constructor method has no argument then its known as the "default constructor".If we do not create constructor then compiler automatically crate "default constructor"

class A
{
 public A()  
 {
     System.out.println("Default Constructor called");
 }
}

public MainClass
{
 public static void main(String[] args)
 {
  A a=new A(); 
 }
}


OUTPUT
Default Constructor called

2. Parameterized Constructor.

If we pass arguments in constructors' method then it called as "parameterized constructor."
class A
{
 public A()  
 {
     System.out.println("Default Constructor called");
 }
 
 public A(int i)  
 {
     System.out.println("The value of i="+i);
 }
}

public MainClass
{
 public static void main(String[] args)
 {
  A a=new A(10); 
 }
}

OUTPUT
The value of i=10


Note :
Constructor never return a value.

Constructor Overloading:

Like other methods in java constructor can be overloaded we can create as many constructors in our class as desired. When we create object and pass the arguments appropriate constructor is called.

class A
{
 public A()  
 {
     System.out.println("Default Constructor called");
 }
 
 public A(int i)  
 {
     System.out.println("The value of i="+i);
 }
 public A(String arg)  
 {
     System.out.println("String Argument is="+arg);
 }
}

public MainClass
{
 public static void main(String[] args)
 {
  A a1=new A();
  A a2=new A(10);
  A a3=new A("Hello");
 }
}

OUTPUT
Default Constructor called
The value of i=10
String Argument is=Hello

Constructor chaining:

constructor can not inherited into subclass.But we can call super class constructor from subclass by "super() key word".when subclass object created subclass constructor invoked and by super keyword we can invoke super class constructor this is called "constructor chaining".

class A
{
 public A()  
 {
     System.out.println("A Constructor called");
 }
 
 public A(String arg)  
 {
     System.out.println("String Argument is="+arg);
 }
}
class B extends Argument
{
 public B()
 {
  super("Hello");
   System.out.println("B Constructor called");
 }
 public B(int i)
 {
  super();
  System.out.println("Argument is="+i);
 }
}
public MainClass
{
 public static void main(String[] args)
 {
  A a1=new A();
  B b1=new B();
  B b2=new B(20);
 }
}

OUTPUT
A Constructor called
String Argument is=Hello
B Constructor called
A Constructor called
Argument is=20

Inheritance in java

00:11 Posted by Unknown No comments

Inheritance :

Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.
Using inheritance you can create general class that defines common set of related items.This class can be inherited by other classes.
In the terminology of java  , a class that is inherited is called "superclass" and the class that does inheriting called as "subclass".

Note : - 

In Java, only si.ngle inheritance is allowed and thus, every class can have at most one direct superclass. A class can be derived from another class that is derived from another class and so on. Finally, we must mention that each class in Java is implicitly a subclass of the Object class.

By "extends" keyword we can implement inheritance in java.

Example of inheritanc

class A
{
          //members of class A
}
class B extends A
{
          //members of class A inherited into B
           // members of class B
}

The private members of the superclass that cannot be accessed directly from the subclass. Also, constructors are not members, thus they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Simple example of inheritance

class Animal
{
     public Animal()
    {
               System.out.println("Animal created........");
    }
    public void sleep()
   {
         System.out.println("Animal sleep........");
    }
}
class Horse extends Animal
{
     public Horse()
    {
               System.out.println("Horse created........");
     }
      //Horse class inherits sleep() method from Animal class
}
public class MainClass
{
                 public static void main(String[] args)
                {
                    Animal animal=new Animal();   //Animal class Object created
                    Horse horse=new Horse();       // Horse class Object created
                   animal.sleep();
                    horse.sleep();   
                }
}

OUTPUT

Animal created........
Animal created........
Horse created........
Animal sleep........ Animal sleep........

The inheritance in Java provides the following features:

1.You can declare new fields in the subclass that are not in the superclass.
2.You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
3.You can declare new methods in the subclass that are not in the superclass.