23
http://atit.patumvan.com Object-Oriented Programming: Class Hierarchies Atit Patumvan Faculty of Management and Information Sciences Naresuan University

OOP: Class Hierarchies

Embed Size (px)

Citation preview

Page 1: OOP: Class Hierarchies

http://atit.patumvan.com

Object-Oriented Programming:Class Hierarchies

Atit PatumvanFaculty of Management and Information SciencesNaresuan University

Page 2: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

2

http://atit.patumvan.com

Subclass Definition

public class Person {

private String name; private int age;

public String toString() { return "Name: " + name + "\nAge: " + age; }}

public class Person {

private String name; private int age;

public String toString() { return "Name: " + name + "\nAge: " + age; }}

public class Employee extends Person {

private long salary; private Manager supervisor;}

public class Employee extends Person {

private long salary; private Manager supervisor;}

+ Person

-name : String-age : int

+toString() : String

+ Employee

-salary : long

+ Manager

-category : int

0..*-subordinates

1-supervisor import java.util.Vector;

public class Manager extends Employee {

private int category; private Vector subordinates;}

import java.util.Vector;public class Manager extends Employee {

private int category; private Vector subordinates;}

Supper Class

Sub Class

Page 3: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

3

http://atit.patumvan.com

Class Diagram Mapping

A

B

class A {

}

class A {

}

class B extends A {

}

class B extends A {

}

Page 4: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

4

http://atit.patumvan.com

Class Diagram Mapping

- id : int

B

class A { private int id; public int getID(){ return id; } public void setID(int id){ this.id = id; } }

class A { private int id; public int getID(){ return id; } public void setID(int id){ this.id = id; } }

class B extends A { }

class B extends A { }

A

+ getID() : int+ setID(int) : void

:B b = new B();b.setID(10); :

:B b = new B();b.setID(10); :

Tester Class

Page 5: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

5

http://atit.patumvan.com

Variable and Method Inheritance

public class Person {

private String name; private int age;

public String toString() { return "Name: " + getName() + "\nAge: " + getAge(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}

public class Person {

private String name; private int age;

public String toString() { return "Name: " + getName() + "\nAge: " + getAge(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}

Employee emp = new Employee(); emp.setName("Alice"); emp.setAge(28);

System.out.println(emp);

Employee emp = new Employee(); emp.setName("Alice"); emp.setAge(28);

System.out.println(emp);

+ Person

-name : String-age : int

+toString() : String

+ Employee

-salary : long

Page 6: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

6

http://atit.patumvan.com

Inheritance and Type Hierarchy

+ Person

-name : String-age : int

+toString() : String

+ Employee

-salary : long

Employee emp = new Employee();

emp.setName("Alice"); emp.setAge(28); emp.setSalary(15000);

System.out.println(emp);

Employee emp = new Employee();

emp.setName("Alice"); emp.setAge(28); emp.setSalary(15000);

System.out.println(emp);

-name : String -age : int

-salary : long

Employee

Person

Employee

Page 7: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

7

http://atit.patumvan.com

Variable Overriding

+ Person

-name : String-age : int

+toString() : String

+ Employee

-salary : long

+ Customer

-name : String

+showName() : void

public class Customer extends Person {

private String name;

public void showName() { System.out.println("Customer: "+getName()); System.out.println("Person: "+super.getName()); }}

public class Customer extends Person {

private String name;

public void showName() { System.out.println("Customer: "+getName()); System.out.println("Person: "+super.getName()); }}

Customer c = new Customer(); Person p = c; c.setName("Bob"); p.setName("Alice"); c.showName();

Customer c = new Customer(); Person p = c; c.setName("Bob"); p.setName("Alice"); c.showName();

Page 8: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

8

http://atit.patumvan.com

Method Overriding

public class Employee extends Person {:

@Override public String toString(){ return "Name: " + getName() + "\nAge: " + getAge() + "\nSalary: "+ getSalary(); }}

public class Employee extends Person {:

@Override public String toString(){ return "Name: " + getName() + "\nAge: " + getAge() + "\nSalary: "+ getSalary(); }}

Employee emp = new Employee(); Person per = emp; emp.setName("Alice"); emp.setAge(28); emp.setSalary(15000);

System.out.println(emp); System.out.println(per);

Employee emp = new Employee(); Person per = emp; emp.setName("Alice"); emp.setAge(28); emp.setSalary(15000);

System.out.println(emp); System.out.println(per);

+ Person

-name : String-age : int

+toString() : String

+ Employee

-salary : long

+toString() : String

Page 9: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

9

http://atit.patumvan.com

Inheritance and Constructors

public class Person { : public Person() { System.out.println("Instance of Person is created"); } :}

public class Person { : public Person() { System.out.println("Instance of Person is created"); } :}

public class Employee extends Person { : public Employee(){ System.out.println("Instance of Employee is created"); } :}

public class Employee extends Person { : public Employee(){ System.out.println("Instance of Employee is created"); } :}

Constructor are not inherited (and there fore cannot be overridden)

Employee emp = new Employee();

Employee emp = new Employee();

Page 10: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

10

http://atit.patumvan.com

Inheritance and Constructors(cont.)

public class Person { : public Person() { System.out.println("Instance of Person is created"); } public Person(String name){ this.name = name; } :}

public class Person { : public Person() { System.out.println("Instance of Person is created"); } public Person(String name){ this.name = name; } :}

public class Employee extends Person { : public Employee(){ System.out.println("Instance of Employee is created"); } public Employee(String name){ } :}

public class Employee extends Person { : public Employee(){ System.out.println("Instance of Employee is created"); } public Employee(String name){ } :}

Employee emp = new Employee("Alice"); System.out.println(emp);

Employee emp = new Employee("Alice"); System.out.println(emp);

Page 11: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

11

http://atit.patumvan.com

Final Class

// Error: VIPCustomer cannot be subclasspublic class VIPCustomer extends Customer{

}

// Error: VIPCustomer cannot be subclasspublic class VIPCustomer extends Customer{

}

public final class Customer extends Person {

private String name;

public void showName() { System.out.println("Customer: "+getName()); System.out.println("Person: "+super.getName()); }}

public final class Customer extends Person {

private String name;

public void showName() { System.out.println("Customer: "+getName()); System.out.println("Person: "+super.getName()); }}

A final class cannot be subclassed.

Customer cus = new Customer();Customer cus = new Customer();

Page 12: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

12

http://atit.patumvan.com

Abstract Classes, Abstract Methods

● Abstract classes● Cannot be instantiated● Cannot be subclassed

● Abstract methods● Method without code, they are declared but not defined● Must be defined in some subclass

● Abstract class can have non-abstract methods

● An abstract method must belong to an abstract class

Page 13: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

13

http://atit.patumvan.com

Abstract Classes: Example

public abstract class Shape { abstract double area();}

public abstract class Shape { abstract double area();}

+ Shape

+area() : double

+ Circle

-center : java.awt.geom.Point2D-radius : double

public class Circle extends Shape {

private java.awt.geom.Point2D center; private double radius;

@Override double area() { return Math.PI * radius * radius; }}

public class Circle extends Shape {

private java.awt.geom.Point2D center; private double radius;

@Override double area() { return Math.PI * radius * radius; }}

Page 14: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

14

http://atit.patumvan.com

Inherited Abstract Methods

public abstract class Shape { abstract double area();}

public abstract class Shape { abstract double area();}

//Error: Polygon must be abstractpublic class Polygon extends Shape{}

//Error: Polygon must be abstractpublic class Polygon extends Shape{}

public class Triangle extends Polygon {

private java.awt.geom.Point2D a, b, c;

@Override double area() {

return Math.abs((b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY()) * (c.getX() - a.getX())) / 2; }}

public class Triangle extends Polygon {

private java.awt.geom.Point2D a, b, c;

@Override double area() {

return Math.abs((b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY()) * (c.getX() - a.getX())) / 2; }}

+ Shape

+area() : double

+ Polygon

+ Triangle

-a : java.awt.geom.Point2D-b : java.awt.geom.Point2D-c : java.awt.geom.Point2D

Page 15: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

15

http://atit.patumvan.com

Interface

● Collection of undefined methods and constant values

● Similar to an abstract class where all method are abstract and public, and all variable are public, static and final

● Subclass a class implement an interface→

● A class may implement several interfaces

Page 16: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

16

http://atit.patumvan.com

Using an Interface

interface Shape { public double area(); public double volume();}

interface Shape { public double area(); public double volume();}

public class Point implements Shape {

static int x, y;public Point() {

x = 0;y = 0;

}public double area() {

return 0;}public double volume() {

return 0;}public static void print() {

System.out.println("point: " + x + "," + y);}

}

public class Point implements Shape {

static int x, y;public Point() {

x = 0;y = 0;

}public double area() {

return 0;}public double volume() {

return 0;}public static void print() {

System.out.println("point: " + x + "," + y);}

}

Page 17: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

17

http://atit.patumvan.com

Polymorphism

● Polymorphic, having multiple behavior

● A polymorphic method results in different actions depending on the object being referenced

● Also knows as late binding or run-time binding

Page 18: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

18

http://atit.patumvan.com

Polymorphism Example

public abstract class Shape { abstract double area();}

public abstract class Shape { abstract double area();}

public class Circle extends Shape { private double radius; @Override public double area() { return Math.PI * getRadius() * getRadius(); } :}

public class Circle extends Shape { private double radius; @Override public double area() { return Math.PI * getRadius() * getRadius(); } :}

public class Rectangle extends Shape { private double width; private double length; @Override public double area() { return width*length; } :}

public class Rectangle extends Shape { private double width; private double length; @Override public double area() { return width*length; } :}

public class Calculator { public static double calculateArea(Shape shape){ return shape.area(); }}

public class Calculator { public static double calculateArea(Shape shape){ return shape.area(); }}

Circle circle = new Circle(); circle.setRadius(5.0);

Rectangle rectangle = new Rectangle(); rectangle.setWidth(3.0); rectangle.setLength(2.0); System.out.println(Calculator.calculateArea(circle)); System.out.println(Calculator.calculateArea(rectangle));

Circle circle = new Circle(); circle.setRadius(5.0);

Rectangle rectangle = new Rectangle(); rectangle.setWidth(3.0); rectangle.setLength(2.0); System.out.println(Calculator.calculateArea(circle)); System.out.println(Calculator.calculateArea(rectangle));

Page 19: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

19

http://atit.patumvan.com

Inner Class

● It’s possible (and sometimes encouraged!) to define one class within another.

● This provides another way to group classes that work closely together.

● Inner classes can be “shielded” so that they are unknown to the outside world.

● Often inner classes are used to hide a class-specific implementation of an external interface.

● Inner classes can be classified into four types, Static Nested Class or Interface, Member Classes, Local Classes and Anonymous Class

Page 20: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

20

http://atit.patumvan.com

Static Nested Class or Interface

class A { static class B{ }}

class A { static class B{ }}

interface C{ static class D{ } }

interface C{ static class D{ } }

class E{ static interface F{ }}

class E{ static interface F{ }}

interface G{ static interface H{ }}

interface G{ static interface H{ }}

class A { static class B { void print() { System.out.println("B"); } } void print() { System.out.println("A"); }}

public class NestedClassTest1 {

public static void main(String[] args) { A a = new A(); a.print(); //A A.B b = new A.B(); b.print(); //B }}

class A { static class B { void print() { System.out.println("B"); } } void print() { System.out.println("A"); }}

public class NestedClassTest1 {

public static void main(String[] args) { A a = new A(); a.print(); //A A.B b = new A.B(); b.print(); //B }}

Page 21: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

21

http://atit.patumvan.com

Member Classes

class A { class B{ }}

class A { class B{ }}

class A { class B { void print() { System.out.println("B"); } }

void print() { System.out.println("A"); }}

public class MemberClassTest {

public static void main(String[] args) { A a = new A(); a.print(); //A A.B b = a.new B(); b.print(); //B }}

class A { class B { void print() { System.out.println("B"); } }

void print() { System.out.println("A"); }}

public class MemberClassTest {

public static void main(String[] args) { A a = new A(); a.print(); //A A.B b = a.new B(); b.print(); //B }}

Page 22: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

22

http://atit.patumvan.com

Local Classes

class A {

A() { class B {

B() { System.out.println("B"); } } new B(); }}

public class LocalClassTest {

public static void main(String[] args) { new A(); // B }}

class A {

A() { class B {

B() { System.out.println("B"); } } new B(); }}

public class LocalClassTest {

public static void main(String[] args) { new A(); // B }}

Local class is a class defined within a method or branch

Page 23: OOP: Class Hierarchies

Object-Oriented Programming: Class Hierarchies

23

http://atit.patumvan.com

Anonymous Classes

class A { int x = 0; A(int x) { this.x = x; }

void f() { System.out.println(x); }}

class A { int x = 0; A(int x) { this.x = x; }

void f() { System.out.println(x); }}

public class AnoClassTest2 {

static void caller(A a) { a.f(); }

public static void main(String[] args) { caller(new A(1) { void f() { System.out.println(++x); } }); }}

public class AnoClassTest2 {

static void caller(A a) { a.f(); }

public static void main(String[] args) { caller(new A(1) { void f() { System.out.println(++x); } }); }}

public class AnoClassTest1 {

static void caller(A a) { a.f(); }

public static void main(String[] args) { caller(new A(1) { }); }}

public class AnoClassTest1 {

static void caller(A a) { a.f(); }

public static void main(String[] args) { caller(new A(1) { }); }}