Object Oriented Software Development
Object Oriented Software Development Naufal F. Setiawan School of - - PowerPoint PPT Presentation
Object Oriented Software Development Naufal F. Setiawan School of - - PowerPoint PPT Presentation
Object Oriented Software Development Object Oriented Software Development Naufal F. Setiawan School of Computing and Information Systems University of Melbourne Workshop 5 Object Oriented Software Development Keywords What does it mean for a
Object Oriented Software Development
Keywords
What does it mean for a variable, method, or class to be final? Answer
Object Oriented Software Development
final
src/AssignmentUI.java /** A button to submit assignments. */ public static final Button SUBMIT_BUTTON = new Button("Submit"); public void changeButtonText(String newText) { // will this line work? SUBMIT_BUTTON.setText(newText); }
Object Oriented Software Development
final
src/AssignmentUI.java /** A button to submit assignments. */ public static final Button SUBMIT_BUTTON = new Button("Submit"); public void changeButtonText(String newText) { // will this line work? SUBMIT_BUTTON.setText(newText); } It will! final only prevents reassignment!
Object Oriented Software Development
Keywords
Which other classes are able to access protected members of a class? Answer
Object Oriented Software Development
protected privacy modifier
Which other classes are able to access protected members of a class? Answer in Java: Classes in the same package will be able to access the member. Subclasses will also have access to the member (whether they are being in the same package or not.) in most languages: Only subclasses (direct and indirect) will have access to the member.
Object Oriented Software Development
protected privacy modifier
protected in protected in protected in protected in protected in protected in protected in protected in protected in protected in protected in protected in protected in protected in protected in protected in protected in
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
- ther languages
protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java protected in Java
Object Oriented Software Development
Inheritance
In terms of classes, what do we mean by inheritance? What relationship does inheritance represent? Answer
Object Oriented Software Development
Inheritance
In terms of classes, what do we mean by inheritance? What relationship does inheritance represent? Answer Inheritance represents an “is a” relationship between a parent and child class. (Superclass and subclass.) Every (instance) attributes and methods owned by the superclass is also owned by the subclass!
Object Oriented Software Development
Inheritance
class Instrument {} class Piano extends Instrument {} class ElectricPiano extends Piano {}
Object Oriented Software Development
Inheritance
src/Button.java public class Button { private String text; public void onClick() { // Do something here. } }
Object Oriented Software Development
Inheritance
src/DropdownButton.java public class DropdownButton extends Button { private ArrayList<MenuItem> items; public void onDropdownClick() { ... } }
Object Oriented Software Development
Inheritance
The use of “parent” and “child” often causes misconceptions. Folders FolderA
FolderA1 FolderA2
FolderB FolderA1 and FolderA2 are children of FolderA (but not in the inheritance sense). They are contained inside the other. It is wrong to say “FolderA1 is a FolderA”.
Object Oriented Software Development
Inheritance
What keyword is used to access methods and attributes of the superclass? Answer
Object Oriented Software Development
The superconstructor
String text; void click(); String text; void click();
Item[] menuItems; void dropClick();
Object Oriented Software Development
The superconstructor
Object Oriented Software Development
Inheritance
What do all classes inherit from? Answer
Object Oriented Software Development
Abstract Classes and Methods
What are abstract ... ? Answer classes: methods:
Object Oriented Software Development
Abstract Classes
When should a class be made abstract or concrete? Answer
Object Oriented Software Development
Abstract Classes
When should a class be made abstract or concrete? Answer It’s a design choice!
Object Oriented Software Development
Abstract Classes
The standard guideline: make it abstract if all the subclasses make up the superclass. Suppose: Code class User { ... } class Admin extends User { ... } class Merchant extends User { ... } class Customer extends User { ... } Should we make the class User abstract?
Object Oriented Software Development
Abstract Classes
The standard guideline: make it abstract if all the subclasses make up the superclass. Suppose: Code abstract class User { ... } class Admin extends User { ... } class Merchant extends User { ... } class Customer extends User { ... } Yes! We don’t want to instantiate any User that does not belong to any of the subclasses. Our system may not be able to handle them.
Object Oriented Software Development
Abstract Classes
Consider the other example, now the class Window can be left as a concrete class, but why? Code class Window { ... } class TerminalWindow extends Window { ... } class BrowserWindow extends Window { ... } class EditorWindow extends Window { ... }
Object Oriented Software Development
Polymorphism
Define what is meant by polymorphism Answer
Object Oriented Software Development