SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
Object-Oriented Programming:
                             Class Hierarchies



Atit Patumvan
Faculty of Management and Information Sciences
Naresuan University




                                 http://atit.patumvan.com
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.com
Object-Oriented Programming: Class Hierarchies
3




                              Class Diagram Mapping
                                                 class A {
                                                  class A {
                             A                   }
                                                     }




                                                 class B extends A {
                                                  class B extends A {
                             B                   }
                                                     }




                                                                        http://atit.patumvan.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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 Class
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies
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.com
Object-Oriented Programming: Class Hierarchies

Contenu connexe

Tendances (9)

01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART I
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Java basic
Java basicJava basic
Java basic
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
JavaYDL8
JavaYDL8JavaYDL8
JavaYDL8
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
Java Day-2
Java Day-2Java Day-2
Java Day-2
 

Similaire à OOP: Class Hierarchies

Similaire à OOP: Class Hierarchies (20)

Inheritance
InheritanceInheritance
Inheritance
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Lecture18
Lecture18Lecture18
Lecture18
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lecture java continued 1
Lecture java continued 1Lecture java continued 1
Lecture java continued 1
 
Java beans
Java beansJava beans
Java beans
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vector
 

Plus de Atit Patumvan

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agricultureAtit Patumvan
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)Atit Patumvan
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556Atit Patumvan
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationAtit Patumvan
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6Atit Patumvan
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsAtit Patumvan
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Atit Patumvan
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2Atit Patumvan
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1Atit Patumvan
 

Plus de Atit Patumvan (20)

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agriculture
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computation
 
Media literacy
Media literacyMedia literacy
Media literacy
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : Methods
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1
 

Dernier

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Dernier (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

OOP: Class Hierarchies

  • 1. Object-Oriented Programming: Class Hierarchies Atit Patumvan Faculty of Management and Information Sciences Naresuan University http://atit.patumvan.com
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 3. 3 Class Diagram Mapping class A { class A { A } } class B extends A { class B extends A { B } } http://atit.patumvan.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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 Class Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies
  • 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.com Object-Oriented Programming: Class Hierarchies