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

Thread: Passing Arguments to Constructor using Inheritance

  1. #1
    Junior Member
    Join Date
    Mar 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing Arguments to Constructor using Inheritance

    I have a class "circle" which extends from class "shapes". When I try to call the "display" method using the keyword "super"
    return (super.display() + " Color: " + color);

    the value for size gets lost. I think it calls this constructor, shapes()
    How do I get the program to call this constructor, shapes(int size)

    Here's my code:

    class shapes {
    private int size;

    shapes(){}

    shapes(int size){
    this.size = size;
    }
    public String display() {
    return ("Size: " + size);
    }
    }



    class circle extends shapes {
    private String color;

    circle(String color){
    this.color = color;
    }

    public String display() {
    return (super.display() + " Color: " + color);
    }
    }



    public class Main {
    public static void main(String[] args) {
    shapes shapes1= new shapes(123);
    System.out.println(shapes1.display());

    circle circle1 = new circle("red");
    System.out.println(circle1.display());
    }
    }


    =====================

    Here's the output:
    Size: 123
    Size: 0 Color: red
    Last edited by locbtran; March 14th, 2023 at 08:20 PM.

  2. #2
    Junior Member
    Join Date
    Mar 2023
    Location
    Bengaluru
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Passing Arguments to Constructor using Inheritance

    It is working as expected, means whatever you have written it is working fine because you have created two objects shape1 and circle1, which is not related to each other, so you cant expect to get size has 123 in second sysout, as you are not passing any size related value while initializing, so it is giving default size

Similar Threads

  1. Passing an array to the constructor!
    By Mike8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2013, 12:21 PM
  2. [SOLVED] Inheritance constructor help?
    By EDale in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 14th, 2013, 11:04 AM
  3. Help with passing arrays as arguments
    By Jumbosize in forum Collections and Generics
    Replies: 4
    Last Post: November 12th, 2012, 08:02 PM
  4. passing arguments
    By Samaras in forum Java Theory & Questions
    Replies: 19
    Last Post: August 2nd, 2012, 05:47 AM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM

Tags for this Thread