Q) What is generalization &
specialization?
A) Moving back from sub class to super class is
called generalization. Casting a sub class type into a super class is called a
up casting or widening.
Coming
down from super class to sub class is called specialization. Casting a super
class type into sub class type is called narrowing or down casting.
In
widening we can access super class methods. In widening we can access sub class
methods. In widening, we can not access sub class methods unless the override
super class methods.
class
Cast
{
public static void main(String args[])
{
// sub class reference to
refer to super class object
One o=new Two( );
Two t=(Two)o;
t.show1( );
t.show2( );
}
}
D:\prr\Core
java>javac Cast.java
D:\prr\Core
java>java Cast
Super
class
Sub
class
Narrowing
using super class object can not access either super class methods or sub class
methods.
Narrowing
using sub class object will make the programmer to access are the members of
super class as well as sub class.
Q) What is widening & narrowing?
A)
Widening: Casting a lower data
type into a higher data type is called widening. In widening no digits or
precision are lost. Casting a sub class type into a super class is called an up
casting or widening. In widening we can access super class methods. In widening
we can access sub class methods. In widening us can not access sub class
methods unless the override super class methods.
Narrowing: Converting a higher data
type into lower type is called narrowing. In narrowing precision or digits are
lost. This is called explicit casting. Casting a super class type into sub
class type is called narrowing or down casting. Narrowing using super class object
can not access either super class methods or sub class methods. Narrowing using
sub class object will make the programmer to access are the members of super
class as well as sub class.