Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are up-casting and down-casting in Java?
Typecasting is converting one data type to another.
Up-casting − Converting a subclass type to a superclass type is known as up casting.
Example
class Super {
void Sample() {
System.out.println("method of super class");
}
}
public class Sub extends Super {
void Sample() {
System.out.println("method of sub class");
}
public static void main(String args[]) {
Super obj =(Super) new Sub(); obj.Sample();
}
}
Down-casting − Converting a superclass type to a subclass type is known as downcasting.
Example
class Super {
void Sample() {
System.out.println("method of super class");
}
}
public class Sub extends Super {
void Sample() {
System.out.println("method of sub class");
}
public static void main(String args[]) {
Super obj = new Sub();
Sub sub = (Sub) obj; sub.Sample();
}
}
Advertisements