Sunday, 11 September 2016

Difference between Method overloading and Method overloading

Q) what are the difference between method overloading and  method overriding?
Ans):-
1.       Method overloading is the process of extending existed method functionality up to some new functionality.
Method overriding is the process of providing replacements for existed method functionality with new functionality.
2.       If we want to overload method then all the methods must have different method signature, that is, all the method must have same name and different parameter list, here parameter list

Difference may be available is either of the following.
1.       Different in no parameters data type in both method.
2.       Different in parameter data type in both the method.
3.       Different in parameter data type order in both the methods.
If we want to override super class method with sub class method. then we must provide the same method prototype for both the method.

3.       To perform method overloading inheritance is not mandatory.
To perform method overriding, we must define inheritance relation between classes.
Consider following programming.
Ex code :-
class DB_Driver
{
                public void getDriver()
                {
                                System.out.println("Type-1 Driver");
                }
}
class DB_Driver_New extends DB_Driver
{
                public void getDriver()
                {
                                System.out.println("Type-4 Driver");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                DB_Driver driver=new DB_Driver_New();
                                driver.getDriver();
                }
}

Output:-   Type-4 Driver

In this above program we have to implemented method overriding, where if we all super class method then JVM will execute sub class method. Not super class method, in this context providing super class method implementation is unnecessary, it is suggestible to remove super class method body.
In java applications, if we want to declare any method without the implementation then we must declare that method as “abstract method’. If we want to declare any method as an abstract method then we must use “abstract” keyword.
If we want to declare any method as an abstract method then the respective class must be abstract class.
Ex code:-   
abstract class DB_Driver
{
                public void getDriver()
                {
                                System.out.println("Type-1 Driver");
                }
}
class DB_Driver_New extends DB_Driver
{
                public void getDriver()
                {
                                System.out.println("Type-4 Driver");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                DB_Driver driver=new DB_Driver_New();
                                driver.getDriver();
                }
}
Output:-  Type-4 Driver

In java applications, abstract method will provide more sharability when compiler with concrete methods.
In java application, abstract methods are possible in abstract classes and interfaces.
To declare abstract method we have to use “abstract” keyword.
Abstract class is a java class, it able to allow both concrete method and abstract methods.
In java applications, we are able to prepare abstract classes with zero number of concrete methods and with full of abstract methods, we are able to prepare abstract classes with 0 number of abstract method and with full of concrete methods.
Note:-     
To declare a method as an abstract method then the respective class must be abstract class, but to declare a class as an abstract class then it is not at all mandatory. Condition to have at least one method, we can declare abstract classes without abstract methods.
In java applications, if we declare an abstract class with abstract methods then it is conversion to implement all the abstract class provides abstract method in the respective sub class  
Ex code:-
abstract class A
{
                void m1()
                {
                                System.out.println("m1-A");
                }
abstract void m2();
abstract void m3();
}
class B extends A
{
                void m2()
                {
                                System.out.println("m2-B");
                }
                void m3()
                {
                                System.out.println("m3-B");
                }
                void m4()
                {
                                System.out.println("m4-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                //A a=new A();---->Error
                                A a=new B();
                                a.m1();
                                a.m2();
                                a.m3();
                  //a.m4();-->Error
                                B b=new B();
                                b.m1();
                                b.m2();
                                b.m3();
                                b.m4();
                }
}
Output:-
m1-A
m2-B
m3-B
m1-A
m2-B
m3-B
m4-B

In java applications, we are able to declare abstract classes without providing abstract methods.
Ex code :-

abstract class A
{
                void m1()
                {
                                System.out.println("m1-A");
                }
                void m2()
                {
                                System.out.println("m2-A");
                }
}
class B extends A
{

}
class Test
{
                public static void main(String[] args)
                {
                                //A a=new A();---->Error
                                A a=new B();
                                a.m1();
                                a.m2();
                }
}

Output:-
m1-A
m2-A
in abstract class, if we declare a set of abstract method then it is mandatory to provide implementation for all the abstract method in the respective sub class. In this context, if we provide implementation for only sub set of the abstract method, not for all the abstract methods then compiler will rise an error, we have to declare the respective sub class as an abstract class and we have provide implementation for the remaining abstract method by taking another sub class in multi-level inheritance.

Ex code:-
abstract class A
{
               
                abstract void m1();
                abstract void m2();
                abstract void m3();
}
abstract class B extends A
{
                void m1()
                {
                                System.out.println("m1-B");
                }
}
class C extends B
{
                void m2()
                {
                                System.out.println("m2-C");
                }
                void m3()
                {
                                System.out.println("m3-C");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new C();
                                a.m1();
                                a.m2();
                                a.m3();
                }
}

Output:-
m1-B
m2-C
m3-C

In java applications, it is possible to extends an abstract class to concrete class and it is possible to concrete class to abstract class
Ex code:-

class A
{
                void m1()
                {
                                System.out.println("m1-A");
                }
}
abstract class B extends A
{
                abstract void m2();
                abstract void m3();
}
class C extends B
{
                void m2()
                {
                                System.out.println("m2-C");
                }
                void m3()
                {
                                System.out.println("m3-C");
                }
                void m4()
                {
                                System.out.println("m4-C");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new C();
                                //a1.m1();--->Error
                                B b=new C();
                                b.m1();
                                b.m2();
                                b.m3();
                                C c=new C();
                                c.m1();
                                c.m2();
                                c.m3();
                                c.m4();
                }
}
Output:-
m1-A
m2-C
m3-C
m1-A
m2-C
m3-c
m4-C

In java applications, we are unable to create object for abstract class, but, we are able to provide constructor in abstract classes in order to recognize instance member of the abstract class, in order to store instance member in sub class object.
Ex code:-

abstract class A 
{
                A()
                {
                                System.out.println("A-Con");
                }
}
class B extends A
{
                B()
                {
                                System.out.println("B-Con");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                B b=new B();
                }
}
Output:-
A-Con
B-Con

Interface:-
Interface is a java feature, it able to allow only abstract methods.
In java applications, for interfaces, we are able to create reference variable, we are unable to create objects.
In case of interfaces, by default, all the variables are “public static final” no need to declare explicitly.
In case of interfaces, by default, all the methods are “public and abstract.
In java application, we are able to provide constructors in classes and abstract classes, but we are unable to   provide constructor inside interfaces.
When compare with classes and abstract classes, interfaces are able to provide more sharability.
In java application, we are declare any interface with abstract methods then it is conversion to provide implementation for all the interface method s by declaring an implementation class.

Ex code:-
interface I
{
                int x=10;
                void m1();
                void m2();
                void m3();
}
class A implements I
{
                public void m1()
                {
                                System.out.println("m1-A");
                }
                public void m2()
                {
                                System.out.println("m2-A");
                }
                public void m3()
                {
                                System.out.println("m3-A");
                }
                public void m4()
                {
                                System.out.println("m4-A");
                }
}
class Test
{
                public static void main(String[] args)
                {

                   System.out.println(I.x);
                   I i=new A();
                   i.m1();
                   i.m2();
                   i.m3();
                   //i.m4();--->Error

                   A a=new A();
                   System.out.println(A.x);
                   System.out.println(a.x);
                   a.m1();
                   a.m2();
                   a.m3();
                   a.m4();
   }
}

Output:-
10
m1-A
m2-A
m3-A
10
10
m1-A
m2-A
m3-A
m4-A
In java applications, if we declare any interface with a set of abstract methods then it is mandatory to implement all the interface methods in the respective implementation class, if we are not providing implementation for any method then compiler will rise an error, in this context,  to remove compilation error, we have to declare implementation class as an abstract class and we have to provide the remaining method implementation by taking a sub class to the implementation class.

Ex code:-
interface I
{
                void m1();
                void m2();
                void m3();
}
abstract class A implements I
{
                public void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                public void m2()
                {
                                System.out.println("m2-B");
                }
                public void m3()
                {
                                System.out.println("m3-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                   I i=new B();
                   i.m1();
                   i.m2();
                   i.m3();
   }
}
Output:-
m1-A
m2-B
m3-B

In java applications, we can implement more then one interface with in a single implementation class.
Ex code:-
interface I1
{
                void m1();
}
interface I2
{
                void m2();
}
interface I3
{
                void m3();
}
class A implements I1,I2,I3
{
                public void m1()
                {
                                System.out.println("m1-A");
                }
                public void m2()
                {
                                System.out.println("m2-A");
                }
                public void m3()
                {
                                System.out.println("m3-A");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                I1 i1=new A();
                                i1.m1();
                                I2 i2=new A();
                                i2.m2();
                                I3 i3=new A();
                                i3.m3();

                                A a=new A();
                                a.m1();
                                a.m2();
                                a.m3();
                }
}
Output:-
m1-A
m2-A
m3-A
m1-A
m2-A
m3-A

In java applications, it is not possible to extends more then one class to a single class, but, in the case of interfaces, we are able to extends more then one interface to a single interface.
Ex code:-

interface I1
{
                void m1();
}
interface I2
{
                void m2();
}
interface I3 extends I1,I2
{
                void m3();
}
class A implements I3
{
                public void m1()
                {
                                System.out.println("m1-A");
                }
                public void m2()
                {
                                System.out.println("m2-A");
                }
                public void m3()
                {
                                System.out.println("m3-A");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                I1 i1=new A();
                                i1.m1();
                                I2 i2=new A();
                                i2.m2();
                                I3 i3=new A();
                                i3.m3();

                                A a=new A();
                                a.m1();
                                a.m2();
                                a.m3();
                }
}

Output:-
m1-A
m2-A
m3-A
m1-A
m2-A
m3-A
Q)  Find the valid Syntax from the following list?
Ans):-
1.  class extends class --->Valid
2.  class extends class, class-->Invalid
3.  class extends abstract class-->Valid
4.  class extends abstract class, abstract class--->Invalid
5.  class extends class, abstract class--->Invalid
6.  class extends interface--->Invalid
7.  class implements class--->Invalid
8.  class implements abstract class--->Invalid
9.  class implements interface--->Valid
10. class implements interface, interface--->Valid
11. class implements interface, extends class--->Invalid
12. class implements interface extends abstract class-->Invalid
13. class extends class implements interface-->Valid
14. class extends abstract class implements interface--->Valid

15. abstract class extends class--->Valid
16. abstract class extends class, class---->Invalid
17. abstract class extends abstract class--->Valid
18. abstract class extends abstract class, abstract class--->Invalid
19. abstract class extends class, abstract class---->Invalid
20. abstract class extends interface--->Invalid
21. abstract class implements interface--->Valid
22. abstract class implements interface, interface--->Valid
23. abstract class implements interface extends class--->Invalid
24. abstract class implements interface extends abstract class--->Invalid
25. abstract class implements interface implements interface--->Invalid
26. abstract class extends class implements interface--->Valid
27. abstract class extends abstract class implements interface--->Valid
28. interface extends class-->Invalid
29. interface extends abstract class-->Invalid
30. interface extends inheritance-->Valid

31. interface extends interface, interface-->Valid

No comments:

Post a Comment