What is upcasting in Java?


The following is an simple example to undersand upcasting.

class A{}  
class B extends A{}

As per the above scenario we can do the upcasting as follows.

A a=new B();//upcasting  [/java]


In the above example, reference variable of Parent class(A) refers to the object of Child class(B) so this is known as upcasting.

Another Example.

class Software{  
  void run(){System.out.println("running");}  
}  
class Facebook extends Software{  
  void run(){System.out.println("Having more than 100 million users");}  
  
  public static void main(String args[]){  
    Software b = new Facebook ();//upcasting  
    b.run();  
  }  
}  

Output:
Having more than 100 million users

No comments:

Post a Comment

What is Normalization?

Database normalization is a data design and organization process applied to data structures based on rules that help building relational dat...