![]() I have found that Java developers often don't put a lot of consideration into making a class final or planning for it to be extensible, but this is an example of where such consideration is worthwhile. This obviously will only work for new classes that don't have classes extending them. Because the issue involves an "overridable" method and a child class that is not fully instantiated when that overridable method is invoked during parent's constructor, an obvious tactic is to make the parent class final so that it cannot be extended. Perhaps the best and easiest approaches are those recommended by the NetBeans hint that flagged this issue.Īs the screen snapshot above shows, NetBeans provides four easy and effective ways to deal with the issue of an overridable method called from the constructor of a class. There are multiple ways to avoid this problem. The child class does override the setSalaryRange() method, but this overridden implementation depends on an instance variable ( marketFactor) that is not yet initialized in the child instance when the parent's constructor calls this child class's overridden method. The reason the Computer Scientist's salary range is not correct is that the parent class's ( Employee's) constructor must first be run before the extending class ( ComputerScientist) is completely instantiated. However, that is not what is shown when the simple main application is executed (command line output and NetBeans output shown). One might expect the Computer Scientist, Fred Flintstone, to earn a weekly salary in the range of $1,000 to $60,000. Public static void main(final String arguments)įinal ComputerScientist cs = new ComputerScientist("Flintstone", "Fred", 5) įinal Employee emp = new Employee("Rubble", "Barney", JobTitle.CHIEF_EXECUTIVE_OFFICER) * the constructor of an extendible class is a bad idea. * Simple driver of the demonstration of why calling an overridable method in That is shown in the next simple executable class ( Main.java). This.maxWeeklySalary = MAX_CS_WEEKLY_SALARY_IN_DOLLARS * this.marketFactor įinally, a simple test driving application is required to run this example. This.minWeeklySalary = MIN_CS_WEEKLY_SALARY_IN_DOLLARS * this.marketFactor This.marketFactor = void setSalaryRange() Super(newLastName, newFirstName, JobTitle.COMPUTER_SCIENTIST) Private static final int MAX_CS_WEEKLY_SALARY_IN_DOLLARS = 60000 įinal String newLastName, final String newFirstName, final int newMarketFactor) Private final int MIN_CS_WEEKLY_SALARY_IN_DOLLARS = 1000 Public class ComputerScientist extends Employee * class's constructor leads to undesired behavior. * real purpose is to demonstrate how overriding a method called in the parent * Class representing a specific type of employee (computer scientist), but its That is shown next with the code listing for ComputerScientist, which extends Employee.ĬomputerScientist.java package To demonstrate a common problem associated with overridable methods called in a constructor, a child class is needed. NetBeans flags the existence of an overridable method called from a constructor as shown in the next screen snapshot ( NetBeans 7.3 in this case): + "' and with a salary range of $" + this.minWeeklySalary + " to $" Return this.firstName + " " + this.lastName + " with title '" This.displayableTitle = newDisplayableTitle įinal String newLastName, final String newFirstName, final JobTitle newJobTitle) JobTitle(final String newDisplayableTitle) ![]() * insidious dangers associated with a constructor calling an overridable method.ĬOMPUTER_SCIENTIST("Computer Scientist") ![]() The main purpose of this class is to demonstrate the * Simple employee class that is intended to be a parent of a specific type of The next class, Employee, is a contrived example of a class in which the extensible class's constructor calls an overridable method ( setSalaryRange()).Įmployee.java package In this post, I look at why having an overridable method called from a parent class's constructor is not a good idea. ![]() I wrote about the NetBeans hint "Overridable Method Call in Constructor" in the blog post Seven Indispensable NetBeans Java Hints.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |