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 7 of 7

Thread: problem with inheritance

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with inheritance

    Hello, I have problem with inheritance , when I try to run the program , I get


    PHP Code:
    Enter the  frist  and last name of student
    dd
    dd
    dd
    dd dd 
    not the full name ?

    3 strings


    the main method

    PHP Code:
     public static void main(String[] args) {
          
    Scanner in = new Scanner (System.in);

           
    System.out.println("Enter the  frist  and last name of student" );
           
        
    String inf=in.nextLine();
           
    String infs=in.nextLine();
            
    String infss=in.nextLine();
     
    NewClass account= new NewClass(inf,infs,infss);
     
     

     
      
         
         
    System.out.print(account);
      
      
     
           
           
     } 
    // end main 




    the class method

    PHP Code:
    public class Account   {
     private 
    String name1;
    private 
    String name2;
     


    public  
    Account(String a1 ,String a2 ) {
     
    name1=a1;
    name2=a2



    }
     

    public 
    void setName1(String a1){

    name1=a1;

    }

    public 
    void setName2(String a2){

    name2=a2

    }


     


     

    public 
    String getName1(){

    return 
    name1;


    }
    public 
    String getName2(){

    return 
    name2;
    }



         public  
    String toString(){
         
         return 
    String.format("%-1s %s   ",name1name2);
         } 




    and the class inheritance method


    PHP Code:
    public class NewClass extends  Account {
        
        private 
    String name3;
        public 
    NewClass(String name1,String name2,String name33){
     
        
    super(name1,name2);
        
            
    getstring(name33);
        
        
        }
        
        
     

        
          
        public 
    void getstring(String name33){
        
        
    name3=name33;
        
        
        
        }
        
        public 
    String getString(){
        
        
        return 
    name3;
        
        }
          
        public 
    String forstring(){
        
        
        return 
    String.format(" %s  just to know if it works  ",super.toString(),name3 );
        } 



    what should I do to fix this problem ??


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem with inheritance

    Please show what the input and output for the program should look like.
    Explain what is wrong with its current output and show what it should be.

    How does anyone compile, and execute the program to test it?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member Mitch1337's Avatar
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with inheritance

    I haven't looked over the entirety of your code, as i am a bit pressed on time. What i have found though is that you did not override the toString() method of the Account class. So automatically, Java will move up to the parent. Since there is no toString in NewClass, the toString from Account is used, which doesn't display the information in NewClass.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem with inheritance

    NewClass@464486de
    That part of the printout is from the default toString() method for the NewClass method. You need to override the toString() method in the NewClass class and have it return the String you want to see.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with inheritance

    Quote Originally Posted by Norm View Post
    That part of the printout is from the default toString() method for the NewClass method. You need to override the toString() method in the NewClass class and have it return the String you want to see.
    It's my first time using it , so it don't know how to override it

    I watched
    Java Programming Tutorial - 49 - Inheritance - YouTube

    to understand more , but he said just copy the method that you want to override , and I tried it , but no use .

    So please how to do it ?

  6. #6
    Junior Member Mitch1337's Avatar
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with inheritance

    Do some more research on Overriding & Overloading methods in Java.

    Overriding is done when a subclass has the same method name & signature as the parent class, therefore replacing it. To call the parent's class you overwrote, you'd then need to do super.toString().

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with inheritance

    Quote Originally Posted by Mitch1337 View Post
    Do some more research on Overriding & Overloading methods in Java.

    Overriding is done when a subclass has the same method name & signature as the parent class, therefore replacing it. To call the parent's class you overwrote, you'd then need to do super.toString().
    I understood my problem . thank you very much , the program works fine now .

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 04:56 AM
  2. Inheritance; Problem with Test class
    By Charlie.beat in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 8th, 2012, 10:59 PM
  3. [SOLVED] Small problem regarding inheritance of classes
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 10th, 2011, 02:11 PM
  4. Problem with inheritance??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 06:13 AM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM