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)

PDF
Object-oriented Basics
PPT
Java Concepts
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPTX
Detailed_description_on_java_ppt_final.pptx
PPT
Core Java
PPT
Core java concepts
PPTX
Inheritance.pptx
PPT
11slide.ppt
PDF
Java Methods
PPT
11slide
PDF
OOPs & Inheritance Notes
PPT
Corejava Training in Bangalore Tutorial
PDF
C h 04 oop_inheritance
PDF
Hello. Im currently working on the last section to my assignment a.pdf
PDF
Java Programming - 04 object oriented in java
DOCX
OOP Lab Report.docx
PPT
8 polymorphism
PPT
Object Oriented Programming Concept.Hello
PPT
Defining classes-and-objects-1.0
PPT
Chapter 6 OOP (Revision)
Object-oriented Basics
Java Concepts
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Detailed_description_on_java_ppt_final.pptx
Core Java
Core java concepts
Inheritance.pptx
11slide.ppt
Java Methods
11slide
OOPs & Inheritance Notes
Corejava Training in Bangalore Tutorial
C h 04 oop_inheritance
Hello. Im currently working on the last section to my assignment a.pdf
Java Programming - 04 object oriented in java
OOP Lab Report.docx
8 polymorphism
Object Oriented Programming Concept.Hello
Defining classes-and-objects-1.0
Chapter 6 OOP (Revision)

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
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Spectroscopy.pptx food analysis technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectroscopy.pptx food analysis technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Machine learning based COVID-19 study performance prediction
20250228 LYD VKU AI Blended-Learning.pptx
NewMind AI Weekly Chronicles - August'25-Week II
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Network Security Unit 5.pdf for BCA BBA.
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
Assigned Numbers - 2025 - Bluetooth® Document
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MYSQL Presentation for SQL database connectivity
Encapsulation theory and applications.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) ); // ?