Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: copying subclass object

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default copying subclass object

    hi all ..
    I am trying to copy Subclass object to Super class ..

    which is in my code . from Bclass object to Aclass object.. but it is not copied .
    the program compiled successfully .

    but the value of Aclass object x is still 10 ..
    Any help ??
    and thanks in advance.


    class Aclass{
    int x;

    Aclass(){
    x =10;
    }

    Aclass(int i ){
    x =i;
    }


    Aclass ( Aclass object)
    {
    x = object.x;
    }
    }


    class Bclass extends Aclass {
    int x;

    Bclass ( int i )
    {

    x = i;
    }

    Bclass ( Bclass obj){
    super( obj);
    x= obj.x;
    }
    }


    public class refrencertst {
    public static void main ( String arg[]){
    Aclass Aobj = new Aclass(6);
    Bclass Bobj= new Bclass (5);

    Aclass Aobj2 = new Aclass (Bobj);

    System.out.print(" member is " + Aobj2.x);

    }
    }
    Last edited by spark; July 22nd, 2012 at 02:55 PM.


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: copying subclass object

    Solved ,,
    i put a constructor In Aclass with arguement of Bclass, to when it is called it will copy subclass object ..

    Aclass ( Bclass obj){
    x = obj.x;
    }

    Solved .

    but is it possible to do this ??

    Super class object = subclass object ?
    Last edited by spark; July 22nd, 2012 at 03:28 PM.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: copying subclass object

    So when you have a superclass extended by a subclass, the subclass is a type of the superclass. But this does not mean that you can save the superclass as a type of the subclass.
    Consider your sample...

    Class B extends Class A. Ok so far so good. Now all objects of class B are also objects of class A. Anywhere you expect to find a class A object in your code, you can throw in an object of class B.

    Now consider this...
    Class C also extends class A. An object of class C is also class A.

    Now compare B to C. Both B and C are of type A, but B and C may not be used for one another, unless the type you expect is type A. If your code wants a B object, you must provide a B object, and if your code requires a C object you must provide a C object.

    What you have done is create a superclass (A) dependant on a subclass (B). This limits the superclass to always include the subclass B and therefore really takes away the meaning of extending class B from A to begin with. That makes it seem to me that possible your B class should have been the super class instead. (Maybe... just guessing).
    If you want to provide some real class samples of what you are trying to do, I can explain with more details. Basically I am asking what purpose do you have in extending a class, and then using the extended class in the super class? Its like you cut something into two parts, just to glue them back together.

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: copying subclass object

    thanks for Explanation. and details ..

Similar Threads

  1. Copying Objects
    By MethodMan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2011, 03:41 AM
  2. Copying Arrays
    By AnnexTrunks in forum What's Wrong With My Code?
    Replies: 30
    Last Post: October 24th, 2011, 11:04 PM
  3. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 02:30 AM
  4. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 12:01 PM
  5. need help copying (what is in address bar)
    By 3ammary in forum Java Networking
    Replies: 0
    Last Post: January 8th, 2011, 09:32 AM