netoops blog
Showing posts with label JAVA QUESTION. Show all posts
Showing posts with label JAVA QUESTION. Show all posts

Sunday, 25 August 2013

What is the difference between constructors and normal methods?

Answer:
Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void.

What is the difference between java and c++?

  • Java is a true object - oriented language while c++ is basically c with object-oriented extension.
  • C++ supports multiple inheritence but Java provides interfaces in case of multiple inheritence.
  • Java does not support operator overloading.
  • Java does not have template classes as in c++.
  • java does not use pointers.

Saturday, 24 August 2013

What restrictions are placed on Method Overloading & Method Overriding?



Answer: Restriction on Method Overloading:
Restriction imposed on Method Overloading are
a) should done with in a same class.
b) it should have same name of overloading method.
c) its arguments or parameter should be different
irrespective of return type
Restriction on Method Overriding:
a) should done with its derived class
b)Overridden methods must have the same name, argument list, and return type.
b)The overriding method may not limit the access of the method it overrides.
c)The overriding method may not throw any exceptions that may not be thrown by the overridden method.

Explain the difference between Method Overloading & Method Overriding?



Answer: Difference Between Method Overloading and Method Overriding

Method Overloading
Method Overriding
Definition
In Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order.
In Method Overriding, sub class have the same method with same name and exactly the same number and type of parameters and same return type as a super class.
Meaning
Method Overloading means more than one method shares the same name in the class but having different signature.
Method Overriding means method of base class is re-defined in the derived class having same signature.
Behavior
Method Overloading is to “add” or “extend” more to method’s behavior.
Method Overriding is to “Change” existing behavior of method.

Overloading and Overriding is a kind of polymorphism.Polymorphism means “one name, many forms”.
Polymorphism
It is a compile time polymorphism.
It is a run time polymorphism.
Inheritance
It may or may not need inheritance in Method Overloading.
It always requires inheritance in Method Overriding.
Signature
In Method Overloading, methods must have different signature.
In Method Overriding, methods must have same signature.
Relationship of Methods
In Method Overloading, relationship is there between methods of same class.
In Method Overriding, relationship is there between methods of super class and sub class.
Criteria
In Method Overloading, methods have same name different signatures but in the same class.
In Method Overriding, methods have same name and same signature but in the different class.
No. of Classes
Method Overloading does not require more than one class for overloading.
Method Overriding requires at least two classes for overriding.
Example
Class Add
{
   int sum(int a, int b)
   {
     return a + b;
   }
  int sum(int a)
  {
    return a + 10;
   }
}
Class A  // Super Class
{
  void display(int num)
  {
     print num ;
   }
}
//Class B inherits Class A
Class B //Sub Class
{
  void display(int num)
  {
     print num ;
   }
}

Compare between constructor overloading vs method overloading.



Answer: Overloading in java

Overloading is defining more than one set of statement with same name but different signature. Overloading happens in the within same java class. The overloading can be applied on methods and constructor of the class.

Constructor overloading

As the constructor name should be as class name, Overloading can be apply by writing more than one constructor with different type of parameters and their different order.
For example:
public class Person {

    String name;
    int age;

    public Person() {
        // do somthing
    }

    public Person(String name, int age) {
        // do somthing
    }

    public Person(int age, String name) {
        // do somthing
    }

    public Person(int age) {
        // do somthing
    }

    public Person(String name) {
        // do somthing
    }
}
In the above example class Person has five different type of constructor with different parameters and their order.

Method overloading

Defining more than one method with the same name but different signature within same class is called method overloading in java. The method signature includes name, parameters and parameters orders. In method overloading the name should be same so to achieve the Method overloading we can define the methods with different parameters and different parameters order.
For Example:
public class Person {

    String name;
    int age;

    public void doStuff() {
        // do something
    }

    public void doStuff(String name) {
        // do something
    }

    public void doStuff(int age) {
        // do something
    }

    public void doStuff(String name, int age) {
        // do something
    }

    public void doStuff(int age, String name) {
        // do something
    }

}
In the above example of method overloading the doStuff() method of Person class is overloaded many time with different parameter and parameter order.

 
Subscribe For Free Updates!

We'll not spam mate! We promise.

Become Our Fan on Social Sites!