Question

Type or paste question Assignment Content One of the capabilities that makes object-oriented programming so powerful...

Type or paste question

Assignment Content

  1. One of the capabilities that makes object-oriented programming so powerful is inheritance because it allows programmers to model real-world relationships.

    Answer the following two questions:

    1. What are the benefits of inheriting one or more derived classes from a base class? What are the drawbacks?
    2. The base class PatientRecord contains a variable X declared as private, a variable Y declared as protected, and a variable Z declared as public. The classes InsuredPatientRecord and UninsuredPatientRecord both derive from PatientRecord. Which of the base class's variables, if any, can the derived classes access? Why?

here

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Inheritance is also called is-A relationship. Below am listing some benefits and drawback of inheritance:-

Code Reuse: - It helps in code reuse.

Reduce Code Redundancy: It helps to reduce code redundancy in derived class by inheriting the properties of base class.

Minimize time and efforts: Save Time and efforts as base class code need not to be written again and again.

Clear Model Structure: Inheritance provides the clear model structure. In inheritance classes become grouped together in hierarchy structure where code is easy to manage and divided into parent and child classes.

DrawBacks:-

Tight coupling: -The biggest drawback of inheritance is base class and its drived classes are tightly coupled. If the functionality of base class change it will reflect on derived classes as well.

Java doesn't support multiple inheritance with classes. It will leads to diamond problem if we try multiple inheritance with java classes. So its a drawback of inheritance with classes

The classes InsuredPatientRecord and UninsuredPatientRecord both derive from PatientRecord. Which of the base class's variables, if any, can the derived classes access? Why?

With extends keyword we can inherit the properties of PatientRecord on InsuredPatientRecord and UninsuredPatientRecord. Now answer to your question it will inherit only protected and public variables. The reason behind its cannot access private variable is that scope/visibility of private variables is only in the class in which it is defined. If you will try to access the private variable of the base class from the derived class it will give compilation error.

Main.java:15: error: x has private access in PatientRecord
                System.out.println(ins.x);
                                      ^
1 error

For more clarification i giving a brief definition of public, private and protected. That will help you understand why it pront only protected and public variables.

Public: - This has the widest scope. classes, variables, methods that are declared public are accessible from every where in program.There is no restriction on the public data members.

Protected: - classes, variables, methods that are declared protected are accessible within same package and subclasses of different package.

Private: - Variables, methods, class members that are declared private can be accessible with in that class only.

Please check below code

public class Main
{
   public static void main(String[] args) {
       insuredPatientRecord ins=new insuredPatientRecord();
       System.out.println(ins.y);
       System.out.println(ins.z);
   }
}
class PatientRecord{
    protected int y=11;
    public int z=12;
}
class insuredPatientRecord extends PatientRecord{
  
}
class uninsuredPatientRecord extends PatientRecord{
  
}

Output: -

11

12

Add a comment
Know the answer?
Add Answer to:
Type or paste question Assignment Content One of the capabilities that makes object-oriented programming so powerful...
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
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