Question 5

(b) Explain Generics in java with demo program.
class Gen<G>
{
G Obj;
Gen(G ObjI)
{
Obj = ObjI;
}
G getObj()
{
return Obj;
}
}
class GenericClassExampleH2E
{
public static void main(String args[])
{
Gen<Integer> ObjGI = new Gen<Integer>(125);
System.out.println("Value of Object : "+ObjGI.getObj());
Gen<String> ObjGS = new Gen<String>("Help2Engg");
System.out.println("Value of Object : "+ObjGS.getObj());
}
}
(b) Differentiate Method Overloading and Method Overriding with example.
class H2EA
{
int a,b;
H2EA(int i,int j)
{
a=i;
b=j;
}
void inside()
{
System.out.println("Inside H2EA");
}
int Multiplication()
{
return a*b;
}
}
class H2EB extends H2EA
{
int c;
H2EB(int i,int j,int k)
{
super(i,j);
c=k;
}
void inside()
{
System.out.println("Inside H2EB");
}
int Multiplication(int x)
{
return x*c;
}
}
class methodOverloadingAndOverridingDifferenceH2E
{
public static void main(String args[])
{
H2EB Obj = new H2EB(5,6,7);
Obj.inside(); // Method Overriding
System.out.println("Multiplication = "+Obj.Multiplication()); // Method Overloading
System.out.println("Multiplication = "+Obj.Multiplication(8)); // Method Overloading
}
}

Make Comments..!!


Oops!! No posts from user.

Download Android App