Saturday, March 21, 2020

Using the Keyword Final with Inheritance in Java

Using the Keyword Final with Inheritance in Java While one of Javas strengths is the concept of inheritance, in which one class can derive from another, sometimes its desirable to prevent inheritance by another class. To prevent inheritance, use the keyword final when creating the class. For example, if a class is likely to be used by other programmers, you may wish to prevent inheritance if any subclasses created could cause problems. A typical example is the String class. If we wanted to create a String subclass: public class MyString extends String{ï » ¿} We would be faced with this error: cannot inherit from final java.lang.String The designers of the String class realized that it was not a candidate for inheritance and have prevented it from being extended. Why Prevent Inheritance? The main reason to prevent inheritance is to make sure the way a class behaves is not corrupted by a subclass. Suppose we have a class Account and a subclass that extends it, OverdraftAccount. Class Account has a method getBalance(): public double getBalance(){ return this.balance; } At this point in our discussion, subclass OverdraftAccount has not overridden this method. (Note: For another discussion using this Account and OverdraftAccount classes, see how a subclass can be treated as a superclass). Lets create an instance each of the Account and OverdraftAccount classes: Account bobsAccount new Account(10); bobsAccount.depositMoney(50); OverdraftAccount jimsAccount new OverdraftAccount(15.05,500,0.05); jimsAccount.depositMoney(50); //create an array of Account objects //we can include jimsAccount because we //only want to treat it as an Account object Account[] accounts {bobsAccount, jimsAccount}; //for each account in the array, display the balance for (Account a:accounts) { System.out.printf(The balance is %.2f%n, a.getBalance()); } The output is: The balance is 60.00 The balance is 65.05 Everything appears to work as expected, here. But what if OverdraftAccount overrides the method getBalance()? There is nothing to prevent it from doing something like this: public class OverdraftAccount extends Account { private double overdraftLimit; private double overdraftFee; //the rest of the class definition is not included public double getBalance() { return 25.00; } } If the example code above is executed again, the output will be different because the getBalance() behavior in the OverdraftAccount class is called for jimsAccount: The output is: The balance is 60.00 The balance is 25.00 Unfortunately, the subclass OverdraftAccount will never provide the correct balance because we have corrupted the behavior of the Account class through inheritance. If you design a class to be used by other programmers, always consider the implications of any potential subclasses. This is the reason the String class cannot be extended. Its extremely important that programmers know that when they create a String object, its always going to behave like a String. How to Prevent Inheritance To stop a class from being extended, the class declaration must explicitly say it cannot be inherited. This is achieved by using the final keyword: public final class Account { } This means that the Account class cannot be a superclass, and the OverdraftAccount class can no longer be its subclass. Sometimes, you may wish to limit only certain behaviors of a superclass to avoid corruption by a subclass. For example, OverdraftAccount still could be a subclass of Account, but it should be prevented from overriding the getBalance() method. In this case use, the final keyword in the method declaration: public class Account { private double balance; //the rest of the class definition is not included public final double getBalance() { return this.balance; } } Notice how the final keyword is not used in the class definition. Subclasses of Account can be created, but they can no longer override the getBalance() method. Any code calling that method can be confident it will work as the original programmer intended.

Wednesday, March 4, 2020

What to Read in March

What to Read in March Not sure what to read this month? Try these suggestions based on   authors born in the month of March! Robert Lowell (March 1, 1917-September 12, 1977): Robert Traill Spence Lowell IV was an American poet who inspired the confessional style of other poets such as Sylvia Plath. He won the Pulitzer Prize for Poetry and was a United States Poet Laureate. His own personal history and his family and friendships were important subjects in his poetry. Recommended: Life Studies (1959). Ralph Ellison: (March 1, 1914- April 16, 1994): Ralph Waldo Ellison was an American literary critic, scholar, and novelist. He won the National Book Award in 1953 served on The American Academy of Arts and Letters. Recommended: Invisible Man (1952). Elizabeth Barrett Browning: (March 6, 1806- June 29, 1861): Elizabeth Barrett was an important English Romantic poet. Many do not know that Brownings family was part-Creole and spent much time in Jamaica, where they owned sugar plantations (kept by slave labor). Elizabeth herself was highly educated and was vehemently opposed to slavery. Her later works are dominated by political and social themes. She met and married the poet Robert Browning after a long epistolary relationship. Recommended: Poems (1844) Garbriel Garcà ­a Mrquez (March 6, 1928-April 17, 2014): Gabriel Josà © de la Concordia Garcà ­a Mrquez was a Colombian author of plays, short stories, and novels. He is considered one of the most important writers of the twentieth-century, having won the Nobel Prize in Literature in 1982. Garcia Marquez was also a journalist who criticized national and international politics, but he is best known for his fiction and magical realism. Recommended: One Hundred Years of Solitude (1967). Jack Kerouac: (March 12, 1922- October 21, 1969): Kerouac was a pioneering member of the 1950s Beat Generation. He originally went to college on a football scholarship, but upon moving to New York City he discovered Jazz and the Harlem scene, which would change his life, and the American literary landscape, forever.   Recommended: On the Road (1957). Louis L’Amour (March 22, 1908-June 10, 1988): Louis Dearborn grew up in North Dakota during the sunset years of the American frontier. His interactions with traveling cowboys, the great Northern Pacific Railroad, and the world of cattle ranching would shape his later fiction, as would the stories of his grandfather, who fought in civil and Indian wars. Recommended: The Daybreakers (1960). Flannery O’Connor (March 25, 1925-August 3, 1964): Mary Flannery  OConnor was an American writer. She flourished in the essay, short story and novel genres and was also a significant contributor to literary reviews and commentaries. Greatly inspired by her Roman Catholicism, her works often explored major themes of ethics and morality. She is one of the greatest Southern writers in American literature. Recommended: A Good Man Is Hard to Find (1955). Tennessee Williams: (March 26, 1911- February 25, 1983): Thomas Lanier Williams III is one of Americas greatest playwrights and an important presence in the history of homosexual writers. His works are heavily inspired by his own life, especially is unhappy family history. He had a great string of successful plays in the late 1940s, before shifting to a more experimental style which was not as well-received by audiences. Recommended: Suddenly, Last Summer (1958). Robert Frost: (March 26, 1874- Jauary 29, 1963): Robert Frost, perhaps Americas greatest and most successful poet, first explored a variety of careers, such as cobbler, editor, and teacher, before publishing his first poem (My Butterfly) in 1894. Frost spent some time living in England during the early 1900s, where he met such talents as Robert Graves and Ezra Pound. These experiences had a profound influence on his work. Recommended: North of Boston (1914). Anna Sewell (March 30, 1820- April 25, 1878): Anna Sewell is an English novelist, born into a Quaker family. When she was a girl, she severely injured both of her ankles, which confined her to crutches and limited walking for the rest of her life. Recommended: Black Beauty (1877). Other Notable Classic Writers Born in March: Theodore Seuss Geisel, Dr. Seuss (March 2, 1904-September 24, 1991)Tom Wolfe (March 2, 1931-  Ã‚  Ã‚   )Douglas Adams (March 11, 1952-May 11, 2001) Albert Einstein (March 14, 1879-April 18, 1955)John Updike (March 18, 1932-January 27, 2009)Philip Roth (March 19, 1933-  Ã‚  Ã‚  Ã‚   )Lawrence Ferlinghetti (March 24, 1919-  Ã‚  Ã‚  Ã‚   ) Gloria Steinem (March 25, 1934-  Ã‚  Ã‚  Ã‚   )John Fowles (March 31, 1926-November 5, 2005)