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

More Related Content

PPT
PPT
JavaYDL11
PPT
oops-1
PPT
Unit i
PPT
Ch2 Liang
PDF
Java oop
PDF
Quoc le tera-scale deep learning
PDF
Public-Key Identification Schemes Based on Multivariate Polynomials
JavaYDL11
oops-1
Unit i
Ch2 Liang
Java oop
Quoc le tera-scale deep learning
Public-Key Identification Schemes Based on Multivariate Polynomials

What's hot (9)

PPTX
01 Java Language And OOP PART I
PPT
C++ classes tutorials
PPT
Java basic
PPTX
Objective c slide I
PPTX
Classes and objects
PPT
JavaYDL8
PPT
Lecture4
PPT
Lecture4
PDF
Java Day-2
01 Java Language And OOP PART I
C++ classes tutorials
Java basic
Objective c slide I
Classes and objects
JavaYDL8
Lecture4
Lecture4
Java Day-2
Ad

Similar to OOP: Class Hierarchies (20)

PPTX
Inheritance
PDF
Java OO Revisited
PPT
Lecture18
PDF
Chapter 04 inheritance
PPT
Lec 42.43 - virtual.functions
PPTX
Classes, objects in JAVA
PPT
Lec 30.31 - inheritance
PPT
Lecture java continued 1
PDF
Java beans
PDF
C# Summer course - Lecture 3
PPS
Introduction to class in java
PPT
Bca 2nd sem u-2 classes & objects
PPT
Mca 2nd sem u-2 classes & objects
PPTX
Classes and objects
PPTX
05 Object Oriented Concept Presentation.pptx
PPTX
Inheritance
PPTX
Inheritance
PDF
OOPs & Inheritance Notes
PDF
Java Programming - 04 object oriented in java
Inheritance
Java OO Revisited
Lecture18
Chapter 04 inheritance
Lec 42.43 - virtual.functions
Classes, objects in JAVA
Lec 30.31 - inheritance
Lecture java continued 1
Java beans
C# Summer course - Lecture 3
Introduction to class in java
Bca 2nd sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
Classes and objects
05 Object Oriented Concept Presentation.pptx
Inheritance
Inheritance
OOPs & Inheritance Notes
Java Programming - 04 object oriented in java
Ad

More from Atit Patumvan (20)

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

Recently uploaded (20)

PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
advance database management system book.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
IGGE1 Understanding the Self1234567891011
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
International_Financial_Reporting_Standa.pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
advance database management system book.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
TNA_Presentation-1-Final(SAVE)) (1).pptx
IGGE1 Understanding the Self1234567891011
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
International_Financial_Reporting_Standa.pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
B.Sc. DS Unit 2 Software Engineering.pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
My India Quiz Book_20210205121199924.pdf
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Hazard Identification & Risk Assessment .pdf
Introduction to pro and eukaryotes and differences.pptx
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
History, Philosophy and sociology of education (1).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