Discuss the importance of the protected access modifier. Provide examples of when to make parent class data fields private versus protected and how a derived class can access the constructor of its parent class. Consider how the parent/child relationship is enforced with the keyword and the importance of inheritance in application development. Also, discuss how multiple inheritance is commonly used in object-oriented programming languages.
Variables, methods(function) and constructors(class constructors) which are declared protected in the parent can only be accessed by the subclasses (or child classes) in other package or any class .The protected access modifier cannot be given to class and interfaces.
If the fields that are declared private ie. Variables, methods(function) and constructors(class constructors) in base class then they cannot be accessed by any derived or child class .
Example -
#include <bits/stdc++.h>
using namespace std;
// base class Parent
class Parent
{ //private data member
private:
int private_id;
// protected data members
protected:
int proctected_id;
Parent(){
cout<<"Parent constructor"<<endl;
}
};
// child class or sub class
class Child : public Parent
{
public:
void setId(int id)
{
//child class can access the protected_id of the parent class as is
is declared protectected
proctected_id = id;
//the below line which I have commented wont be executed because
Child class cannot access the private property of parent
class
//if you uncomment the below line and run the program it will give
you error that private memebers cannot be accessed
//private_id = id;
}
void displayId()
{
//it will print the id
cout << "proctected_id is:"
<< proctected_id << endl;
//the below line which I have commented wont be executed because
Child class cannot access the private property of parent
class
//if you uncomment the below line and run the program it will give
you error that private memebers cannot be accessed
//cout << "private_id is:" << private_id <<
endl;
}
};
// main function
int main() {
//this will call parent class contructor also
Child child1;
child1.setId(81);
child1.displayId();
return 0;
}
Multiple Inheritance is a feature of object-oriented programming languages where a class can inherit from more than one classes. All the object oriented language supports multiple inheritance except java.
Pls do give a like thanks .
Discuss the importance of the protected access modifier. Provide examples of when to make parent class...
JAVA: 1. What access modifier grants direct access to the super class members? 2. When a child class is created, when is the call to the parent class constructor made? 3. What is the keyword used to grant access to other classes that are in the same package?
Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...
1.Which of the following modifiers allow for inheritance? Make two selections. public protected portable private 2. Which one of the following is a class header indicating that we are defining a class named Amphibian, which is derived from the Animal class? Amphibian extends Animal Animal->Amphibian Amphibian : Animal Animal(Amphibian) 3. You have a parent class called Account, and a derived class called CheckingAccount. How would you go about initializing an instance of...
(1) ____ is the principle that allows you to apply your knowledge of a general category to more specific objects. a. Inheritance c. Encapsulation b. Polymorphism d. Override (2) When you create a class by making it inherit from another class, you are provided with data fields and ____ automatically. a. fonts c. class names b. methods d. arrays (3) By convention, a class diagram contains the ____ following each attribute or...
Question 11 The feature that enables you to split source code between two or more files is: Select one: a. base class b. partial classes c. generics d. dynamic link library e. package Question 12 Packaging data attributes and behaviors into a single unit so that the implementation details can be hidden describes an object-oriented feature called: Select one: a. abstraction b. objects c. inheritance d. polymorphism e. encapsulation Question 13 A multitier application would probably have:...
At face value this class seems ok ... but really it's not... select all that are true public class Student private Personal Info information; public Personal Info getInfo() return this.information; This is just a standard getter ... nothing wrong with that. Returning the Personalinfo reference can open this Student to direct manipulation of their data. A more robust public interface should be created to protect and control access to Personalinfo information. This is technically encapsulated properly, but could have very...
Y. Daniel Liang’s 8 Class Design Guidelines were posted on the File Manager and handed out in class. Please choose 5 guidelines and discuss them in depth. For each guideline, use 1 page or more for your discussion. You can use the code provided in class to demonstrate your points. The code should not be more than one-third of your writing. 1. Cohesion • [✓] A class should describe a single entity, and all the class operations should logically fit...
//*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee { protected final String name; protected final int id; public Employee(String empName, int empId) { name = empName; id = empId; } public Employee() { name = generatedNames[lastGeneratedId]; id = lastGeneratedId++; } public String getName() { return name; } public int getId() { return id; } //returns true if work was successful. otherwise returns false (crisis) public abstract boolean work(); public String toString() { return...
SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what choices does one make when creating a class that is an example of Abstraction? Encapsulation: What is encapsulation? What is information hiding? What does a public interface to a class consist of (not in the sense of actual java interfaces but what the client uses to manipulate objects of the class) What is an object of a class? What is the constructor? How do...
I need help for part B and C 1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...