Object  Orientated  Programming  with   Java   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
Object  Orientated  Concepts   •  Class   •  Object   •  Inheritance   •  Constructors   •  Abstract  class   •  Interface   •  Polymorphism  
Class   class Student { private String name; public Student(String name) { this.name = name; } public void setName(String name) { this.name = name } public void getName() { return name; } }
CreaAng  objects   Student jack = new Student(“Jack”); Student bill = new Student(“Bill”); System.out.println( jack.getName() ); System.out.println( bill.getName() );
Reference?   Student jack = new Student(“Jack”); Student bill = jack; jack.setName(“Lisa”); // What is the output? System.out.println(bill.getName());
Reference   •  In  Java,  every  object  is  passed  by  reference   •  If  you  want  to  clone  a  object,  you  use  special   techniques  (later  on  the  material)  
Inheritance   class Person { private String name; ... } class Student extends Person { private int id; ... }
Constructors   class Person { private String name; public Person() { System.out.println(“Person”); } } class Student extends Person { private int id; public Student() { System.out.println(“Student”); } } // What is the output? Student s = new Student();
Default  Constructor   •  If  programmer  does  not  define  a  constructor,  Java   creates  a  default  constructor:   class Person { } => class Person { public Person() { super(); } }
Default  Constructor   class Person { private String name; public Person() { System.out.println(“Person”); } } class Student extends Person { private int id; } // What is the output? Student s = new Student();
Default  Constructor  Problem   class Person { private String name; public Person(String name) { System.out.println(“Person”); } } class Student extends Person { private int id; } // What is the output? Student s = new Student();
Abstract  Class   •  You  cannot  create  a  object  from  abstract  class   •  Abstract  class  may  contain  abstract  method   •  Abstract  method  is  a  method  declaraAon  which   must  be  implemented  in  inherited  classes  
Abstract  Class   abstract class Graphic { abstract double calculateSurfaceArea(); } class Circle extends Graphic { private int radius; double calculateSurfaceArea() { ... } }
Abstract  Class   abstract class A { abstract void m(); } abstract class B extends A { // What is the implementation of the class B? }
Interface   •  Interface  is  a  abstract  class  that  contain  only   abstract  methods   •  Interface  can  contain  also  public  staAc  final   variables   •  Class  can  inherit  only  one  class,  but  it  can   implement  many  interfaces  
Interface   interface class A { public void m(); } class B implements A { public void m() { ... } }
Polymorphism   •  Declaring  a  object:   –  Graphic c;! •  IniAalizing  the  object:   –  c = new Graphic();! •  This  is  also  possible:   –  c = new Circle();! –  c = new Rect();! •  If  Circle and  Rect are  inherited  from  Graphic!
Polymorphism   class Polymorphism { public static void main(String [] args) { // What are the possible objects to be passed? method(??) } public static void method(Graphic c) { ... } }
Polymorphism   interface R { ... } class Polymorphism { public static void main(String [] args) { // What are the possible objects to be passed? method(??) } public static void method(R r) { ... } }
Cloning   class Person implements Cloneable { private name; public Person(String name) { this.name = name; } public Object clone() { return new Person(this.name); } } Person a = new Person(“jack”); Person b = a.clone();
Equals   // What happens here? Student jack1 = new Student(“Jack”); Student jack2 = new Student(“Jack”); System.out.println( jack1 == jack2 ); // true or false? System.out.println( jack1.equals(jack2) ); // ?

Java OO Revisited

  • 1.
    Object  Orientated  Programming  with   Java   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2.
    Object  Orientated  Concepts   •  Class   •  Object   •  Inheritance   •  Constructors   •  Abstract  class   •  Interface   •  Polymorphism  
  • 3.
    Class   class Student { private String name; public Student(String name) { this.name = name; } public void setName(String name) { this.name = name } public void getName() { return name; } }
  • 4.
    CreaAng  objects   Student jack = new Student(“Jack”); Student bill = new Student(“Bill”); System.out.println( jack.getName() ); System.out.println( bill.getName() );
  • 5.
    Reference?   Student jack= new Student(“Jack”); Student bill = jack; jack.setName(“Lisa”); // What is the output? System.out.println(bill.getName());
  • 6.
    Reference   •  In  Java,  every  object  is  passed  by  reference   •  If  you  want  to  clone  a  object,  you  use  special   techniques  (later  on  the  material)  
  • 7.
    Inheritance   class Person{ private String name; ... } class Student extends Person { private int id; ... }
  • 8.
    Constructors   class Person { private String name; public Person() { System.out.println(“Person”); } } class Student extends Person { private int id; public Student() { System.out.println(“Student”); } } // What is the output? Student s = new Student();
  • 9.
    Default  Constructor   •  If  programmer  does  not  define  a  constructor,  Java   creates  a  default  constructor:   class Person { } => class Person { public Person() { super(); } }
  • 10.
    Default  Constructor   class Person { private String name; public Person() { System.out.println(“Person”); } } class Student extends Person { private int id; } // What is the output? Student s = new Student();
  • 11.
    Default  Constructor  Problem   class Person { private String name; public Person(String name) { System.out.println(“Person”); } } class Student extends Person { private int id; } // What is the output? Student s = new Student();
  • 12.
    Abstract  Class   •  You  cannot  create  a  object  from  abstract  class   •  Abstract  class  may  contain  abstract  method   •  Abstract  method  is  a  method  declaraAon  which   must  be  implemented  in  inherited  classes  
  • 13.
    Abstract  Class   abstract class Graphic { abstract double calculateSurfaceArea(); } class Circle extends Graphic { private int radius; double calculateSurfaceArea() { ... } }
  • 14.
    Abstract  Class   abstract class A { abstract void m(); } abstract class B extends A { // What is the implementation of the class B? }
  • 15.
    Interface   •  Interface  is  a  abstract  class  that  contain  only   abstract  methods   •  Interface  can  contain  also  public  staAc  final   variables   •  Class  can  inherit  only  one  class,  but  it  can   implement  many  interfaces  
  • 16.
    Interface   interface classA { public void m(); } class B implements A { public void m() { ... } }
  • 17.
    Polymorphism   •  Declaring  a  object:   –  Graphic c;! •  IniAalizing  the  object:   –  c = new Graphic();! •  This  is  also  possible:   –  c = new Circle();! –  c = new Rect();! •  If  Circle and  Rect are  inherited  from  Graphic!
  • 18.
    Polymorphism   class Polymorphism{ public static void main(String [] args) { // What are the possible objects to be passed? method(??) } public static void method(Graphic c) { ... } }
  • 19.
    Polymorphism   interface R{ ... } class Polymorphism { public static void main(String [] args) { // What are the possible objects to be passed? method(??) } public static void method(R r) { ... } }
  • 20.
    Cloning   class Personimplements Cloneable { private name; public Person(String name) { this.name = name; } public Object clone() { return new Person(this.name); } } Person a = new Person(“jack”); Person b = a.clone();
  • 21.
    Equals   // What happens here? Student jack1 = new Student(“Jack”); Student jack2 = new Student(“Jack”); System.out.println( jack1 == jack2 ); // true or false? System.out.println( jack1.equals(jack2) ); // ?