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

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

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

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

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

CS260 Intro to Java & Android 03.Java Language Basics

1 Shyam sir JAVA Notes

INHERITANCE. Spring 2019

CMSC 132: Object-Oriented Programming II

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Java Magistère BFA

Java Strings Java, winter semester

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Java: introduction to object-oriented features

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

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

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

EMBEDDED SYSTEMS PROGRAMMING More About Languages

Inheritance. Transitivity

Java Fundamentals (II)

Java Exceptions Java, winter semester

Abstract Classes and Interfaces

Class, Variable, Constructor, Object, Method Questions

What is Inheritance?

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Points To Remember for SCJP

Declarations and Access Control SCJP tips

Big software. code reuse: The practice of writing program code once and using it in many contexts.

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

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

1.Which four options describe the correct default values for array elements of the types indicated?

The class Object. Lecture CS1122 Summer 2008

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

CLASS DESIGN. Objectives MODULE 4

Introduction to Inheritance

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Array. Prepared By - Rifat Shahriyar

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

Selected Java Topics

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Concurrent Programming using Threads

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Inheritance. The Java Platform Class Hierarchy

Java Enum Java, winter semester ,2018 1

ECE 122. Engineering Problem Solving with Java

public static boolean isoutside(int min, int max, int value)

Selected Questions from by Nageshwara Rao

Lecture 4: Extending Classes. Concept

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

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

Polymorphism. return a.doublevalue() + b.doublevalue();

OCA Java SE 7 Programmer I Certification Guide By Mela Gupta. Arrays

The Java Programming Language

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

Chapter 1: Introduction to Computers, Programs, and Java

Course information. Petr Hnětynka 2/2 Zk/Z

Objects and Iterators

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

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

Faculty of Science FINAL EXAMINATION

CSCI 201L Written Exam #1 Fall % of course grade

Inheritance (Part 2) Notes Chapter 6

Binghamton University. CS-140 Fall Dynamic Types

Methods Common to all Classes

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

TeenCoder : Java Programming (ISBN )

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

CISC370: Inheritance

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Oracle 1Z Java Certified Programmer. Download Full Version :

CMSC 341. Nilanjan Banerjee

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

STRUCTURING OF PROGRAM

Inheritance (Part 5) Odds and ends

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

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

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

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Brief Summary of Java

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CSCI 201L Written Exam #1 Fall % of course grade

G Programming Languages - Fall 2012

Super-Classes and sub-classes

JAVA MOCK TEST JAVA MOCK TEST II

CS 251 Intermediate Programming Inheritance

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

Java Overview An introduction to the Java Programming Language

Fundamentals of Object Oriented Programming

Introduction to Java

Rules and syntax for inheritance. The boring stuff

Transcription:

JAVA Classes

Class definition complete definition [public] [abstract] [final] class Name [extends Parent] [impelements ListOfInterfaces] {... // class body public public class abstract no instance can be created final class cannot be extended 2

Constructor constructor object initialization declaration the same name as the class no return type modifier only visibility several constructors with different arguments selected by arguments of new class MyClass { int value; public MyClass() { value = 10; public MyClass(int v) { value = v; 3

Object removal garbage collector finalize() method present in every class called before object's removal it is not a destructor like in other languages not known when it is called calling is not guaranteed object need not be removed by garbage collecting e.g. at the end of the program calls of finalize() are not chained To simplify you can forget that finalize() exists 4

Initialization of fields in constructor or direct class MyClass { int a = 5; float b = 1.2; MyClass2 c = new MyClass2(); int d = fn(); int e = g(f); // error! int f = 4;... 5

Initialization: static just once before first access or before first instance of a class is created direct static int a = 1; static initializer class MyClass { static int a; static { a = 10;... 6

Initialization: "non-static static" similar to static initializer necessary for initialization of anonymous inner classes class MyClass { int a; int b; { a = 5; b = 10;... 7

Classes: inheritance parent specification extends ParentName single inheritance single parent only class java.lang.object each class inherits from this class directly or indirectly the only class without parent multiple inheritance only via Interfaces 8

Polymorphism polymorphism ~ inheritance cast automated child to parent class A { /*...*/ class B extends A { /*...*/ A a = new B(); Object o = a; B b = (B) o; 9

Polymorphism constructor constructor of the parent super() other constructor of the same object this() calling other constructors only as the first statement and just once parent's constructor is called always even if not explicitly called exception this() class without constructor declared has default constructor calls super() only 10

java.lang.object Object clone() boolean equals(object obj) void finalize() Class<?> getclass() int hashcode() void notify() void notifyall() String tostring() void wait() void wait(long timeout) void wait(long timeout, int nanos) 11

Classes: visibility of members must be specified for each member fields and methods public from everywhere (if the class is also visible) protected from the same package and children private just from the same class without a visibility modifier from the same package holds within a single module 12

Classes: other modifiers final field constant must have initializer after initialization cannot be changed method cannot be overridden in children transient field does not belong to a persistent state of the object volatile field non-synchronized access of multiple threads no optimization can be perfomed 13

Classes: modifiers of methods abstract no method body the class must be also abstract no instance can be created method body semicolon synchronized calling thread must obtain a lock on the called object (or the class in the case the method is static) native native method implementation directly in native code for a particular platform (as an external library) method body semicolon static see the previous lecture 14

Classes: method modifiers no modifier virtual all methods are virtual static methods are not virtual public class A { public void foo() { System.out.print( A ); public class B extends A { public void foo() { System.out.print( B );... A a = new B(); a.foo(); // prints out B public class As { public static void foo() { System.out.print( A ); public class Bs extends As { public static void foo() { System.out.print( B );... A a = new B(); a.foo(); // prints out A 15

Static methods static methods are called on a class do not belong to any object class As { public static void foo() {... As.foo(); they can be called on an object (a class instance); but in reality only a type of the reference is taken value of the object is ignored type (and thus a method to be called) is determined at compile time see the previous slide 16

this reference to the object of the executed method can be used in methods and initializers only public class MyClass { private int a; public MyClass(int a) { this.a = a; 17

super access to members of the direct parent in the case S is direct parent of C ((S) this).name ~ super.name super.super cannot be used class T1 { int x = 1; class T2 extends T1 { int x = 2; class T3 extends T2 { int x = 3; void test() { System.out.println(x); // 3 System.out.println(super.x); // 2 System.out.println(((T2)this).x); // 2 System.out.println(((T1)this).x); // 1 18

super super can be used with methods too WARNING casting this does not work a code can be compiled but the same method will be called recursively class TX1 { public void foo() { /* */ class TX2 extends TX1 { public void foo() { /* */ public class TX3 extends TX2 { public void foo() { ((TX1) this).foo(); System.out.println("TX3.foo()"); 19

Java Interfaces 20

Interface only interface no implementation since Java 8, there can be an implementation can contain method headers fields inner interfaces public interface Iterator { boolean hasnext(); Object next(); void remove(); 21

Interface: fields implicitly they are public, static and final must be initialized super and this cannot be used in initialization public interface Iface { int a = 5; String s = "hello"; 22

Interface: methods without implementation implicitly abstract and public cannot be synchronized native final default methods since Java 8 contains implementation intended for extending interfaces static methods since Java 8 the same as the static methods in classes 23

Interface: inheritance multiple inheritance interface Iface1 {... interface Iface2 {... interface Iface3 extends Iface1, Iface2 {... 24

Classes and interfaces classes implement interfaces public interface Colorable { void setcolor(int c); int getcolor(); public class Point { int x,y; public class ColoredPoint extends Point implements Colorable { int color; public void setcolor(int c) { color = c; public int getcolor() { return color; Colorable c = new ColoredPoint(); 25

Classes and interfaces a class must implement all methods of its interfaces except the default methods not true for abstract classes a single method in a class can implement several interfaces interface A { void log(string msg); interface B { void log(string msg); public class C implements A, B { public void log(string msg) { System.out.println(msg); 26

Interfaces and default methods the implementation in a class has always precedence over the implementation in interfaces if implementing two interfaces with the same default method, then the method has to be implemented in the class otherwise the class cannot be compiled interface If1 { default void foo() { class Mixed implements If1, If2 { interface If2 { default void foo() { cannot be compiled 27

Interfaces and default methods it is forbidden to define a default method for a public method from java.lang.object interface Iface { public default boolean equals(object obj) { return false; the implementation in a class has always precedence over the implementation in interfaces even an inherited one 28

Interfaces and default methods interface If1 { default void foo() { System.out.println( interface ); class A { public void foo() { System.out.println( class ); class B extends A implements If1 { public static void main(string[] args) { B b = new B(); b.foo(); // -> class 29

Java Arrays 30

Array definition array ~ object variable ~ reference int[] a; // array short[][] b; // 2-dimensional array Object[] c, // array d; // array long e, // non-array f[]; // array 31

Array initialization "static" int[] a = { 1, 2, 3, 4, 5 ; char[] c = { 'h', 'e', 'l', 'l', 'o' ; String[] s = { "hello", "bye" ; int[][] d = { { 1, 2, { 3, 4 ; 32

Array initialization dynamic int[] array = new int [10]; float[][] matrix = new float[3][3]; just several dimensions can be specified but first ones empty brackets for the rest float[][] matrix = new float[3][]; for (int i=0;i<3;i++) matrix[i] = new float [3]; // wrong int[][][][] a = new int[3][][3][]; 33

Array initialization "non-rectangular" array int a[][] = { {1, 2, {1, 2, 3, {1, 2, 3, 4, 5 ; int b[][] = new int [3][]; for (int i=0; i<3; i++) b[i] = new int [i+1]; 34

Array initialization no constructor is called elements in the created array (using new) default values references null int 0... expressions in array creation (new) fully evaluated from left int i = 4; int ia[][] = new int[i][i=3]; // array 4x3 35

Access to array array[index] indexes always 0..length-1 bounds always checked cannot be switched off exception thrown for out of bounds access ArrayIndexOutOfBoundsException array length field length int[] a = { 1, 2, 3 ; for (int i=0; i < a.length; i++) {... 36

Array ~ object int[] intarray = new int [100]; String[] strarray = new String [100]; array is object Object o1 = strarray; Object o2 = intarray; // OK // OK but Object[] oa1 = strarray; // OK Object[] oa2 = intarray; // error 37

Array ~ object Object[] oa = new Object [2]; oa[0] = new String("hello"); oa[1] = new String("world"); String[] sa1 = oa; // error String[] sa2 = (String[]) oa; // error too // can be compiled but run-time error 38

Java, winter Slides semester version 2017 J02.en.2017.02 This slides are licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 39