Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 81

CSC240 Computer Science III

Zhen Jiang
Dept. of Computer Science
West Chester University
West Chester, PA 19383
zjiang@wcupa.edu

10/30/2019 1
Table of Contents
 Introduction to inheritance
 Inheritance
 Syntax
 More!
 Overridding
 OOP (object-oriented programming)
 Polymorphism
 Modifiers
 Abstract class and interfaces
 GUI
10/30/2019 2
Introduction
 Write an Employee class with methods that
return values for the following properties of
employees at a particular company:

 Work week: 40 hours


 Annual salary: $40,000
 Paid time off: 2 weeks
 Leave of absence form: Yellow form

10/30/2019 3
// A class to represent employees
public class Employee {
public int getHours() {
return 40; // works 40 hours / week
}

public double getSalary() {


return 40000.0; // $40,000.00 / year
}

public int getVacationDays() {


return 10; // 2 weeks' paid vacation
}

public String getVacationForm() {


return "yellow"; // use the yellow form
}
}
10/30/2019 4
 Write a Secretary class with methods that return
values for the following properties of secretaries at a
particular company:

 Work week: 40 hours


 Annual salary: $40,000
 Paid time off: 2 weeks
 Leave of absence form: Yellow form

 Add a method takeDictation that takes a string


as a parameter and prints out the string prefixed by
"Taking dictation of text: ".
10/30/2019 5
// A class to represent secretaries
public class Secretary {
public int getHours() {
return 40; // works 40 hours / week
}

public double getSalary() {


return 40000.0; // $40,000.00 / year
}

public int getVacationDays() {


return 10; // 2 weeks' paid vacation
}

public String getVacationForm() {


return "yellow"; // use the yellow form
}

public void takeDictation(String text) {


System.out.println("Taking dictation of text: " + text);
}
}
10/30/2019 6
// A class to represent employees // A class to represent secretaries
public class Employee { public class Secretary {
public int getHours() { public int getHours() {
return 40; return 40;
} }

public double getSalary() { public double getSalary() {


return 40000.0; return 40000.0;
} }

public int getVacationDays() { public int getVacationDays() {


return 10; return 10;
} }

public String getVacationForm() { public String getVacationForm() {


return "yellow"; return "yellow";
} }
}
public void takeDictation(String text) {
System.out.println("Taking dictation of text: "
+ text);
}
}

10/30/2019 7
Inheritance: Is-a relationship
 is-a relationship: A hierarchical connection where
one category can be treated as a specialized version
of another.

 Examples:
 Every secretary is an employee.
 Every square is a rectangle.
 Every dog is a mammal.

10/30/2019 8
 code reuse: The practice of writing program code once and using
it in many contexts.

 We'd like to be able to say the following:


// A class to represent secretaries
public class Secretary {
<copy all the contents from Employee class>

public void takeDictation(String text) {


System.out.println("Taking dictation of text: "
+ text);
}
}

 That way we would be reusing the Employee code.


10/30/2019 9
 inheritance: A way to specify a relationship
between two classes where one class inherits the
state and behavior of another.

 The child class (also called subclass) inherits from


the parent class (also called superclass).

 The subclass receives a copy of every field and


method from the superclass.

10/30/2019 10
Syntax
 Creating a subclass, general syntax, p625:
public class <subclass name> extends <superclass name>

 Example:
public class Secretary extends Employee
{
....
}

 By extending Employee, each Secretary object


automatically has a getHours, getSalary,
getVacationDays, and getVacationForm method.
10/30/2019 11
 Improved Secretary class:

// A class to represent secretaries


public class Secretary extends Employee
{
public void takeDictation(String text) {
System.out.println("Taking dictation of text: "
+ text);
}
}

10/30/2019 12
Writing even more classes
 Write a Marketer class that represents marketers
who have the same properties as general employees,
but instead of making only a paltry $40,000,
marketers make $50,000!

 Can we still leverage the Employee class or do we


have to re-write everything, because one method
(getSalary) is different?

 If only Marketer could write a new version of the


getSalary method, but inherit everything else…

10/30/2019 13
Overriding methods
 override: To write a new version of a method in a
subclass to replace the superclass's version.

 To override a superclass method, just write a new


version of it in the subclass. This will replace the
inherited version.

10/30/2019 14
Marketer class
// A class to represent marketers
public class Marketer extends Employee {
public void advertise() {
System.out.println("Act now while supplies last!");
}

public double getSalary() {


return 50000.0; // $50,000.00 / year
}
}

10/30/2019 15
OOP: Based in reality or too
convenient?
 At many companies, all new employees attend a
common orientation to learn general rules (e.g., what
forms to fill out when).

 Each person receives a big manual of these rules.

 Each employee also attends a subdivision-specific


orientation to learn rules specific to their subdivision
(e.g., marketing department).

 Everyone receives a smaller manual of these rules.


10/30/2019 16
 The smaller manual adds some rules and also changes
(read: overrides) some rules from the large manual
(e.g., "use the pink form instead of the yellow form")

10/30/2019 17
 Why not just have a 22-page manual for lawyers,
21-page manual for secretaries, 23-page manual for
marketers, etc…?

10/30/2019 18
 maintenance: If a common rule changes, only the
common manual needs to be updated.

10/30/2019 19
 The 20 page manual manner is useful to be able to
specify general rules that will apply to many groups.

 Locality
 A person can look at the manual for lawyers and
quickly discover all rules that are specific to lawyers.

 It is also useful to specify a smaller set of rules for


such a particular group, including being able to
replace rules from the overall set (overriding, e.g.,
"use the pink form instead of the yellow form").
10/30/2019 20
//base class //attribute & method inheritance
public class PubA { public class PubB extends PubA
public int x = 1; {
public void setX(){ public void setX(){
x=2; x=3;}
} }
public int getX(){
return x;
}
}

PubA a = new PubA();


PubB b = new PubB();
a.setX();
b.setX();
System.out.println(a.getX());
10/30/2019 System.out.println(b.getX()); 21
Why inheritance?
 Need for Jbutton, FlowLayout, JTextField, etc
 http://www.cs.wcupa.edu/~zjiang/Extend_action.java
 Need for a customized response from the computer for
all GUI actions
 http://www.cs.wcupa.edu/~zjiang/MultiEventSource.java

10/30/2019 22
Constructor for superclass

10/30/2019 23
public class Employee {
private double salary;
public Employee(double initialSalary)
{
salary = initialSalary;
}
public int getHours() {
return 40; // 40 hours per week
}
public double getSalary() {
return salary;
}
public int getVacationDays() {
return 10; // 2 weeks' paid vacation
}
public String getVacationForm() {
return "yellow"; // use the yellow form
}
}
10/30/2019 24
Use the super() method to call the superclass’s constructor, p634
public class Marketer extends Employee {
// inherits double salary

public Marketer(double initialSalary)


{
//construct superclass
super(initialSalary);
}
}

- For every constructor of a subclass, the call to super() must be the


first statement in the subclass’s constructor.
- Make sure to give the same number of arguments as there are
parameters in the definition of the superclass’s constructor.

10/30/2019 25
 Question: If a method is declared private, does a
subclass inherit it?
 Actually, yes. Subclasses inherit everything that they
don’t override.
 If a method is declared private, can a subclass
call it?
 NO! Only code inside the same class can call a private
method.
 What if you want a subclass to be able to use it?
 Use the protected access level
10/30/2019 26
public class Employee {
private double salary = 40000.00;

public int getHours() {


return 40; // works 40 hours / week
}

public double getSalary() {


return salary;
}

public int getVacationDays() {


return 10; // 2 weeks' paid vacation
}

public String getVacationForm() {


return "yellow"; // use the yellow form
}
}
10/30/2019 27
 Subclasses cannot see salary directly!
public class CEO extends Employee {

public void giveMyselfRaise() {

salary += 1000000.00; // Compile-time Error!

public static void main(String [] args)


{
CEO c = new CEO();
// This is fine, no error here
// Access to salary field is indirect
// We’re accessing the public getSalary() method
System.out.println(“My salary is “ + c.getSalary());
}
}
10/30/2019 28
public class Employee {
protected double salary = 40000.00;

public int getHours() {


return 40; // works 40 hours / week
}

public double getSalary() {


return salary;
}

public int getVacationDays() {


return 10; // 2 weeks' paid vacation
}

public String getVacationForm() {


return "yellow"; // use the yellow form
}
}
10/30/2019 29
 Subclasses can see protected variables
and methods just fine.
public class CEO extends Employee {
public void giveMyselfRaise() {
salary += 1000000.00; // No longer an error
}

public static void main(String [] args)


{
CEO c = new CEO();
// This is fine, no error here
// Access to salary field is indirect
// We’re accessing the public getSalary() method
System.out.println(“My salary is “ + c.getSalary());
}
}
10/30/2019 30
What would happen if ....
public class Employee {
private double salary = 40000.00;

public int getHours() {


return 40; // works 40 hours / week
}

public double getSalary() {


return salary;
}

public void addToSalary(double raise) {


salary += raise;
}

public int getVacationDays() {


return 10; // 2 weeks' paid vacation
}

public String getVacationForm() {


return "yellow"; // use the yellow form
}
}
10/30/2019 31
public class CEO extends Employee {

public void giveMyselfRaise() {

addToSalary(1000000.00);

}
}

 CEO still has its own copy of the salary


field, and this code will change the value of
it appropriately.
 The fact that salary is private simply means
that CEO can't access it directly. It can still
call public (or protected) superclass
methods that can access it.

10/30/2019 32
//base class //sub class
public class ProA { public class ProB extends ProA {
private int x = 1; public int getB(){
protected void setX(int a){ setX(2);
x=a; // your next step is to return x
} // but “return x” does not work
// due to the private modifier, so
protected int getX(){ return getX();
return x; }
} }
}

ProA a = new ProA();


ProB b = new ProB();
System.out.println(a.getX());
System.out.println(b.getB());
10/30/2019 33
Polymorphism
 A reference variable of type T can refer to an object
of any subclass of T.
Employee Laura = new Lawyer();
Employee Mark = new Marketer();

 polymorphism: The ability for the same code to be


used with several different types of objects and
behave differently depending on the type of object
used, p664.

10/30/2019 34
Employee Laura = new Lawyer();
System.out.println(Laura.getSalary()); // 40000.0
System.out.println(Laura.getVacationForm()); // "pink"

 You can call any method from Employee on the


person variable, but not any method specific to
Lawyer (such as sue).

 Once a method is called on that object, it behaves in


its normal, overridden way (as a Lawyer, not as a
normal Employee).

10/30/2019 35
public class Secretary extends Employee public class Marketer extends Employee {
{ public void advertise() {
public void takeDictation(String text) { System.out.println("Act now while supplies last!");
System.out.println("Taking dictation of text: " }
+ text);
} public double getSalary() {
} return 50000.0; // $50,000.00 / year
}
}

public class EmployeeMain {


public static void main(String[] args) {
Secretary laura = new Secretary();
Marketer mark = new Marketer(); Polymorphism
printInfo(laura);
printInfo(mark); and
}
parameters
public static void printInfo(Employee empl) {
System.out.println("salary = " + empl.getSalary());
System.out.println("days = " + empl.getVacationDays());
System.out.println("form = " + empl.getVacationForm());
System.out.println();
}
}
Output:
salary = 40000.0
vacation days = 10
vacation form = yellow
salary = 50000.0
vacation days = 10
vacation form = yellow
10/30/2019 36
public class EmployeeMain2 {
public static void main(String[] args) {
Employee[] employees = {new Secretary(), new Marketer() };
for (int i = 0; i < employees.length; i++) {
System.out.println("salary = " + employees[i].getSalary());
System.out.println("vacation days = " +
employees[i].getVacationDays());
System.out.println();
}
}
}
Polymorphism
Output:
and
salary = 40000.0
vacation days = 10 arrays
salary = 50000.0
vacation days = 10

10/30/2019 37
Exercises
 Assume that the following four classes have been declared:
public class Foo { public class Baz extends Foo {
public void method1() { public void method1() {
System.out.println("foo 1"); System.out.println("baz 1");
} }

public void method2() { public String toString() {


System.out.println("foo 2"); return "baz";
} }
}
public String toString() {
return "foo"; public class Mumble extends Baz {
} public void method2() {
} System.out.println("mumble 2");
}
public class Bar extends Foo { }
public void method2() {
System.out.println("bar 2");
}
}

10/30/2019 38
 What would be the output of the following client
code?

Foo[] pity = { new Baz(), new Bar(),


new Mumble(), new Foo() };
for (int i = 0; i < pity.length; i++) {
System.out.println(pity[i]);
pity[i].method1();
pity[i].method2();
System.out.println();
}

10/30/2019 39
10/30/2019 40
 The code produces the following output:
baz
baz 1
foo 2
foo
foo 1
bar 2
baz
baz 1
mumble 2
foo
foo 1
foo 2
10/30/2019 41
 Kind of override under the standard of
superclass!

10/30/2019 42
Variable Shadowing:
Something to avoid!!
 Polymorphism applies to methods in Java
 But not to fields! A a1 = new A();
A a2 = new B();
public class A { System.out.println(a1.method()); // prints 1
int x = 1; System.out.println(a2.method()); // prints 2

int method() { return 1; } System.out.println(a1.x);


// prints 1
} System.out.println(a2.x);
// prints 1 still!
public class B extends A { // Not like method, which prefers to its own.
int x = 2;
int method() { return 2; }
}

10/30/2019 43
 Variable Shadowing:
 When a class extends another class and
defines a field with the same name, each
object of the subclass contains two fields
with that name.
 The supclass’s version of the field is said

to shadow the subclass’s version, making


the subclass’s version invisible within that
class.
 This is called variable shadowing.
10/30/2019 44
public class A {
protected int x = 1; A a = new A();
protected void setX(int a){ B b = new B();
x=a; System.out.println(a.getX());
} System.out.println(b.getX());
protected int getX(){ System.out.println(b.getB());
return x;} System.out.println(a.x);
} System.out.println(b.x);

public class B extends A { A a = new A();


protected int x = 3; A b = new B();
System.out.println(a.getX());
public int getX(){ System.out.println(b.getX());
return x; } //System.out.println(b.getB());
System.out.println(a.x);
public int getB(){ System.out.println(b.x);
return x;}
} 10/30/2019 45

http://www.cs.wcupa.edu/~zjiang/240modifier_inheritance.pdf

10/30/2019 46
 Exercises
 Slide 39
 PolymorphismDemo in
 http://www.cs.wcupa.edu/~zjiang/Poly.zip

10/30/2019 47
 How to determine overriding and
shadow?
 Method, overriding, own study processing
 Attribute, shadow, >60 is a passing score
so that you cannot override it

10/30/2019 48
Modifiers
 Public
 A class, method, constructor, interface etc declared public
can be accessed from any other class. Therefore fields,
methods, blocks declared inside a public class can be
accessed from any class belonging to the Java Universe.
 Because of class inheritance, all public methods and
variables of a class are inherited by its subclasses.

10/30/2019 49
 Private
 Methods, Variables and Constructors that are declared
private can only be accessed within the declared class itself.
 Private access modifier is the most restrictive access level.
Class and interfaces cannot be private.
 Variables that are declared private can be accessed outside
the class only if public accessor methods are present in the
class.

10/30/2019 50
 Protected
 Variables, methods and constructors which are declared
protected in a superclass can be accessed only by the
subclasses.
 Protected access gives the subclass a chance to use the
helper method or variable, while preventing a nonrelated
class from trying to use it.

10/30/2019 51
 Default (like public)
 Default access modifier means we do not explicitly declare
an access modifier for a class, field, method etc.
 A variable or method declared without any access control
modifier is available to any other class in the same package.
 For example:
public class Logger {
String format;
String getFormat() { return this.format; }
public void setFormat(String f) { format = f; }
}

10/30/2019 52
Abstract and Interfaces
 What have you learnt from the above exercises
on “extends”?
 Is it good to block the use of subclass method that is
not declared in super class?
 Good, because methods can be in template. In the

security control, no leakage!


 Is it good to have the direct access of attribute?
 Better not, if it is not in your control. See how

complicate it is in the variable shadowing.


 Suppose we’ve defined an Employee class, and we
don’t want someone to come along and muck it up
 E.g., we don’t want a CEO subclass that gives itself a raise

 The final keyword indicates that some definition


(of a class, method, or field) cannot be changed or
overridden by a subclass.

54
final Example
// A class to represent employees
public class Employee {
public int getHours() {
return 40; // works 40 hours / week
}

public final double getSalary() {


return 40000.0; // $40,000.00 / year
}

public int getVacationDays() {


return 10; // 2 weeks' paid vacation
}

public String getVacationForm() {


return "yellow"; // use the yellow form
}
}
No subclass is allowed to change the definition of getSalary()! 55
final fields, methods, and
classes
The final keyword can be applied to fields (as we’ve seen before):
// no code may change the value of salary,
//including a subclass’s code
public final double salary = 40000.0;

Or to methods:
// subclasses cannot override the getSalary method
public final double getSalary() { return salary; }

Or even to classes:
// the Employee class cannot be extended
// It can’t have any subclasses at all!
public final class Employee { ... }
56
Opposite of final
 The final keyword prevents subclasses from
changing (overriding) code

 Sometimes, you want to do the opposite:

Force another programmer or piece of code


to finish parts of a class.
Example: Employee salary
 Let’s say you want every subclass of
Employee to have a salary, but you
want the subclass to decide what the
salary should be.

We can define an “abstract” getSalary()


method, p668:
public abstract double
getSalary();
Abstract Rules (1)
If a class has an abstract method (p669), or it
inherits an abstract method that it doesn’t
override, then the class must be declared
abstract (p669).

public abstract class Employee {


public abstract double getSalary();
// you can mix abstract and non-abstract methods
// in an abstract class
public int getHours() { // Note: not abstract!
return 40;
}
Abstract Rules (2)
If a class is abstract, it can’t have a constructor.
 No Employee object can be constructed
 But you can declare Employee references.

public abstract class Employee {


public abstract double getSalary();

public static void main(String [] args) {


Employee e; // NO ERROR: reference is fine
e = new Employee(); // ERROR! No constructor
}
}
Extending an abstract class
public class Lawyer extends Employee {
// since Employee declares an abstract getSalary,
// Lawyer must define getSalary by overriding it
// or else Lawyer must be an abstract class
public double getSalary() {
return 45000.0;
}

public static void main(String [] args) {


Employee e; // Fine, no problem
e = new Lawyer(); // Also fine (polymorphism)
e = new Employee(); // ERROR! No constructor!
}
}
Abstract classes: what’s the
point?
 If you can’t construct objects for a class,
what’s the point of the class? How can we
use it?
 Short Answer: polymorphism.
 We can use references of type Employee as a
place to store Lawyers, Secretaries, CEOs, etc.
 Because getSalary() is declared in Employee,
e.getSalary() is legal syntax, even though
getSalary() is not defined in Employee.
http://docs.oracle.com/javase/tutorial/java/Iand
I/abstract.html
Exercise
 Create an abstract ClosedShape class
with an abstract getArea() method
 Write non-abstract subclasses
Rectangle and Circle
 Write main methods for each that
construct an object and print its area.
Do not use attribute to store area
information.
public abstract class Closedshape{ public class shapeApp {
public abstract double getArea(); } public static void main(String [] args){
Rectangle r = new Rectangle(2, 3);
public class Circle extends Closedshape{ Circle c = new Circle(1.5);
private double radius; System.out.println(r.getArea());
public Circle (double r){ System.out.println(c.getArea());
radius = r; } }
}
public double getArea (){
return Math.PI*radius*radius; } 6.0
7.0685834705770345
public double getCircumference(){
return radius; }
}

public class Rectangle extends Closedshape{


private int length=0;
private int width=0;
public Rectangle(int l, int w){
length=l;
width=w; }

public double getArea (){


return length*width; }
} 10/30/2019 65
Going full abstract
 What if our abstract class had no non-
abstract methods?
public abstract class Employee {
public abstract double getSalary();
public abstract int getHours();
public abstract String getVacationForm();
}

Each subclass would have different definitions.


They share only the names of their methods.
Java has an alternative way to do this:
interfaces (p675).

66
Interfaces
 Let's say you have the following two
related classes:
public class Scientist { public class Engineer {
public void discover() { public void discover() {
System.out.println(“Eureka! I System.out.println(“Cool, what did
have found it!“); I just do?“);
} }
public void publish() { public void publish() {
System.out.println(“My System.out.println(“I don't
research is better than yours.”); know how this happened, but it
} works.”);
}
}
}

 Neither of their methods do the same thing.


67
Code Reuse
 But they're still similar – they both discover
and publish. Can we get code reuse?

interface Researcher {
void discover();
void publish();
}

 Now we can create Researcher references

68
Using Interface Objects
public static void researchCycle(Researcher r) {
r.discover();
r.publish();
}

public static void main(String [] args) {


Researcher researcher1 = new Scientist();
Researcher researcher2 = new Engineer();
// Interfaces have no constructors
// They can only be used as types for references
researcher2 = new Researcher(); // ERROR!

researchCycle(researcher1);
researchCycle(researcher2);
}

69
Using Interfaces
 Interfaces are a way of specifying what objects are
capable of, without saying how.

 Interface variables can execute any of the methods listed


in the interface, but the behavior depends on the class of
the object
 That is, interface variables are polymorphic.

 There are no constructors for interfaces. They are not


classes, and no objects of that run-time type are created.
They are compile-time types for references.
70
Implementing Interfaces

public class Scientist implements Researcher {

public void discover() {


System.out.println(“Eureka! I have found it!“);
}

public void publish() {


System.out.println(“My research is better than yours.”);
}
}

71
public class Engineer implements Researcher {
public void discover() {
System.out.println(“Whoa, what did I just do?“);
}

public void publish() {


System.out.println(“I don't know how this happened, but
it works.”);
}
}

72
Exercise
 Create an interface Measurable class
with an non-abstract getArea() method
 Write subclasses Rectangle and Circle to
implement the above interface
 Write main methods for each that
construct an object and print its area.
Do not use attribute to store area
information.
public interface Measurable{ public class shapeApp {
public abstract double getArea(); } public static void main(String [] args){
Rectangle r = new Rectangle(2, 3);
public class Circle implements Measurable{ Circle c = new Circle(1.5);
private double radius; System.out.println(r.getArea());
public Circle (double r){ System.out.println(c.getArea());
radius = r; } }
}
public double getArea (){
return Math.PI*radius*radius; } 6.0
7.0685834705770345
public double getCircumference(){
return radius; }
}

public class Rectangle implements Measurable{


private int length=0;
private int width=0;
public Rectangle(int l, int w){
length=l;
width=w; }

public double getArea (){


return length*width;}
} 10/30/2019 74
public interface Measurable{ public class shapeApp {
public abstract double getArea(); } public static void main(String [] args){
// Rectangle r = new Rectangle(2, 3);
public class Circle implements Measurable{ // Circle c = new Circle(1.5);
private double radius; // System.out.println(r.getArea());
public Circle (double r){ // System.out.println(c.getArea());
radius = r; } Measurable r = new Rectangle(2, 3);
//polymorphism
public double getArea (){ display(r);
return Math.PI*radius*radius; } Circle c = new Circle(1.5);
Measurable m = c;
public double getCircumference(){ display(m);
return radius; } // (m.getCircumference()); not allowed
} Circle t = (Circle) m;
System.out.println(t.getCircumference());
public class Rectangle implements Measurable{
}
private int length=0;
public static void display (Measurable fig){
private int width=0; System.out.println(fig.getArea());
public Rectangle(int l, int w){ }
length=l;
width=w; }
6.0
public double getArea (){ 7.0685834705770345
return length*width;} 1.5
} 10/30/2019 75
Comparable interface
 Define the Comparable interface
public interface Comparable {
public int compareTo(Object other);
}

public interface Comparable<T> {


public int compareTo(T other);
}

Return a comparable value!


 Fruit.java
 FruitDemo.java
 Contained in the zip file:
http://www.cs.wcupa.edu/~zjiang/Poly.zip

10/30/2019 77
 Explanation of
interfacehttp://docs.oracle.com/javase/tutoria
l/java/concepts/interface.html
 Exercises
 Revisit ButtonDemo.java in
http://www.cs.wcupa.edu/~zjiang/button.zip

10/30/2019 79
GUI
 Window interface, p755
 JFrame
 Basic components
 Button
 see the button sample available in Chapter 0 from class website

 Event
 Multiple events (e.g., which button is clicked?)
 Label
 Text field
 Layouts, p787
 https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
 Other Buttons
 https://docs.oracle.com/javase/tutorial/uiswing/components/button.html
 Color
 Complete package of the samples of the above
 https://www.cs.wcupa.edu/~zjiang/csc142_java_sample.htm

You might also like