SlideShare a Scribd company logo
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) ); // ?

More Related Content

PPT
Java Programming - Polymorphism
PPTX
04. Review OOP with Java
PPTX
Pi j2.2 classes
PDF
Core java complete notes - Contact at +91-814-614-5674
PPT
Objected-Oriented Programming with Java
PPTX
03 classes interfaces_principlesofoop
PPTX
Classes in c++ (OOP Presentation)
PPTX
Object Oriented Programming_Lecture 2
Java Programming - Polymorphism
04. Review OOP with Java
Pi j2.2 classes
Core java complete notes - Contact at +91-814-614-5674
Objected-Oriented Programming with Java
03 classes interfaces_principlesofoop
Classes in c++ (OOP Presentation)
Object Oriented Programming_Lecture 2

What's hot (20)

PDF
ITFT-Classes and object in java
PPTX
Pi j3.2 polymorphism
PPT
Class and object in C++
PPT
Best Core Java Training In Bangalore
PPTX
‫Object Oriented Programming_Lecture 3
PPTX
Classes and objects
PPT
Core java
PPT
Lect 1-class and object
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Class or Object
PPT
Object and class
PDF
Classes and objects in java
PPT
Object and Classes in Java
PPT
Classes&objects
PDF
Lect 1-java object-classes
PPT
Object Oriented Programming with Java
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPTX
Pi j2.3 objects
PDF
Java OOP Programming language (Part 5) - Inheritance
PDF
Java OOP Programming language (Part 3) - Class and Object
ITFT-Classes and object in java
Pi j3.2 polymorphism
Class and object in C++
Best Core Java Training In Bangalore
‫Object Oriented Programming_Lecture 3
Classes and objects
Core java
Lect 1-class and object
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Class or Object
Object and class
Classes and objects in java
Object and Classes in Java
Classes&objects
Lect 1-java object-classes
Object Oriented Programming with Java
[OOP - Lec 09,10,11] Class Members & their Accessing
Pi j2.3 objects
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 3) - Class and Object
Ad

Viewers also liked (17)

PPTX
Design Pattern lecture 1
PPT
Applying OO Concepts
PPT
JavaYDL15
ODP
Itt1 sd uml and oo
PDF
Object oriented fundamentals_in_java
PDF
OO & UML
PPTX
Introduction to OO, Java and Eclipse/WebSphere
PPTX
Unified modelling language (UML)
PPT
OOP programming
PPT
OO Development 3 - Models And UML
PPT
Module 3 Object Oriented Data Models Object Oriented notations
PPTX
Packaes & interfaces
PPT
What does OOP stand for?
ODP
Uml
PPTX
Java 103 intro to java data structures
PPT
Object Oriented Design in Software Engineering SE12
KEY
Objects & OO Thinking for Java
Design Pattern lecture 1
Applying OO Concepts
JavaYDL15
Itt1 sd uml and oo
Object oriented fundamentals_in_java
OO & UML
Introduction to OO, Java and Eclipse/WebSphere
Unified modelling language (UML)
OOP programming
OO Development 3 - Models And UML
Module 3 Object Oriented Data Models Object Oriented notations
Packaes & interfaces
What does OOP stand for?
Uml
Java 103 intro to java data structures
Object Oriented Design in Software Engineering SE12
Objects & OO Thinking for Java
Ad

Similar to Java OO Revisited (20)

PPT
8 polymorphism
PPT
Chap11
PPT
9781439035665 ppt ch10
PPT
9 abstract interface
PDF
java-06inheritance
PDF
javainterface
PPTX
Abstraction in java [abstract classes and Interfaces
PPT
Java interface
PPTX
Lecture 18
DOC
Complete java&j2ee
PPTX
Java presentation
PPT
Java inheritance
PPTX
Lecture 8 abstract class and interface
PPT
Java oops PPT
DOC
116824015 java-j2 ee
PPTX
L03 Software Design
PPTX
L02 Software Design
PDF
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
PPT
Ap Power Point Chpt7
PPT
025466482929 -OOP with Java Development Kit.ppt
8 polymorphism
Chap11
9781439035665 ppt ch10
9 abstract interface
java-06inheritance
javainterface
Abstraction in java [abstract classes and Interfaces
Java interface
Lecture 18
Complete java&j2ee
Java presentation
Java inheritance
Lecture 8 abstract class and interface
Java oops PPT
116824015 java-j2 ee
L03 Software Design
L02 Software Design
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
Ap Power Point Chpt7
025466482929 -OOP with Java Development Kit.ppt

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
PDF
Java Web Services
PDF
Box2D and libGDX
PDF
libGDX: Screens, Fonts and Preferences
PDF
libGDX: Tiled Maps
PDF
libGDX: User Input and Frame by Frame Animation
PDF
Intro to Building Android Games using libGDX
PDF
Advanced JavaScript Development
PDF
Introduction to JavaScript
PDF
Introduction to AngularJS
PDF
libGDX: Scene2D
PDF
libGDX: Simple Frame Animation
PDF
libGDX: Simple Frame Animation
PDF
libGDX: User Input
PDF
Implementing a Simple Game using libGDX
PDF
Building Android games using LibGDX
PDF
Android Threading
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
Creating Games for Asha - platform
PDF
Intro to Asha UI
Moved to Speakerdeck
Java Web Services
Box2D and libGDX
libGDX: Screens, Fonts and Preferences
libGDX: Tiled Maps
libGDX: User Input and Frame by Frame Animation
Intro to Building Android Games using libGDX
Advanced JavaScript Development
Introduction to JavaScript
Introduction to AngularJS
libGDX: Scene2D
libGDX: Simple Frame Animation
libGDX: Simple Frame Animation
libGDX: User Input
Implementing a Simple Game using libGDX
Building Android games using LibGDX
Android Threading
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Games for Asha - platform
Intro to Asha UI

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Machine learning based COVID-19 study performance prediction
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Electronic commerce courselecture one. Pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
Spectral efficient network and resource selection model in 5G networks
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Unlocking AI with Model Context Protocol (MCP)
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The AUB Centre for AI in Media Proposal.docx
A comparative analysis of optical character recognition models for extracting...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Assigned Numbers - 2025 - Bluetooth® Document
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools
sap open course for s4hana steps from ECC to s4
Per capita expenditure prediction using model stacking based on satellite ima...
Electronic commerce courselecture one. Pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Chapter 3 Spatial Domain Image Processing.pdf

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 class A { 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 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();
  • 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) ); // ?