?
Solved

Extended class constructors, how to use them?

Posted on 2003-03-03
2
Medium Priority
?
241 Views
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)
{
    super.Literature(somethingerether);
    publisher = pub;
    price = pr;
}

This is just something I'm curious about efficiency wise. Thanks in advance.
0
Comment
Question by:scudco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
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.
0
 

Author Comment

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

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 Month12 days, 5 hours left to enroll

752 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