Why do we need downcasting in Java? What is the use of it?

Why do we need downcasting in Java? What is the use of it?

here's an example of a valid downcast.
class Dog {}
class Dogma extends Dog {}
class Dogmatic extends Dog {}
Dog dogwood = new Dogma(); // a Dogma object is upcast to a Dog
Dogma bush = (Dogma) dogwood; // a Dogma object is downcast from a Dog
You cannot upcast a Dogmatic to a Dog and then downcast it to a Dogma. You also cannot downcast a Dog to a Dogma. In the working example, above, the dogwood references a Dogma, so you can downcast the Dogma.
and one more example
If you have a class hierarchy where class B extends class A (B is a specialization of A) you can use B in the place of A since B supports all the operations that B does. This is called polymorphism.
When you use an object of type B in place of A you actually do a up-cast, meaning you make the object of type B look like an object of type A (a less specific class type).
What you are asking about is down-casting which is making an object be of a more specific type, i.e. transforming an object of type A to type B. Is is only allowed if the object was actually created as a type B.
This is allowed (line 2 is a down-cast):
A a = new B();
B b = (B)a;

Similar Messages

  • Why do we need to import 'java.lang.annotation' ?

    Hi All,
    Since java.lang.* is the default package for all java file, Why do we need to import 'java.lang.annotation' separately?
    Thanks in advance..
    Regards
    Karthikeya V

    Karthikeya wrote:
    Hi All,
    Since java.lang.* is the default package for all java file, Why do we need to import 'java.lang.annotation' separately?
    Thanks in advance..
    Regards
    Karthikeya VLet me explain that differently: packages on disc usually form a directory structure (but it is not a rule, elements of the same package structure can come from different jars). BUT, this does not automatically mean that packages to Java form some kind of hierarchy as well - they don't. java.lang and java.lang.annotation are simply two completely different, unrelated packages.

  • What is the use of Serializable Interface in java?

    Hello friends,
    I have one dout and i want to share with u guys:
    Why Serializable interface made and actully
    what's the use of it?
    At which place it become useful ?
    It is not contain any method then why it is made?
    If anyone have any idea about this then please reply.
    Thanks in advace
    Regards,
    Jitendra Parekh

    t is not contain any method then why it is made?To point out to the user of a class (and the programs) that the design of this class is conforming to certain restraints needed for Serialization.

  • Why do we create indexes for DSOs and Cubes.What is the use of it?

    Hi All,
    Can you please tell me why are indexes created for DSOs and Cubes.
    What is the use with the creation of indexes.
    Thanks,
    Sravani

    HI ,
    An index is a copy of a database table that is reduced to certain fields. This copy is always in sorted form. Sorting provides faster access to the data records of the table, for example, when using a binary search. A table has a primary index and a secondary index. The primary index consists of the key fields of the table and is automatically created in the database along with the table. You can also create further indexes on a table in the Java Dictionary. These are called secondary indexes. This is necessary if the table is frequently accessed in a way that does not take advantage of the primary index. Different indexes for the same table are distinguished from one another by a separate index name. The index name must be unique. Whether or not an index is used to access a particular table, is decided by the database system optimizer. This means that an index might improve performance only with certain database systems. You specify if the index should be used on certain database systems in the index definition. Indexes for a table are created when the table is created (provided that the table is not excluded for the database system in the index definition). If the index fields represent the primary keys of the table, that is, if they already uniquely identify each record of the table, the index is referred to as an unique index.
    they are created on DSO and cube for the performance purpose ..and reports created on them wil be also more efficent ..
    Regards,
    shikha

  • What is the use of Generic class in java

    hi everyone,
    i want to know that
    what is the use of Generic class in java ?
    regards,
    dhruvang

    Simplistically...
    A method is a block of code that makes some Objects in the block of code abstract (those abstract Objects are the parameters of the method). This allows us to reuse the method passing in different Objects (arguments) each time.
    In a similar way, Generics allows us to take a Class and make some of the types in the class abstract. (These types are the type parameters of the class). This allows us to reuse the class, passing in different types each time we use it.
    We write type parameters (when we declare) and type arguments (when we use) inside < >.
    For example the List class has a Type Parameter which makes the type of the things in the list become abstract.
    A List<String> is a list of Strings, it has a method "void add(String)" and a method "String get(int)".
    A List<File> is a list of Files, it has a method "void add(File)" and a method "File get(int)".
    List is just one class (interface actually but don't worry about that), but we can specify different type arguments which means the methods use this abstract type rather than a fixed concrete type in their declarations.
    Why?
    You spend a little more effort describing your types (List<String> instead of just List), and as a benefit, you, and anyone else who reads your code, and the compiler (which also reads your code) know more accurately the types of things. Because more detail is known, the compiler is able to tell you when you screw up (as opposed to finding out at runtime). And people understand your code better.
    Once you get used to them, its a bit like the difference between black and white TV, and colour TV. When you see code that doesn't specify the type parameters, you just get the feeling that you are missing out on something. When I see an API with List as a return type or argument type, I think "List of what?". When I see List<String>, I know much more about that parameter or return type.
    Bruce

  • Why do we use open URL in default browser function? What are the uses of it?

    Why do we use "open URL in default browser" function?  What are the uses of it?

    kdm7 wrote:
    Okay.
    So can we keep a web button to access the www.ni.com ? So that web site opens only when button pressed?
    P.S  I,m a newbie.
    Yes, you can also, e.g. include a help file or manual as html and open that in the browser.
    /Y
    LabVIEW 8.2 - 2014
    "Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
    G# - Free award winning reference based OOP for LV

  • I want to change my account in the AppStore from USA to Saudi , but there are some cents 0.04 $ prevent change to Saudi and I do not need these cents So what is the solution ?

    I want to change my account in the AppStore from USA to Saudi , but there are some cents 0.04 $ prevent change to Saudi and I do not need these cents So what is the solution ?

    Click here and request assistance.
    (88185)

  • When I upgrade from OS10.5.8 to Snow Leopard will all my data transfer.  If I need to back up what is the best way to do this?

    I plan to upgrade from OS10.5.8 to Snow Leopard. Will all my data, applications and settings transfer automatically?
    If I need to back up what is the best way to do this?

    If everything works perfectly, your data should not be touched.
    Personally I never trust my data to everything working perfectly.
    I always backup everything twice at least.

  • Why can't I open and answer an instant imessage notification using just one key(such as enter key)? If I shouldn't, what's the use of answering right from notification?

    Why can't I open and answer an instant imessage notification using just one key(such as enter key)? If I shouldn't, what's the use of answering right from notification? I can instead, move my mouse to the app, right?

    Hi,
    The Alert option in Notifications gives the Reply option
    9:33 pm      Monday; December 8, 2014
    ​  iMac 2.5Ghz i5 2011 (Mavericks 10.9)
     G4/1GhzDual MDD (Leopard 10.5.8)
     MacBookPro 2Gb (Snow Leopard 10.6.8)
     Mac OS X (10.6.8),
     Couple of iPhones and an iPad

  • The Iphone 4 patch IOS 7.6 is suppose to be a security update but it freezes the phone and doesn't reset. Help I need my phone. What makes the phone go back to normal? There is nothing but a simulated plug in wire and an ITunes icon on a black screen.

    The Iphone 4 patch IOS 7.6 is suppose to be a security update but it freezes the phone and doesn't reset. Help I need my phone. What makes the phone go back to normal? There is nothing but a simulated plug in wire and an ITunes icon on a black screen.

    Hi SDPISMENOW,
    If you are having issues updating or restoring your iPhone after attempting the iOS 7.0.6 update, you may find the following article helpful:
    Apple Support: If you can't update or restore your iOS device
    http://support.apple.com/kb/ht1808
    Regards,
    - Brenden

  • I have a problem that my phone only vibrates but does NOT ring any more.  No matter what I try to do with settings turning off "silent" or whatever nothing helps.  Do I need to reset.  what are the consequences of resetting?

    I have a problem that my phone only vibrates but does NOT ring any more.  No matter what I try to do with settings turning off "silent" or whatever nothing helps.  Do I need to reset.  what are the consequences of resetting?

    What a numpty!  Yes of course you're right.
    Couldn't find the answer when searching the forum but then came accross this.
    Next dumb question coming up! :-(

  • I have 91 file that are pdfs i need to make searchable what is the best way of doing this

    help
    i have 91 file that are pdfs i need to make searchable what is the best way of doing this

    Hi ismaelr61128862,
    I would recommend you to try Advanced search option of Acrobat software, Go to Edit and then Advanced search.
    Hope that helps. In case you have any further questions please provide sufficient amount of details so that i can assist you more accurately.
    Regards,
    Rahul

  • I am switching accounts with my brother (long story why) but I need to remove my emails from the Mail application. I removed my Gmail account but when I try to remove my iCloud email it still keeps the emails. Does anyone know to remove a iCloud email?

    I am switching accounts with my brother (long story why) but I need to remove my emails from the Mail application. I removed my Gmail account but when I try to remove my iCloud email it still keeps the emails. Does anyone know to remove a iCloud email?

    Hi, how are you seeing these eMails if the account is removed?

  • Why class builder allows to develop abstract final class ? What is the use of such class in ABAP?

    I am new to ABAP. I tried creating abstract class and found that class builder allows development of abstract final class. What is the use of such class in ABAP?

    Hi,
    Does not compile:
    This one do:
    Inheritance:
    Regards.

  • What is the use of 'ALTER DATABASE CLOSE'?

    Hello Everybody,
    SQL*Plus: Release 10.1.0.2.0 - Production on Ma Aug 15 21:28:30 2005
    Copyright © 1982, 2004, Oracle. All rights reserved.
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
    With the Partitioning, OLAP and Data Mining options
    SQL> conn / as sysdba
    Connected.
    SQL> startup force
    ORACLE instance started.
    Total System Global Area 171966464 bytes
    Fixed Size 787988 bytes
    Variable Size 145750508 bytes
    Database Buffers 25165824 bytes
    Redo Buffers 262144 bytes
    Database mounted.
    Database opened.
    SQL> select status from v$instance;
    STATUS
    OPEN
    SQL> alter database close;
    Database altered.
    SQL> select status from v$instance;
    STATUS
    MOUNTED
    SQL> alter database open;
    alter database open
    ERROR at line 1:
    ORA-16196: database has been previously opened and closed
    If i have to shutdown the instance and database then
    WHAT IS THE USE OF 'ALTER DATABASE CLOSE' ?
    I need explanation. Why should i shutdown?
    Dont give me this answer.
    ORA-16196: database has been previously opened and closed
    Cause: The instance has already opened and closed the database, which is allowed only once in its lifetime.
    Action: Shut down the instance.

    If you read the documentation you will see that there is no ALTER DATABASE CLOSE command.
    This is an undocumented Oracle command, which we are not supposed to use. Consequently we are not allowed to complain about the way that it works. Instead we're supposed to use SHUTDOWN IMMEDIATE.
    Cheers, APC

Maybe you are looking for

  • How to represent multiple values in one column

    Hi, I need some help in designing a schema for this problem. I have : - 4 domains - In each domain there will be many groups - users belong to multiple groups in multiple domains Eg. user1 might be in groups g1, g2 and g3 in domains d1, d2 In a text

  • Html export with embedded video won't play on ipad

    Keynote 6.0 for Mac - when you export a keynote as html, if there is embedded video, the video will not play on an ios device.

  • How to pass dynamic params to Transaction iView from webdynpro java app

    Hi Experts, I am trying to call a transaction iview in PCD from my Dynpro application with a dynamic param. Can somebody help me generating the URL for transaction iView and passing the dynamic params??? Thanks in advance biroj...

  • Balanced Data Distributor in SSIS 2012

    I have been trying to get Balanced Data Distributor to install and show up in my toolbox in SSIS 2012 (version 11.0.31030.0 Q11REL) but so far I have not no success.  The versions dated 7/8/2012 fail to install but the 32 bit version dated 8/16/2012

  • Data points not visible on xy chart

    I am trying create an xy chart (a scatter plot) in staroffice (.sxc) using two sets of data: Data 1: -0.15 -0.1 0.04 0 -0.1 0.18 -0.05 0.08 0 Data 2: 5 50 80 70 80 80 50 90 80 After having selected my data, axis specifications, etc, when I click "cre