Question

Question 4 (10 points) How does Java handle constructors in inheritance? Please explain if there is any problem with the bloc
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In java, the constructor of base class with no argument gets automatically called in derived class constructor.

If we want to call the parameterized contructor of base class, then we can call it using super(parameter1,parameter2,....)

The base class constructor call must be the first line in derived class constructor.

The problem with the block of code given above is that the value for name is not initialised for the Student object.

For setting the value of name for student, the constructor must receive the name in its parameters and the base class constructor needs to be called using super.

The changed Student class would look like:

class Student extends People{
    private String studentID;

    public Student(String studentID,String name){
        super(name);
        this.studentID = studentID;
    }
}

Here we initialise the name value using super() and passing the value of name. This call to super() calls the base class constructor and initialises the value of name.

Add a comment
Know the answer?
Add Answer to:
Question 4 (10 points) How does Java handle constructors in inheritance? Please explain if there is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students...

    PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed....

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person...

    Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name, address, phone number, and email address (all Strings). A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable. An Employee Object has an office number, salary (both ints ), and a date hired. Use...

  • 2 Lab5 - Compatibility Mode - Saved to my Mac View Tell me jout References Mailings...

    2 Lab5 - Compatibility Mode - Saved to my Mac View Tell me jout References Mailings Review A Аа C 21 पा daBbCode AaBbCcDc AaBbCcDe AaBb CcD ABCDdF Emphasis Heading 1 Heading 2 Heading Heading ANA- Objectives: In this lab, the following topics will be covered 1. Inheritance in Java), 2. Overriding, and Hiding Inheritance (in Java): Inheritance is an important object-oriented concept that allows classes to be reused in order to define similar, but distinct, classes. In this lab...

  • 3 6 7 10 11 QUESTION 4: Object Orientation (a) Given the provided code: [08] 1...

    3 6 7 10 11 QUESTION 4: Object Orientation (a) Given the provided code: [08] 1 public class Base{ public void doSomething() {} /* Remainder of class omitted */ public class Derived extends Base{ public void do something() {} * Remainder of class omitted */ public class Container<t extends Base> { private T data; public void dort() { data.doSomething(); /* Remainder of Class omitted */ 16 public class Main { public static void main(String[] args) { Container<Derived> instance = new...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Question 1 1 pts Which of the following is not a valid class name in Java?...

    Question 1 1 pts Which of the following is not a valid class name in Java? O MyClass MyClass1 My_Class MyClass# Question 2 1 pts Which of the following statements is False about instance variables and methods? Instance variables are usually defined with private access modifier. Instance variables are defined inside instance methods. Instance methods are invoked on an object of the class that contains the methods. A class can have more than one instance variables and methods. Question 3...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT