SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente (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
 

Similar a OOP: Class Hierarchies

Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
Princess Sam
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
Princess Sam
 
Lecture java continued 1
Lecture java continued 1Lecture java continued 1
Lecture java continued 1
Kamran Zafar
 

Similar a 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
 

Más de 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 tools
Atit Patumvan
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : Methods
Atit 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
 

Más 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
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

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