Object-Oriented Programming:                             Class HierarchiesAtit PatumvanFaculty of Management and Informati...
2                                       Subclass Definition                                                     public cla...
3                              Class Diagram Mapping                                                 class A {            ...
4                              Class Diagram Mapping                             A                   class A {            ...
5              Variable and Method Inheritance      public class Person {       public class Person {                     ...
6               Inheritance and Type Hierarchy                   + Person             -name : String             -age : in...
7                                       Variable Overriding                          + Person                 public class...
8                                        Method Overriding      public class Employee extends Person {        public class...
9                     Inheritance and Constructors       Constructor are not inherited (and there fore cannot be overridde...
10              Inheritance and Constructors(cont.)      public class Person {        public class Person {          :    ...
11                                                 Final Class   A final class cannot be subclassed.      public final cla...
12        Abstract Classes, Abstract Methods        ●    Abstract classes               ●   Cannot be instantiated        ...
13                         Abstract Classes: Example      public abstract class Shape {                              + Sha...
14                               Inherited Abstract Methods      public abstract class Shape {        public abstract clas...
15                                                 Interface        ●    Collection of undefined methods and constant valu...
16                                        Using an Interface      interface Shape {        interface Shape {          publ...
17                                                 Polymorphism        ●    Polymorphic, having multiple behavior        ●...
18                              Polymorphism Example      public abstract class Shape {        public abstract class Shape...
19                                                 Inner Class        ●    It’s possible (and sometimes encouraged!) to de...
20                Static Nested Class or Interface      class A {                          class A {        class A {     ...
21                                             Member Classes      class A {                          class A {        cla...
22                                                 Local Classes      Local class is a class defined within a method or br...
23                                    Anonymous Classes      class A {       class A {         int x = 0;           int x ...
Upcoming SlideShare
Loading in …5
×

OOP: Class Hierarchies

1,089 views
980 views

Published on

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,089
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
40
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

OOP: Class Hierarchies

  1. 1. Object-Oriented Programming: Class HierarchiesAtit PatumvanFaculty of Management and Information SciencesNaresuan University http://atit.patumvan.com
  2. 2. 2 Subclass Definition public class Person { public class Person { + Person Supper Class private String name; -name : String private String name; -age : int private int age; private int age; +toString() : String public String toString() { public String toString() { return "Name: " + name + "nAge: " + age; return "Name: " + name + "nAge: " + age; } } } } + Employee Sub Class -salary : long public class Employee extends Person { public class Employee extends Person { private long salary; private long salary; -subordinates private Manager supervisor; 0..* private Manager supervisor; } } 1 import java.util.Vector; -supervisor import java.util.Vector; public class Manager extends Employee { + Manager public class Manager extends Employee { -category : int private int category; private int category; private Vector subordinates; private Vector subordinates; } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  3. 3. 3 Class Diagram Mapping class A { class A { A } } class B extends A { class B extends A { B } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  4. 4. 4 Class Diagram Mapping A class A { class A { private int id; private int id; - id : int public int getID(){ public int getID(){ + getID() : int return id; return id; } + setID(int) : void } public void setID(int id){ public void setID(int id){ this.id = id; this.id = id; } } } } class B extends A { class B extends A { B } } : Tester Class : B b = new B(); B b = new B(); b.setID(10); b.setID(10); : : http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  5. 5. 5 Variable and Method Inheritance public class Person { public class Person { + Person private String name; -name : String private String name; -age : int private int age; private int age; +toString() : String public String toString() { public String toString() { return "Name: " + getName() + "nAge: " + getAge(); return "Name: " + getName() + "nAge: " + getAge(); } } public String getName() { + Employee public String getName() { return name; return name; -salary : long } } public void setName(String name) { public void setName(String name) { this.name = name; this.name = name; } } public int getAge() { public int getAge() { return age; Employee emp = new Employee(); return age; Employee emp = new Employee(); } } public void setAge(int age) { emp.setName("Alice"); public void setAge(int age) { emp.setName("Alice"); this.age = age; emp.setAge(28); this.age = age; emp.setAge(28); } } } System.out.println(emp); } System.out.println(emp); http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  6. 6. 6 Inheritance and Type Hierarchy + Person -name : String -age : int Employee +toString() : String -name : String -age : int Person + Employee -salary : long Employee -salary : long Employee emp = new Employee(); Employee emp = new Employee(); emp.setName("Alice"); emp.setName("Alice"); emp.setAge(28); emp.setAge(28); emp.setSalary(15000); emp.setSalary(15000); System.out.println(emp); System.out.println(emp); http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  7. 7. 7 Variable Overriding + Person public class Customer extends Person { public class Customer extends Person { -name : String -age : int private String name; private String name; +toString() : String public void showName() { public void showName() { System.out.println("Customer: "+getName()); System.out.println("Customer: "+getName()); System.out.println("Person: "+super.getName()); System.out.println("Person: "+super.getName()); } + Employee + Customer } } -salary : long -name : String } +showName() : void Customer c = new Customer(); Customer c = new Customer(); Person p = c; Person p = c; c.setName("Bob"); c.setName("Bob"); p.setName("Alice"); p.setName("Alice"); c.showName(); c.showName(); http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  8. 8. 8 Method Overriding public class Employee extends Person { public class Employee extends Person { : : @Override @Override public String toString(){ public String toString(){ return "Name: " + getName() + "nAge: " + getAge() + "nSalary: "+ getSalary(); return "Name: " + getName() + "nAge: " + getAge() + "nSalary: "+ getSalary(); } } } } + Person -name : String Employee emp = new Employee(); Employee emp = new Employee(); -age : int Person per = emp; Person per = emp; emp.setName("Alice"); +toString() : String emp.setName("Alice"); emp.setAge(28); emp.setAge(28); emp.setSalary(15000); emp.setSalary(15000); System.out.println(emp); System.out.println(emp); + Employee System.out.println(per); System.out.println(per); -salary : long +toString() : String http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  9. 9. 9 Inheritance and Constructors Constructor are not inherited (and there fore cannot be overridden) public class Person { public class Person { : : public Person() { public Person() { System.out.println("Instance of Person is created"); System.out.println("Instance of Person is created"); } } : : } } public class Employee extends Person { public class Employee extends Person { : : public Employee(){ public Employee(){ System.out.println("Instance of Employee is created"); System.out.println("Instance of Employee is created"); } } : : } } Employee emp = new Employee(); Employee emp = new Employee(); http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  10. 10. 10 Inheritance and Constructors(cont.) public class Person { public class Person { : : public Person() { public Person() { System.out.println("Instance of Person is created"); System.out.println("Instance of Person is created"); } } public Person(String name){ public Person(String name){ this.name = name; this.name = name; } } : : } } public class Employee extends Person { public class Employee extends Person { : : public Employee(){ public Employee(){ System.out.println("Instance of Employee is created"); System.out.println("Instance of Employee is created"); } } public Employee(String name){ public Employee(String name){ } Employee emp = new Employee("Alice"); } Employee emp = new Employee("Alice"); : System.out.println(emp); : System.out.println(emp); } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  11. 11. 11 Final Class A final class cannot be subclassed. public final class Customer extends Person { public final class Customer extends Person { private String name; private String name; public void showName() { public void showName() { System.out.println("Customer: "+getName()); System.out.println("Customer: "+getName()); System.out.println("Person: "+super.getName()); System.out.println("Person: "+super.getName()); } } } } // Error: VIPCustomer cannot be subclass // Error: VIPCustomer cannot be subclass public class VIPCustomer extends Customer{ public class VIPCustomer extends Customer{ } } Customer cus = new Customer(); Customer cus = new Customer(); http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  12. 12. 12 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 http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  13. 13. 13 Abstract Classes: Example public abstract class Shape { + Shape public abstract class Shape { abstract double area(); abstract double area(); } } +area() : double public class Circle extends Shape { public class Circle extends Shape { + Circle private java.awt.geom.Point2D center; private java.awt.geom.Point2D center; -center : java.awt.geom.Point2D private double radius; private double radius; -radius : double @Override @Override double area() { double area() { return Math.PI * radius * radius; return Math.PI * radius * radius; } } } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  14. 14. 14 Inherited Abstract Methods public abstract class Shape { public abstract class Shape { + Shape abstract double area(); abstract double area(); } } +area() : double //Error: Polygon must be abstract //Error: Polygon must be abstract public class Polygon extends Shape{ public class Polygon extends Shape{ } } + Polygon public class Triangle extends Polygon { public class Triangle extends Polygon { private java.awt.geom.Point2D a, b, c; private java.awt.geom.Point2D a, b, c; @Override @Override + Triangle double area() { double area() { -a : java.awt.geom.Point2D return Math.abs((b.getX() - a.getX()) * (c.getY() - a.getY()) -b : java.awt.geom.Point2D return Math.abs((b.getX() - a.getX()) * (c.getY() - a.getY()) -c : java.awt.geom.Point2D - (b.getY() - a.getY()) * (c.getX() - a.getX())) / 2; - (b.getY() - a.getY()) * (c.getX() - a.getX())) / 2; } } } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  15. 15. 15 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 http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  16. 16. 16 Using an Interface interface Shape { interface Shape { public double area(); public double area(); public double volume(); public double volume(); } } public class Point implements Shape { public class Point implements Shape { static int x, y; static int x, y; public Point() { public Point() { x = 0; x = 0; y = 0; y = 0; } } public double area() { public double area() { return 0; return 0; } } public double volume() { public double volume() { return 0; return 0; } } public static void print() { public static void print() { System.out.println("point: " + x + "," + y); System.out.println("point: " + x + "," + y); } } } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  17. 17. 17 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 http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  18. 18. 18 Polymorphism Example public abstract class Shape { public abstract class Shape { public class Calculator { abstract double area(); public class Calculator { abstract double area(); public static double calculateArea(Shape shape){ } public static double calculateArea(Shape shape){ } return shape.area(); return shape.area(); } public class Circle extends Shape { } public class Circle extends Shape { } private double radius; } private double radius; @Override @Override public double area() { public double area() { return Math.PI * getRadius() * getRadius(); return Math.PI * getRadius() * getRadius(); } } : : Circle circle = new Circle(); } Circle circle = new Circle(); } circle.setRadius(5.0); circle.setRadius(5.0); public class Rectangle extends Shape { Rectangle rectangle = new Rectangle(); public class Rectangle extends Shape { Rectangle rectangle = new Rectangle(); private double width; rectangle.setWidth(3.0); private double width; rectangle.setWidth(3.0); private double length; private double length; rectangle.setLength(2.0); @Override rectangle.setLength(2.0); @Override public double area() { System.out.println(Calculator.calculateArea(circle)); public double area() { System.out.println(Calculator.calculateArea(circle)); return width*length; return width*length; System.out.println(Calculator.calculateArea(rectangle)); } System.out.println(Calculator.calculateArea(rectangle)); } : : } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  19. 19. 19 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 http://atit.patumvan.com Interface, Member Classes, Local Classes and Anonymous ClassObject-Oriented Programming: Class Hierarchies
  20. 20. 20 Static Nested Class or Interface class A { class A { class A { class A { static class B{ static class B { static class B{ static class B { } void print() { } void print() { } System.out.println("B"); } System.out.println("B"); } } } interface C{ } interface C{ void print() { static class D{ void print() { static class D{ System.out.println("A"); } System.out.println("A"); } } } } } } } class E{ public class NestedClassTest1 { class E{ public class NestedClassTest1 { static interface F{ static interface F{ public static void main(String[] args) { } public static void main(String[] args) { } A a = new A(); } A a = new A(); } a.print(); //A a.print(); //A A.B b = new A.B(); interface G{ A.B b = new A.B(); interface G{ b.print(); //B static interface H{ b.print(); //B static interface H{ } } } } } } } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  21. 21. 21 Member Classes class A { class A { class A { class A { class B{ class B { class B{ class B { } void print() { } void print() { } System.out.println("B"); } System.out.println("B"); } } } } void print() { void print() { System.out.println("A"); System.out.println("A"); } } } } public class MemberClassTest { public class MemberClassTest { public static void main(String[] args) { public static void main(String[] args) { A a = new A(); A a = new A(); a.print(); //A a.print(); //A A.B b = a.new B(); A.B b = a.new B(); b.print(); //B b.print(); //B } } } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  22. 22. 22 Local Classes Local class is a class defined within a method or branch class A { class A { A() { A() { class B { class B { B() { B() { System.out.println("B"); System.out.println("B"); } } } } new B(); new B(); } } } } public class LocalClassTest { public class LocalClassTest { public static void main(String[] args) { public static void main(String[] args) { new A(); // B new A(); // B } } } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies
  23. 23. 23 Anonymous Classes class A { class A { int x = 0; int x = 0; A(int x) { A(int x) { this.x = x; this.x = x; } } void f() { void f() { System.out.println(x); System.out.println(x); } public class AnoClassTest2 { } public class AnoClassTest2 { } } static void caller(A a) { static void caller(A a) { public class AnoClassTest1 { a.f(); public class AnoClassTest1 { a.f(); } } static void caller(A a) { static void caller(A a) { a.f(); public static void main(String[] args) { a.f(); public static void main(String[] args) { } caller(new A(1) { } caller(new A(1) { void f() { void f() { public static void main(String[] args) { System.out.println(++x); public static void main(String[] args) { System.out.println(++x); caller(new A(1) { } caller(new A(1) { } }); }); }); }); } } } } } } } } http://atit.patumvan.comObject-Oriented Programming: Class Hierarchies

×