Explanation of Solution
Flawed method implementation:
Let us make the assumption that, in the code fragment 1.5 in the textbook the following instance variable “balance” is changed to private visibility.
//Create a main class
public class CreditCard
{
//Declare the instance variable
private double balance;
}
Program:
The following code is the subclass PredatoryCreditcard extended from CreditCard class. While changing the instance variable “balance” to private visibility in main class CreditCard, the following method leads to an error:
//Create a sub class PredatoryCreditCard
public class PredatoryCreditCard extends CreditCard
{
// Override the charge()method from superclass
public boolean charge(double price)
{
//Call inherited method
boolean isSuccess = super...
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Data Structures and Algorithms in Java
- SEE ATTACHED PHOTO FOR THE PROBLEMarrow_forwardGiven a Student class, and UndergradCourse is a true subtype of Course, there is one method called recommend in the Student class: public class Student { public Course recommend(Course pCourseID); } Please evaluate if EACH of the method in UndergradStudent class violates the Liskov Substitution Principle. public class UndergradStudent extends Student { 1. public Course recommend(UndergradCourse pCourseID); 2. public UndergradCourse recommend(Course pCourseID); 3. public UndergradCourse recommend(Object pCourseID); 4. public Course recommend(Course pCourseID) throw SomeCheckedException;arrow_forwardGiven class x{ static int a; int b=12; void m1(0{ } static void m2({ } class Ab extends x{ static int a; int b=10; void m10{ } static void m20{ } public static void main (String[] args){ int b=3; x obj=new Ab(); } How can you get access to the "m1" method in the child class O cast obj before getting access to it O use obj.m1() O you cannot get access to the "m1" in the child class O use the super keyword O Either A or Barrow_forward
- JAVAarrow_forward4arrow_forwardAssume the bank allows customers to open SavingAccount for which there are certain policies that shall be enforced when the account holder withdraw some amount. Here is the basic version of SavingAccount class. public class SavingAccount extends Account { private double minimumBalance = 10000; public SavingAccount(String title, double balance,boolean filer) { super(title, balance, filer); } @Override public void withdraw(double amount) { //... } } You are supposed to provide new definition of withdraw method that shall work as per following policies: The withdraw method shall proceed with transaction only if the remaining balance (after the transaction) do not goes below minimumAmount, as the bank want the saving account holder to maintain a minimum balance always. On each transaction, bank deduct 0.3% tax if the account holder if filer, if the account holder is not filer (not tax payer in the eyes of Government), the bank deduct 0.6% tax on…arrow_forward
- Create a class Animal Create a class Cat, and a class Dog, and a class Bearded Dragon which extend Animal. Add to your Animal class: Member: name Methods: Public Animal (String name) //constructor public void makes Sound () with the implementation printing out a generic animal sound. Next, override (add) the makes Sound() method to your Cat and Dog class, with the implementation specific to each animal (i.e, cat says purr..). Do not override the makesSound() for your Dragon (bearded dragons don't make sounds!) Note, you'll also need a constructor in each of your subclasses which calls super(name) to initialize the common 'name' member of Animal. Next (in your test harness) create a List of different Animals ( a couple cats, a dog, a dragon... ) and add these Animals to your list. Iterate through your list & call makeSound on each. (you should observe the makeSound method called will be: cat -> from Cat class, dog-> from Dog class, bearded Dragon -> from Animal class ) EC:…arrow_forwardCreate an interface Wheel with methods setWheels (int c) and getWheels () and a class Vehicle with abstract methods getColor () and getBrand (). Now, create a concrete class Taxi with additional method setDriver (String name) that inherits both Wheel and Vehicle. (Note: Concrete classes don’t have any abstract method)arrow_forwardplease asaparrow_forward
- In javaarrow_forwardMake a class Employee with a name and salary. Make a class Manager inherit from Employee. Add an instance variable, named department, of type string. Supply a method tostring that prints the manager's name, department, and salary. Make a class Executive inherit from Manager. Supply appropriate tostring methods for all classes. Supply a test program that tests these classes and methods.arrow_forwardJAVAarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education