Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Extended class constructors, how to use them?

Posted on 2003-03-03
Medium Priority
Last Modified: 2010-03-31
I have to make a couple extended classes and in my main() the constructors will be called for these extended classes. If I have a subclass which assigns variables that could be assigned in the superclass, yet the subclass just has more variables it assigns then is there any shortcut to building the constructor in the subclass. Yeah that makes no sense, so in other words

    Book a = new Book("Big Java","Cay S. Horstmann", "Wiley", 59.95);

Book is a subclass of Literature which has a constructor which will set the title and author of the book. So the constructor for Book, would it need to look like

public Book(String t, String a, String pub, float pr)
    title = t;
    author = a;
    publisher = pub;
    price = pr;

or is there some shorter method for doing this so the constructor might look like

public Book(String pub, float pr)
    publisher = pub;
    price = pr;

This is just something I'm curious about efficiency wise. Thanks in advance.
Question by:scudco

Accepted Solution

functionpointer earned 80 total points
ID: 8062419
actually, if Literature's constructor looks like
public Literature( String a, String b )
then Book's will look like
public Book( string a, string b, String pub, float pr )
   super( a, b );
   publisher = pub;
   price = pr;

just gotta do it.

Author Comment

ID: 8062591
Thanks for the help... too bad there's no shortcut.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question