DOC PREVIEW
UMBC CMSC 331 - Advanced Object-Oriented Programming in Java

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11Some material adapted from Mary Hall’s Core Web ProgrammingCMSC 331Advanced Advanced ObjectObject--Oriented Oriented Programming Programming in Java in Java Introduction to Java2Agenda• Overloading• Designing “real” classes• Inheritance• Advanced topics– Abstract classes– Interfaces– Understanding polymorphism– Setting a CLASSPATH and using packages– Visibility modifiers – Creating on-line documentation using JavaDocAdvanced Object Oriented Programming3Example 4: Overloadingclass Ship4 {public double x=0.0, y=0.0, speed=1.0, direction=0.0;public String name;public Ship4(double x, double y,double speed, double direction,String name) {this.x = x;this.y = y;this.speed = speed;this.direction = direction;this.name = name;}public Ship4(String name) {this.name = name;}private double degreesToRadians(double degrees) {return(degrees * Math.PI / 180.0);}...Introduction to Java4Overloading (Continued)...public void move() {move(1);}public void move(int steps) {double angle = degreesToRadians(direction);x = x + (double)steps * speed * Math.cos(angle);y = y + (double)steps * speed * Math.sin(angle);}public void printLocation() {System.out.println(name + " is at ("+ x + "," + y + ").");}}2Introduction to Java5Overloading: Testing & Resultspublic class Test4 {public static void main(String[] args) {Ship4 s1 = new Ship4("Ship1"); Ship4 s2 = new Ship4(0.0, 0.0, 2.0, 135.0, "Ship2");s1.move();s2.move(3);s1.printLocation();s2.printLocation();}}• Compiling and Running:javac Test4.javajava Test4• Output:Ship1 is at (1,0).Ship2 is at (-4.24264,4.24264).Introduction to Java6Overloading: Major Points• Idea– Allows you to define more than one function or constructor with the same name• Overloaded functions or constructors must differ in the number or types of their arguments (or both), so that Java can always tell which one you mean• Simple examples:– Here are two square methods that differ only in the type of the argument; they would both be permitted inside the same class definition.// square(4) is 16public int square(int x) { return(x*x); }// square("four") is "four four"public String square(String s) { return(s + " " + s); }Introduction to Java7Example 5: OOP Design and Usage/** Ship example to demonstrate OOP in Java. */public class Ship {private double x=0.0, y=0.0, speed=1.0, direction=0.0;private String name;…/** Get current X location. */public double getX() {return(x);}/** Set current X location. */public void setX(double x) {this.x = x;}Introduction to Java8Example 5: Major Points• Encapsulation– Lets you change internal representation and data structures without users of your class changing their code– Lets you put constraints on values without users of your class changing their code– Lets you perform arbitrary side effects without users of your class changing their code• Comments and JavaDoc– See later slides (or book) for details3Introduction to Java9Example 6: Inheritancepublic class Speedboat extends Ship {private String color = "red";public Speedboat(String name) {super(name);setSpeed(20);}public Speedboat(double x, double y,double speed, double direction,String name, String color) {super(x, y, speed, direction, name);setColor(color);}public void printLocation() {System.out.print(getColor().toUpperCase() + " ");super.printLocation();}...}Introduction to Java10Inheritance Example: Testingpublic class SpeedboatTest {public static void main(String[] args) {Speedboat s1 = new Speedboat("Speedboat1");Speedboat s2 = new Speedboat(0.0, 0.0, 2.0, 135.0,"Speedboat2", "blue");Ship s3 = new Ship(0.0, 0.0, 2.0, 135.0, "Ship1");s1.move();s2.move();s3.move();s1.printLocation();s2.printLocation();s3.printLocation();}}Introduction to Java11Inheritance Example: Result• Compiling and Running:javac SpeedboatTest.java– The above calls javac on Speedboat.java and Ship.java automaticallyjava SpeedboatTest• OutputRED Speedboat1 is at (20,0).BLUE Speedboat2 is at (-1.41421,1.41421).Ship1 is at (-1.41421,1.41421).Introduction to Java12Example 6: Major Points• Format for defining subclasses• Using inherited methods• Using super(…) for inherited constructors– Only when the zero-arg constructor is not OK• Using super.someMethod(…) for inherited methods – Only when there is a name conflict4Introduction to Java13Inheritance• Syntax for defining subclassespublic class NewClass extends OldClass {...}• Nomenclature:– The existing class is called the superclass, base class or parent class– The new classis called the subclass, derived class or child class• Effect of inheritance– Subclasses automatically have all public fields and methods of the parent class– You don’t need any special syntax to access the inherited fields and methods; you use the exact same syntax as with locally defined fields or methods. – You can also add in fields or methods not available in the superclass• Java doesn’t support multiple inheritanceIntroduction to Java14Inherited constructors and super(...)• When you instantiate an object of a subclass, the system will automatically call the superclass constructor first– By default, the zero-argument superclass constructor is called unless a different constructor is specified– Access the constructor in the superclass through super(args)– If super(…) is used in a subclass constructor, then super(…)must be the first statement in the constructor• Constructor life-cycle– Each constructor has three phases:1. Invoke the constructor of the superclass2. Initialize all instance variables based on their initialization statements3. Execute the body of the constructorIntroduction to Java15Overridden methods and super.method(...)• When a class defines a method using the same name, return type, and arguments as a method in the superclass, then the class overrides the method in the superclass– Only non-static methods can be overridden• If there is a locally defined method and an inherited method that have the same name and take the same arguments, you can use the following to refer to the inherited method super.methodName(...)– Successive use of super (super.super.methodName) will not access overridden methods higher up in the hierarchy; super can only be used to invoke overridden methods from within the class that does the overridingIntroduction to Java16Advanced OOP Topics• Abstract classes• Interfaces• Polymorphism details• CLASSPATH• Packages• Visibility other than public or


View Full Document

UMBC CMSC 331 - Advanced Object-Oriented Programming in Java

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Advanced Object-Oriented Programming in Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Advanced Object-Oriented Programming in Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Advanced Object-Oriented Programming in Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?