Thursday, 29 September 2016
Create Your Own Website and Earn Online Money Free Demo Class
Create Your Own Website and Earn Online Money
Free Demo classes
Address:
SNP AGRAHARAM, GBC ROAD, BAPATLA-522101
CELL NO:9553667049
EMAIL ID: tlvnsaikumar@gmail.com
Website: www.jntusuperfastupdates.com
Thursday, 15 September 2016
Sunday, 11 September 2016
Wrapper classes
Date: july-03-2016
Wrapper classes:-
Collection is an object, it able to store a group of other
objects.
In java applications, collection objects are not storing
primitive data directly., collection object is able to store the object
reference values.
If we want to store primitive data in collection object,
first, we have to convert data from primitive type to object type then we have
to store the generated object in collection object.
In the above context, if we want to get primitive data from
collection object, first, we have to get data in the form of object then, we
have to convert data from object type to primitive type.
Conclusion:-
In the above two situations, to convert data from primitive
type to object type and to convert data
from object type to primitive type, we have to use a set of predefined classes
called as “Wrapper classes”.
Java has provide the following list of wrapper classes with
respect to the 8 number of primitive data types.
|
Primitive
Data Types
|
Wrapper
classes
|
|
byte
|
java.lang.Byte
|
|
short
|
java.lang.Short
|
|
int
|
java.lang.Integer
|
|
long
|
java.lang.Long
|
|
float
|
java.lang.Float
|
|
double
|
java.lang.Double
|
|
char
|
java.lang.Character
|
|
boolean
|
java.lang.Boolean
|
Note:-
All wrapper class objects are immutable objects, they will
not allow modifications on their content, if we trying to perform modifications
over wrapper class object context then data is allowed for operations, but, the
respective data will not be stored back in original object, the resultant data
will be store by creating new object.
1.
Conversion from primitive data type to object
type:-
a.
By using parameterized constructors.
public XXX(xxx value)
xxx---->byte, short, int,.....
XXX---->Wrapper classes.
Ex :-
class Test
{
public static void
main(String[] args)
{
int i=10;
Integer
in=new Integer(i);
System.out.println(i+"
"+in);
}
}
Output:-
10 10
b.
By using valueOf() method:-
public static XXX valueOf(xxx val)
xxx--->byte,short,int,....
XXX---->Wrapper classes
Ex code:-
class Test
{
public static void
main(String[] args)
{
int i=10;
Integer
in=Integer.valueOf(i);
System.out.println(i+"
"+in);
}
}
Output:-
10 10
c.
By using Auto-Boxing mechanism:-
Auto-Boxing mechanism by JDK5.0 version, in this approach to convert data
from primitive type to object type no need to use any predefined method and
constructors. Simply, we have to assign
primitive variable to the respective wrapper class reference variable.
Ex code:-
class Test
{
public static void
main(String[] args)
{
int i=10;
Integer
in=i;
System.out.println(i+"
"+in);
}
}
Output:-
10 10
2.
Conversion from object type to Primitive
type:-
a.
By using xxxValue() method from wrapper
classes:-
public xxx xxxValue()
xxx--->byte,short,....
Ex code:-
class Test
{
public static void
main(String[] args)
{
Integer
in=new Integer(10);
int
i=in.intValue();
System.out.println
in+" "+i);
}
}
Output:-
10 10
b.
By using auto unboxing mechanism:-
To conversion data from object type to primitive type, if we use
auto-unboxing mechanism then it is not required to use any predefined library,
simply, we have to assign wrapper class reference variable to the respective
primitive variable.
Note:-
Auto-Unboxing feature was provided by JAVA along with its JDK 5.0
Version.
Ex code:-
class Test
{
public static void
main(String[] args)
{
Integer
in=new Integer(10);
int i=in;
System.out.println(in+"
"+i);
}
}
Output:-
10 10
3.
Conversion from String type to Object type:-
a.
By using String parameterized constructor:-
public XXX(String data)
XXX --->Wrapper classes.
Ex code:-
class Test
{
public static void
main(String[] args)
{
String
data="10";
Integer
in=new Integer(data);
System.out.println(data+"
"+in);
}
}
Output:-
10 10
b.
By using static valueOf(-)method:-
public static XXX valueOf(String data)
XXX --->Wrapper classes.
Ex code:-
class Test
{
public static void
main(String[] args)
{
String
data="10";
Integer
in=Integer.valueOf(data);
System.out.println(data+"
"+in);
}
}
Output:-
10 10
4.
Conversion from Object type to String type:-
a.
By using toString() method:-
public String toString()
Ex code:-
class Test
{
public static void
main(String[] args)
{
Integer
in=new Integer(10);
String
data=in.toString();
System.out.println(in+"
"+data);
}
}
Output:-
10 10
b.
By using ‘+’ concatenation operator:-
In java applications, if we concatenate any reference variable with “ ”
then JVM will access toString() method internally and JVM will appends the
return value of toString() to “”.
Ex code:-
class Test
{
public static void
main(String[] args)
{
Integer
in=new Integer(10);
String
data=""+in;
System.out.println(in+"
"+data);
}
}
Output:-
10 10
5.
Conversion from primitive type to String
type:-
a.
By using static toString() method from all
the Wrapper classes:-
public static String toString(xxx value)
xxx--->byte, short, int, ....
Ex code:-
class Test
{
public static void main(String[]
args)
{
int i=10;
String
data=Integer.toString(i);
System.out.println(i+"
"+data);
}
}
Output:-
10 10
b.
By using ‘+’ concatenation operator :-
In java applications, if we concatenate any primitive variable to “ “ by using ‘+’ operator then the primitive
variable data will be converted to String type.
Ex code:-
class Test
{
public static void
main(String[] args)
{
int i=10;
String
data=""+i;
System.out.println(i+"
"+data);
}
}
Output:-
10 10
6.
Conversion from String type to Primitive
type:-
a.
To convert data from String type to primitive
type then we have to use the following method.
public static xxx parseXXX(String data)
xxx--->byte, short, ....
Ex code:-
class Test
{
public static void
main(String[] args)
{
String data="10";
int
i=Integer.parseInt(data);
System.out.println(data+"
"+i);
}
}
Output:-
10
10
![]() |
Method Overriding
Method Overriding:-
It is the process of providing replacement for existed method functionality with new functionality.
To perform method overriding, we must provide inheritance, where we must provide same method prototype for both super class method and sub class method.
Procedure to perform method overriding:-
a. Declare a super class with a method, which we want to override.
b. Declare a sub class and declare a method with the same super class method prototype and with different implementation.
c. In main class, in main() method, create object for sub class and declare reference variable for super class.
d. Access super class method and get output from sub class method.
Note:-
In java application, when we create object for sub class when JVM will override super class method with sub class method, here to provide method overriding we must access only super class method, but JVM must execute sub class method as per overriding. To active this requirements only, we have to create object for sub class and we have to declare reference variable for super class.
Ex code 1:-
class A
{
void m1()
{
System.out.println("m1-B");
}
}
class B extends A
{
void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
/*
A a=new A();
a.m1();
status:- Method overriding required sub class object
*/
/*
B b=new B();
b.m1();
status: To provide method overriding, we must access super class method for this super class reference variable is required
*/
A a=new B();
a.m1();
}
}
Output:-
m1-B
Ex code 2:-
class Loan
{
public float getIR()
{
return 5.0f;
}
}
class Craft_Loan extends Loan
{
public float getIR()
{
return 3.0f;
}
}
class Study_Loan extends Loan
{
public float getIR()
{
return 12.5f;
}
}
class Gold_Loan extends Loan
{
}
class Test
{
public Static void main(String[] args)
{
Loan cl=new Craft_Loan();
System.out.println("Craft Loan IR:"+cl.getIR());
Loan sl=new Study_Loan();
System.out.println("Study Loan IR:"+sl.getIR());
Loan gl=new Gold_Loan();
System.out.println("Gold Loan IR :"+gl.getIR());
}
}
Output:-
Rules to perform method overriding:-
1. To override super class method with sub class method, super class method must not be declare as “private”.
Ex code:-
class A
{
private void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Status: - Compilation Error.
2. To override super class method with sub class method return type must be same as super class method return type otherwise compilation error will come.
Ex code:-
class A
{
void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
int m1()
{
System.out.println("m1-B");
return 10;
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Output:- Compilation Error.
Ex code 2:-
class A
{
int m1()
{
System.out.println("m1-A");
return 20;
}
}
class B extends A
{
int m1()
{
System.out.println("m1-B");
return 10;
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Status:- No Compilation Error.
Output:- m2-B
3. If we want to override super class method with sub class method then super class method must not be declared as “final” irrespective of sub class method “final”.
Ex code 1 :-
class A
{
final void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new A();
a.m1();
}
}
Output:- Compilation Error.
Ex code 2:-
class A
{
final void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
final void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m2();
}
}
Status:- Compilation Error.
Ex code 3:-
class A
{
void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
final void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Output:-
m1-B
4. In method overriding, either super class method or sub class method must not be declared as static, if we declare either super class method or sub class method as static then compiler will rise an error. If we declare both the method as static then compiler will not rise any Error, but JVM will execute super class method instead of executing sub class method, this process is not method overriding, this process in “method Hiding”.
Ex code 1:-
class A
{
static void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Status:- Compilation Error.
Ex code 2:-
class A
{
void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
static void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Status:- Compilation Error.
Ex code 3:-
class A
{
static void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
static void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Output:-
m1-A
5. To override the super class method with sub class method then sub class method scope must be same as super class method scope or at least more then super class method scope.
6. If we want to override super class method with sub class method then sub class method must have either some access privileges or weaker access privileges when compared with super class method access privileges.
Ex code 1:-
class A
{
public void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
protected void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Status:- Compilation Error.
Ex code2 :-
class A
{
protected void m1()
{
System.out.println("m1-A");
}
}
class B extends A
{
public void m1()
{
System.out.println("m1-B");
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
a.m1();
}
}
Status:- No Compilation Error.
Output:- m1-B
Subscribe to:
Posts (Atom)


