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

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

Java Object Oriented Design. CSC207 Fall 2014

Chapter 5 Object-Oriented Programming

CLASS DESIGN. Objectives MODULE 4

CS-202 Introduction to Object Oriented Programming

What is Inheritance?

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Introduction to Programming Using Java (98-388)

Example: Count of Points

OVERRIDING. 7/11/2015 Budditha Hettige 82

CMSC 132: Object-Oriented Programming II

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Practice for Chapter 11

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

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

CS Programming I: Inheritance

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

Programming II (CS300)

Java Magistère BFA

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Exercise: Singleton 1

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Inheritance and Polymorphism

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

Instance Members and Static Members

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

Object Oriented Programming: Based on slides from Skrien Chapter 2

Introduction to Inheritance

Declarations and Access Control SCJP tips

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

15CS45 : OBJECT ORIENTED CONCEPTS

CS260 Intro to Java & Android 03.Java Language Basics

Java: introduction to object-oriented features

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

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

Java Fundamentals (II)

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Inheritance. Transitivity

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

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

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

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

Logistics. Final Exam on Friday at 3pm in CHEM 102

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

More Relationships Between Classes

Rules and syntax for inheritance. The boring stuff

Inheritance (Outsource: )

VIRTUAL FUNCTIONS Chapter 10

Informatik II Tutorial 6. Subho Shankar Basu

Object-Oriented Programming More Inheritance

1 Shyam sir JAVA Notes

First IS-A Relationship: Inheritance

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Chapter 5. Inheritance

Inheritance and Polymorphism. CS180 Fall 2007

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

Implements vs. Extends When Defining a Class

COMP 110/L Lecture 20. Kyle Dewey

What are the characteristics of Object Oriented programming language?

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

More on Objects in JAVA TM

Overriding Variables: Shadowing

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

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Inheritance -- Introduction

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Outline. More optimizations for our interpreter. Types for objects

PROGRAMMING LANGUAGE 2

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Inheritance and object compatibility

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Informatik II (D-ITET) Tutorial 6

Super-Classes and sub-classes

Class, Variable, Constructor, Object, Method Questions

COP 3330 Final Exam Review

G Programming Languages - Fall 2012

Lecture 4: Extending Classes. Concept

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

COE318 Lecture Notes Week 8 (Oct 24, 2011)

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)

CMSC 132: Object-Oriented Programming II

QUESTIONS FOR AVERAGE BLOOMERS

Type Hierarchy. Lecture 6: OOP, autumn 2003

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

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

Inheritance & Polymorphism

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Object Model. Object Oriented Programming Spring 2015

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

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

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

Chapter 14 Abstract Classes and Interfaces

INHERITANCE. Spring 2019

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Compaq Interview Questions And Answers

CPS 506 Comparative Programming Languages. Programming Language

Example: Count of Points

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Transcription:

Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar + 1; //ok now Eg. void mymethod() { int foo; try { foo = getres(); catch (Exception e) { int bar = foo; //compile error since foo //may never have been initialized Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Eg. int i; someobj obj = new someobj(); mymethod(i, obj); void mymethod (int j, someobj o) {

Method Overloading There can be multiple methods defined in a class with the same name. The compiler picks the correct one based on the number and type of arguments passed to the method (also called parametric polymorphism). There is no programmer specified operator overloading in Java. (Except the + operator is overloaded for string concatenation). Constructors Used to initialize class instances (i.e. create objects). Constructors can be overloaded as shown in the following example. E.g. class car { String model; int doors; car (String m, int d) { //constructor 1 model = m; doors = d; E.g. class car { car (String m) { //constructor 2 this(m, 4); //calls the first //constructor final int def_doors = 4; car (String m) { //constructor this(m, def_doors); //compile error

Object Destruction Garbage collection removes objects, which no longer have references to them. It then reclaims the memory space. Subclassing and Inheritance A class inherits variables and methods from its superclass and uses them as if they're declared within the subclass itself. E.g. class animal { float wt; void eat() { class mammal extends animal { //subclass of animal int heartrate; //inherits wt from animal void breathe() { //inherits eat() class cat extends mammal { //subclass of mammal boolean longhair; //inherits heartrate and wt void purr() { //inherits eat() and breathe() A subclass inherits all members of its superclass NOT designated as private. Instances (objects) of a subclass can be used anywhere instances (objects) of the superclass are allowed. E.g. cat simon = new cat(); //simon refs an object of subclass cat animal creature = simon; //object ref simon can be assigned to //object ref creature because cat is a subtype of animal

Shadowed Variables An instance var in a subclass can shadow (hide) an instance var of the same name in the parent class. e.g. class IntCalc { int sum; class DecimalCalc extends IntCalc { //methods defined in this class see the //integer var sum double sum; //methods defined in this subclass see // the float var sum However, both vars exist for a given instance (object) of DecimalCalc, and they can have independent values. Methods that DecimalCalc inherits from IntCalc see the integer var sum. To be able to reference the int var inherited from IntCalc from within a method in DecimalCalc, we need to use the super keyword as in: int s = super.sum; Overriding Methods vs. Overloaded Methods Overloaded methods are methods with the same name but a different number or different type of arguments within a class. A subclass can also define a method that has exactly the same signature (i.e. same arguments and return type) as a method in its superclass. The method in the subclass is said to override the method in the superclass (this is another form of polymorphism). Thus methods in the superclass are shadowed by methods in the subclass by using method overriding.

e.g. class animal eat() sleep() reproduce() cat simon class mammal extends animal eat() reproduce() reproduce() sleep() class cat extends mammal purr() sleep() purr() Overloaded methods are selected by compiler at compile-time. Overriden methods are selected by Java dynamically at runtime. You can explicitly declare methods that can't be overridden by using the final modifier; these methods are not subject to dynamic binding and so there is no performance penalty. A static method in a superclass can be overridden by another static method (only) in a subclass, as long as the original method was not declared final. Overridden methods must adhere to the throws clause of the parent method's signature. this refers to members of the current object and super refers to the members of the superclass. e.g. class animal { void eat (fooditem f){ class carnivore extends animal { void eat( fooditem f) { if (f instanceof plant) //complain else super.eat(f);

Casting Casts checked both at compile and run-time. Incompatible casts result in class CastException at run-time. Only casts between objects in the same inheritance hierarchy are legal. e.g. cat simon = new cat(); animal creature = simon; //Implicit cast simon = creature; //compile error, incompatible type simon = (cat) creature; //ok Object Construction e.g. class Person { String name1; Person(String name) { name1 = name; class Doctor extends Person { String spec; Doctor(String name, String specialty) { super(name); //calls constructor of superclass spec = specialty;