

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
it is about object oriented programming.
Typology: Exercises
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















n one of the major benefits of object-oriented programming (OOP) is this code sharing between classes through inheritance
n public class CheckingAccount extends BankAccount { n the objects of your class will now receive all of the state (fields) and behavior (methods) of the parent class n constructors and static methods/fields are not inherited n by default, a class's parent is Object
n other languages (C++) allow multiple inheritance
class BankAccount { private double myBal; public BankAccount() { myBal = 0; } public double getBalance() { return myBal; } } class CheckingAccount extends BankAccount { private double myInterest; public CheckingAccount(double interest) { } public double getInterest() { return myInterest; } public void applyInterest() { } } n CheckingAccount objects have myBal and myInterest fields, and getBalance(), getInterest(), and applyInterest() methods
n "Has-a" relationship: when one object contains another as a field public class BankAccountManager { private BankAccount myAccount; // ... } n a BankAccountManager object "has-a" BankAccount class object inside it, and therefore can use it
n "Is-a" relationships represent sets of abilities; implemented through interfaces and inheritance public class CheckingAccount extends BankAccount { // ... } n a CheckingAccount object "is-a" BankAccount n therefore, it can do anything an BankAccount can do n it can be substituted wherever a BankAccount is needed n a variable of type BankAccount may refer to a CheckingAccount object
Method Overriding Subclasses inherit all methods from their superclass Sometimes, the implementation of the method in the superclass does not provide the functionality required by the subclass. In these cases, the method must be overridden. To override a method, provide an implementation in the subclass. The method in the subclass MUST have the exact same signature as the method it is overriding.
n Child class can replace the behavior of its parent's methods by redefining them n you have already done this ... where? public class BankAccount { private double myBalance; // .... public String toString() { return “The balance in parent class" + getBalance(); } } public class FeeAccount extends BankAccount { private static final double FEE = 2.00; public String toString() { // overriding return “ The balance overwritten in child $"
Final Methods and Final Classes Methods can be qualified with the final modifier Final methods cannot be overridden. This can be useful for security purposes. public final boolean validatePassword(String username, String Password) { [...] Classes can be qualified with the final modifier The class cannot be extended public final class Color { [...]
n an industry-standard way to draw pictures of your classes and their relationships to each other n classes are boxes that list the fields, methods, and constructors of the type n classes that have inheritance relationships, or are tightly related to each other, are connected by arrows
n inheritance relationships n hierarchies drawn top-down with arrows from child to parent
n public: visible to all other classes public class BankAccount n private: visible only to the current class, its methods, and every instance (object) of its class n a child class cannot refer to its parent's private members! private String myID; n protected (this one's new to us): visible to the current class, and all of its child classes protected int myWidth; n package (default access; no modifier): visible to all classes in the current "package" (seen later) int myHeight;
n Person - > student, faculty… n Shape - > circle, triangle, rectangle… n Other examples???
Inheritance Abstract Classes An abstract class is one with at least one method that is abstract. It must be qualified as abstract. public abstract class Shape { private String name; public Shape (String shapeName) { name = shapeName; } public abstract double area( ); public abstract double perimeter( ); public String toString( ) { return name; } } Abstract methods – How does one find the area or perimeter of a shape? A class with abstract methods must be qualified abstract There can be no instances of abstract classes. An abstract class provides an interface that concrete classes (such as Circle and Rectangle in this example) can implement.