Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Research and respond to the following questions: 1-Drug class hierarchy in which

ID: 3678405 • Letter: R

Question

Research and respond to the following questions:

1-Drug class hierarchy in which several classes are derived from a single base class.

2-Explain the difference between overriding and overloading a member function of a base class in a derived class.

3-Explain how in a private inheritance, the members of the base class are inherited by a derived class.

4-Explain how in a protected inheritance, the members of the base class are inherited by a derived class.

5-Explain how in a public inheritance, the members of the base class are inherited by a derived class.

6-Explain the difference between the private and protected members of the class.

7-Explain the difference between the protected and public members of a class.

After completing your research for each question, use a Word document that includes each question number along with its response. Use a Word table with two columns, question # and your response (see table below).

Be sure to insert a cover page as well as including sources of information.

Correct grammar, spelling, and punctuation are expected.

Question # Researched Response

Explanation / Answer

2)Overloading vs. overriding.

- Overriding of functions occurs when one class is inherited from another class. Overloading can occur without inheritance.
- Overloaded functions must differ in function signature ie either number of parameters or type of parameters should differ. In overriding, function signatures must be same.
- Overridden functions are in different scopes; whereas overloaded functions are in same scope.
- Overriding is needed when derived class function has to do some added or different job than the base class function.
- Overloading is used to have same name functions which behave differently depending upon parameters passed to them.

6)difference between public, private, and protected

We use these keywords to specify access levels for member variables, or for member functions (methods).

Deciding when to use private, protected, or public variables is sometimes tricky. You need to think whether or not an external object (or program), actually needs direct access to the information. If you do want other objects to access internal data, but wish to control it, you would make it either private or protected, but provide functions which can manipulate the data in a controlled way.

Take the following example :

public class bank_balance

{

               public String owner;

               public int balance;

               public bank_balance( String name, int dollars )

               {

                               owner = name;

                               if (dollars >= 0)

                                              balance = dollars;

                               else

                                              dollars =0;

               }

}

We have declared our string and integer to be public. This means that any object in the system can change the balance (setting it to zero, or even giving us a negative balance). This could cause the program to fall over, even though we wrote code in our constructor to prevent negative balances.

Instead, we should have provided a getBalance/setBalance method, and made our balance private or proteced. Other objects can still access the data, but they can't put invalid data in.

public class bank_balance

{

               public String owner;

               private int balance;

               public bank_balance( String name, int dollars )

               {

                               owner = name;

                               if (dollars >= 0)

                                              balance = dollars;

                               else

                                              dollars =0;

               }

               public int getBalance()

               {

                               return balance;

               }

               public void setBalance(int dollars)

               {

                               if (dollars >= 0)

                                              balance = dollars;

                               else

                                              dollars = 0;                           

               }

}

7)Difference between the protected and public members of a class.

Public Access Modifier - public

A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.

However if the public class we are trying to access is in a different package, then the public class still need to be imported.

Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.

Example:

The following function uses public access control:

The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.

Protected Access Modifier - protected:

Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.

Example:

The following parent class uses protected access control, to allow its child class override openSpeaker() method:

Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intension is to expose this method to its subclass only, thats why we used protected modifier

Inheritence

When a subclass extends a superclass in Java, all protected and public fields and methods of the superclass are inherited by the subclass. By inherited is meant that these fields and methods are part of of the subclass, as if the subclass had declared them itself. protected and public fields can be called and referenced just like the methods declared directly in the subclass.

Fields and methods with default (package) access modifiers can be accessed by subclasses only if the subclass is located in the same package as the superclass. Private fields and methods of the superclass can never be referenced directly by subclasses. They can, however, be referenced indirectly via methods reachable from the subclass (e.g default (package), protected and public methods).

Constructors are not inherited by subclasses, but a subclass constructor must call a constructor in the superclass.

private inheritance is the default type of inheritance for classes but not for structs--ie if you omit the "public" keyword in the inheritance specification, you get private inheritance.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote