Login Register
Code2night
  • Home
  • Blog Archive
  • Tutorial
  • Interview Q&A
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. Upcast and Downcast in Java

Upcast and Downcast in Java

Date- Sep 03,2023

3812

java upcasting


Upcast means to create an object of the sub(child) class and access methods and variables of the child class using the child class object.

package Tutorial_01;

public class MainClass extends TestOne{

	int x = 50000;
	void TestResult()
	{
		System.out.println("Child Class Values...:"+x);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
			///-------------Upcast Example--------------------------------------
				MainClass z = new MainClass(); //  new object(child class object)
				z.TestResult();
				
		
	}

}

Downcast means to create an object of extend class (parent class) and access methods and variables of parent class using parent class object.

package Tutorial_01;

public class TestOne {
	int x = 1000;
	void TestResult()
	{
		System.out.println("Parent Class Values...:"+x);
	}

}

package Tutorial_01;

public class MainClass extends TestOne{

	int x = 50000;
	void TestResult()
	{
		System.out.println("Child Class Values...:"+x);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
////----------DownCast Example----------------------------------------
		TestOne y = new TestOne();  // new obj  (parent class object)
			y.TestResult();
		
	}

}

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Ultimate Guide to Method Overloading in Java with Examples
Dec 09, 2023
Parameterised constructor in java
Sep 09, 2023
Understanding Inheritance in Java: A Complete Guide with Examples
Dec 09, 2023
User-defined data types in java
Sep 05, 2023
Previous in Java
Static vs Non-static Keywords in Java: Everything You Need to Kno…
Next in Java
User-defined data types in java

Comments

Contents

๐ŸŽฏ

Interview Prep

Ace your Java interview with curated Q&As for all levels.

View Java Interview Q&As

More in Java

  • Master Java Type Casting: The Complete Guide with Examples 6176 views
  • How to Import java.util.List in Eclipse: Step-by-Step Guide 5787 views
  • org.openqa.selenium.SessionNotCreatedException: session not … 5738 views
  • java.lang.IllegalStateException: The driver executable does … 5063 views
  • Java Program to Display Fibonacci Series 4901 views
View all Java posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy