Java: introduction to object-oriented features

Similar documents
Java Fundamentals (II)

CMSC 132: Object-Oriented Programming II

1 Shyam sir JAVA Notes

Programming overview

Practice for Chapter 11

A Short Summary of Javali

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

CS-202 Introduction to Object Oriented Programming

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Java Object Oriented Design. CSC207 Fall 2014

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Class, Variable, Constructor, Object, Method Questions

What is Inheritance?

Chapter 6 Introduction to Defining Classes

C++ Important Questions with Answers

Computer Science II (20073) Week 1: Review and Inheritance

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance -- Introduction

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Declarations and Access Control SCJP tips

Java: advanced object-oriented features

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Introduction to Programming Using Java (98-388)

CMSC 132: Object-Oriented Programming II

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Chapter 5 Object-Oriented Programming

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance (continued) Inheritance

CPS 506 Comparative Programming Languages. Programming Language

Index COPYRIGHTED MATERIAL

Compaq Interview Questions And Answers

Rules and syntax for inheritance. The boring stuff

Java and C# in Depth

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Data Structures (list, dictionary, tuples, sets, strings)

25. Generic Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Properties of an identifier (and the object it represents) may be set at

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Data Abstraction. Hwansoo Han

Introduction to Inheritance

Inheritance and Polymorphism

EMBEDDED SYSTEMS PROGRAMMING More About Languages

The Decaf Language. 1 Lexical considerations

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

CLASS DESIGN. Objectives MODULE 4

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Binghamton University. CS-140 Fall Dynamic Types

C#: advanced object-oriented features

OBJECT ORİENTATİON ENCAPSULATİON

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Chapter 4 Defining Classes I

Operators and Expressions

PROGRAMMING LANGUAGE 2

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Java Programming Training for Experienced Programmers (5 Days)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Inheritance Motivation

Universe Type System for Eiffel. Annetta Schaad

Short Notes of CS201

WA1278 Introduction to Java Using Eclipse

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Implements vs. Extends When Defining a Class

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

First IS-A Relationship: Inheritance

CS201 - Introduction to Programming Glossary By

Subtype Polymorphism

Lecture 4: Extending Classes. Concept

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Example: Count of Points

Language Features. 1. The primitive types int, double, and boolean are part of the AP

JavaScript: Sort of a Big Deal,

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

What are the characteristics of Object Oriented programming language?

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Modern Programming Languages. Lecture Java Programming Language. An Introduction

More On inheritance. What you can do in subclass regarding methods:

Inheritance and Polymorphism

COP 3330 Final Exam Review

CS 251 Intermediate Programming Inheritance

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Transcription:

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java classes and objects

Classes and objects The basic encapsulation unit is the class as in every object-oriented language A class is made of a number of features (or members) instance variables (attributes, fields) methods Classes and features have different levels of visibility Objects are class instances and classes are sets of objects or blueprints for creating objects constructors are special methods to create new objects in Java, objects are automatically destroyed when no longer referenced (garbage collection) no destructors, but optional finalize methods 3

A simple class example package ch.ethz.inf.se.javacsharpindepth; /** * @author John H. Doe */ public class MainClass { // main must be all lowercase public static void main (String[] args) { Game mygame = new Game(); System.out.println("Game starts!"); mygame.startgame(); 4

Attributes (instance variables, fields) Relate to a class instance Declared within the class curly brackets, outside any method Visible at least within the class scope, within any method of the class Automatically initialized to the default values 0 or 0.0 for numeric types, \u0000 for chars, null for references, false for booleans 5

Methods (instance methods, member functions) Relate to an instance and are declared within the class curly brackets May have arguments Must have return type (possibly void) boolean test(int i, boolean b){ // some stuff here return true; Constructors are special (more on this later) 6

Information hiding Attribute and method visibility modifiers : public: visible everywhere protected: visible in the same package and in subclasses (wherever they are) (*): visible in the same package private: visible only in the class in which it is defined Class visibility Top level classes can only have default or public visibility Nested classes can have any chosen visibility level (except for inner classes: see later) (*) No keyword for package visibility: it s the default 7

Classes, packages, and files The Java language specification does not constrain how classes and packages are stored in files. However, practically all implementations of the Java platform follow Sun s original conventions: one top-level public class per file compiled to one.class bytecode file packages hierarchically map to directories Example: public class MyClass in package my.package is stored in file: MyClass.java in subdirectory my/package/ relative to the sourcepath root. Its compiled version is in file MyClass.class in subdirectory my/package/ relative to the classpath root directory sourcepath and classpath often coincide 8

The static modifier When applied to non-local variables and methods Relates to a specific class, not to a class instance Shared by every object of a certain class (in the JVM) Accessed without creating any class object Kind of like a global entity Static methods can only reference static entities, locals, and arguments (no instance members) MyClass.myStaticAttribute MyClass.myStaticMethod() The static modifier does not apply to top-level classes in Java 9

Constructors Same name as the class No return type (not even void) An argumentless constructor is provided by default if no other constructor is explicitly given 10

Local variables Declared within a method s scope (denoted by curly brackets) Visible only within the method s scope De-allocated at method end Not automatically initialized warning if no explicit initialization is given 11

The keyword this Refers to the current object. Typical usage: bypass local variable shadowing attribute. public class Card { private int value; public int getvalue() { return value; public void setvalue(int value) { this.value = value; 12

Nested classes A class defined inside another class, that may access its private data. (Nested is the opposite of top-level.) Variants of nested classes static nested class no references to the outer class (non-static) instance any visibility specifier Inner class: non-static nested class can reference the outer class instance any visibility specifier Anonymous (inner) class: inner class without a name, defined in the middle of a method or initialization block no visibility specifiers allowed Local (inner) class: inner class with a name, defined in the middle of a method or initialization block no visibility specifiers allowed 13

Static nested classes A static nested class: can only reference static members of the enclosing class (besides its own arguments and locals) can include both static and non-static members it is used as a top-level class; nesting affects naming not behavior public class Nested { static class SN { static int m() { return 5; int n() { return 3; // Client code: int y = Nested.SN.m(); // 5 Nested.SN sn = new Nested.SN(); int x = sn.n(); // 3 14

Inner classes An inner (non-static nested) class: can reference the outer class instance all instances of the inner class refer to the instance of the containing class used to create them can be instantiated only through an instance of the outer class (cannot include static members, except constants) public class Nested { int a; class I { int n() { return 3; int m() { return a; // Client code: Nested n = new Nested(); Nested.I i = n.new I(); int x = i.n(); // 3 n.a = 5; Nested.I j = n.new I(); int y = j.m(); // 5 == i.m() 15

Anonymous and local classes An anonymous or local (inner) class: can reference the outer class members all instances of the inner class refer to the instance of the containing class used to create them but cannot access local variables of its enclosing class (except constants) cannot include static members, except constants Anonymous classes have essentially the same restrictions as local classes but have no name typically used to wrap operations into an object that can be passed around this usage will be superseded by lambda expressions in Java 8 and later. 16

Anonymous inner class example public void start(int num) { // ActionListener is an interface that includes // a method actionperformed ActionListener listener = new ActionListener() // anonymous inner class starts here { public void actionperformed(actionevent e) { // reaction code here ; // anonymous inner class ends here // other code here Which design pattern does this example suggest? 17

Anonymous inner class example public void start(int num) { // ActionListener is an interface that includes // a method actionperformed ActionListener listener = new ActionListener() // anonymous inner class starts here { public void actionperformed(actionevent e) { // reaction code here ; // anonymous inner class ends here // other code here This is an instance of the observer design pattern 18

Method overloading Using the same name with different argument list list can differ in length, argument type, or both Example: constructors Method signature: name + arguments list The return type is not part of the signature Tip: overloading may reduce readability: don t abuse it 19

Method overloading with subtypes When a method name is overloaded with argument types that are related by inheritance, method resolution selects the closest available type. Example: Student is a subtype of Person class X { // v1 void foo (Person p) { // v2 void foo (Student p) { X x = new X(); x.foo(new Person()); // Executes v1 x.foo(new Student()); // Executes v2 20

Method overloading with subtypes When a method name is overloaded with argument types that are related by inheritance, method resolution selects the closest available type. Example: Student is a subtype of Person class Y { void foo (Person p) {... class Z { void foo (Student p) {... Y y = new Y(); y.foo(new Person()); // OK y.foo(new Student()); // OK Z z = new Z(); z.foo(new Person()); // Error z.foo(new Student()); // OK 21

Operator overloading No custom operator overloading is possible Only + for String is overloaded at language level System.out.println( Custom operator overloading + would have been nice ) 22

Method argument passing All the primitive types are passed by value Inside the method body we work with a local copy We return information using the return keyword (Object) Reference types are passed by value too, but: What is passed by value is the reference (i.e., an object address) Consequently, a method can change the state of the object attached to the actual arguments through the reference 23

Variable number of arguments To pass a variable number of arguments to a method: Use a collection (including arrays) From Java 5.0: varargs arguments... public void write(string... somestrings) { for (String astring : somestrings) { System.out.println(aString); This is just syntactic sugar for an array You can pass an array as actual The varargs argument must be the only one of its kind and the last one in the signature 24

Block initializers (a.k.a. initialization blocks) Similar to anonymous method bodies without signature and return type, only curly brackets and possibly the static modifier The code within them is executed during initialization Can be static or non-static Useful to perform some computation before the constructors are invoked Factor out code common to multiple constructors Initialize final static variables 25

Finalizer methods The Object class includes a method: protected void finalize() which can be overridden in any class. The finalize method is called just before garbage collection May never be called, if an object is not collected No real-time guarantee that the object is collected right after finalize is executed What s for: do some final clean-up upon object disposal E.g.: resources not properly released beforehand It is not meant for general release of resources Files and other I/O resources have close/destroy methods, which should be called explicitly 26

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Inheritance, polymorphism, and dynamic dispatching

Inheritance We can explicitly extend from one class only Otherwise, every class implicitly extends Object Public and protected inherited fields and methods are available in the descendant. Package-visible (no visibility specifiers) inherited members are visible only in descendants within the same package. 28

Overriding and dynamic dispatching Overriding: method redefinition in a subclass Overriding rule: (before Java 5.0) overriding method must have the same signature and return type as in the superclass (from Java 5.0) overriding method must have the same signature as in the superclass and a covariant return type of the superclass Annotation @Override avoids compiler warning Dynamic dispatching applies The keyword final prevents overriding in subclasses Overriding cannot reduce the visibility of a method e.g.: from public to private No overriding for static methods 29

Covariant return types example In Java 5.0 the return type of an overridden method can be a subtype of the base method s return type. class Account {... class SavingsAccount extends Account {... class AccountManager { public Account getaccount() {... class SavingsAccountManager extends AccountManager { public SavingsAccount getaccount() {... 30

Casting and Polymorphism Casting is C++/Java/C# jargon to denote polymorphic assignments. Let S be an ancestor of T (that is, T * S) Upcasting: an object of type T is attached to a reference of type S Downcasting: an object of type S is attached to a reference of type T class Vehicle; class Car extends Vehicle; Vehicle v =(Vehicle)new Car(); // upcasting Car c = (Car)new Vehicle(); // downcasting 31

Casting in Java Upcasting is implicit For primitive types, upcasting means assigning a smaller type to a larger compatible type byte to short to int to long to float to double (long to float may actually lose precision) char to int For reference types, upcasting means assigning a subtype to a supertype, that is: a subclass to superclass an implementation of an interface X to that interface X an interface X to the implementation of an ancestor of X Downcasting must be explicit can raise runtime exceptions if it turns out to be impossible No casts are allowed for reference types outside the inheritance hierarchy 32

The instanceof keyword The instanceof keyword performs runtime checking of the dynamic type of a reference variable Syntax: avariable instanceof atype Is the object attached to avariable compatible with atype? Compatible means of atype or one of its subtypes 33

Shadowing Variables with the same name and different (but overlapping) scopes: A local variable shadows an attribute with the same name: use this to access the attribute A subclass attribute shadows a superclass attribute with the same name Polymorphism does not apply if a reference is superclass type and attached object is subclass type, the superclass variable is used Tip: avoid if possible (it may decrease readability) 34

The final modifier final class Cannot be inherited from final attribute, argument, or local variable It s a constant: cannot be redefined and must be initialized (If it s a reference: the object state can change) final static attributes can only be initialized by block initializers final (non-static) attributes can be set only once, and must be set by every constructor of the class (whenever initializers haven t already set them). Style tip: constant names are capitalized final method Cannot be overridden 35

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer The object creation process

The keyword super Enables invocation of a superclass method from within an overriding method in a subclass Can be used to explicitly invoke a constructor of the superclass (see next example) 37

Chained constructors Any constructor implicitly starts by executing the argumentless constructor of the parent class, unless: A specific constructor of the superclass is invoked using super(...) Another specific constructor of the same class is invoked using this(...) If used, super(...) or this(...) must be the first instruction 38

Chained constructors: example public class CreatureCard extends Card { int value; public CreatureCard(String name){ super(name); // class-specific initializations value = 7; public CreatureCard(int value){ this( Big Monster ); // class-specific initializations this.value = value; 39

Object creation process MyClass obj = new MyClass(); (static members are initialized before) new allocates memory for a MyClass instance (all attributes, including inherited ones) initializes all attributes to default values If constructor references super (explicitly or by default): 1.Recursive call to constructor of superclass 2.Execute MyClass s initializers in their textual order 3.Execute constructor body If constructor references this (another constructor X): 1. Recursive call to other constructor X 2. Execute rest of originally called constructor body 40

Object creation process: example public class Person { int age = 1; public class Student extends Person { { age = 6; double gpa = age/2; public Student() { gpa += 1.0; Person p1 = new Person(); // age = 1 Person p2 = new Student(); // age = 6, gpa = 4.0 41