Type or paste question
Assignment Content
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:
here
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
Type or paste question Assignment Content One of the capabilities that makes object-oriented programming so powerful...