Object-Oriented Concepts

Similar documents
Inheritance (Part 5) Odds and ends

Practice for Chapter 11

Chapter 5 Object-Oriented Programming

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

Java Fundamentals (II)

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Chapter 5. Inheritance

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

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

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

Full file at Chapter 2 - Inheritance and Exception Handling

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

AP CS Unit 6: Inheritance Notes

CS-202 Introduction to Object Oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

PROGRAMMING LANGUAGE 2

Lecture 4: Extending Classes. Concept

Arrays Classes & Methods, Inheritance

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Inheritance and Polymorphism

Inheritance. The Java Platform Class Hierarchy

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

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

ECE 122. Engineering Problem Solving with Java

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Programming overview

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Example: Count of Points

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Object Oriented Programming. Java-Lecture 11 Polymorphism

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance (Part 2) Notes Chapter 6

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance -- Introduction

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Class, Variable, Constructor, Object, Method Questions

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

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Abstract Classes and Interfaces

Lecture 18 CSE11 Fall 2013 Inheritance

Questions Answer Key Questions Answer Key Questions Answer Key

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Instance Members and Static Members

Inheritance, Polymorphism, and Interfaces

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Lecture 2: Java & Javadoc

interface MyAnno interface str( ) val( )

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Exercise: Singleton 1

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Java Magistère BFA

What is Inheritance?

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

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CSC 1214: Object-Oriented Programming

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

Programming II (CS300)

Inheritance and Polymorphism

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

Polymorphism and Inheritance

Example: Count of Points

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Object Orientated Programming Details COMP360

CS111: PROGRAMMING LANGUAGE II

2. (True/False) All methods in an interface must be declared public.

Building custom components IAT351

Chapter 14 Abstract Classes and Interfaces

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

Questions Answer Key Questions Answer Key Questions Answer Key

First IS-A Relationship: Inheritance

More about inheritance

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CLASS DESIGN. Objectives MODULE 4

Introduction to Computation and Problem Solving

Tutorial 8 Date: 15/04/2014

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

VIRTUAL FUNCTIONS Chapter 10

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.

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)

1.00 Lecture 13. Inheritance

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

Inheritance and object compatibility

CS260 Intro to Java & Android 03.Java Language Basics

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

1 Shyam sir JAVA Notes

INSTRUCTIONS TO CANDIDATES

Inheritance. Transitivity

INHERITANCE. Spring 2019

More Relationships Between Classes

ECE 122. Engineering Problem Solving with Java

1.00 Lecture 14. Exercise: Plants

Transcription:

JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1

Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing and Extending Interfaces with Default Methods Abstract Classes 2

Inheritance Definition: A subclass is a class that extends another class. A subclass inherits state and behavior from all its ancestor. The superclass refers to a direct ancestor. Subclass inherits all, but private superclasses members public class SuperClass { public class SubClass extends SuperClass { 3

Overriding Definition: Replacing the superclass s implementation with a new method in a subclass is called overriding. The signature should be identical. Only accessible non-static method can be overridden. Access modifier could be different in overridden method as long as the subclass modifier is less restrictive than the superclass. A subclass can change whether a parameter in an overridden method is final ( final is not part of method signature). Fields cannot be overridden; they can only be hidden. super acts as a reference accessing fields and method of superclass. Ex: super.superclassfield; 4

Overriding and Hiding - Example class Base { public String s = X ; public void show() { System.out.println(s); class Extended extends Base { public String s = Y ; public void show() { System.out.println(s); public static void main(string[] args) { Extended e = new Extended(); Base b = e; b.show(); e.show(); System.out.println(b.s + + e.s); Results: Y Y X Y When invoke a method on an object, the actual class of the object governs which implementation is used. When access a field the declared type of the referenced is used. 5

Final Methods and Classes A method could be declared as final A final method cannot be overridden. A class could be declared as final A final class cannot be subclassed. Example: Immutable class like String class 6

Implementing / Extending Implementing Interface I interface I { void m(); class A implements I { void m1() { Extending Interface I interface J extends I { void m2(int i); class A implements J { void m1() { void m2(int i) { A B C Interface accepts multiple inheritance interface X extends A, B, C { X 7

Default and Static Methods Interface could contain Static Methods and Default Methods One can add new methods to an old interface, without breaking old code interface I { void m(); class X implements I { I Evolving the I Interface interface I { void m(); default String n(int k){ if (k % 2) return It is OK ; class Y implements I { X I Y 8

Abstract Methods / Classes Abstract Method is a method without implementation absract void movepoint(int deltax, int deltay); Abstract Class is a class with at least an abstract method public abstract class X { //fields //other methods absract void movepoint(int deltax, int deltay); Evolving the I Interface interface I { void m(); default String n(int k){ if (k % 2) return It is OK ; class Y implements I { I Y 9

Extending Interfaces - revisited Extending an interface with default methods Three options: 1. Ignore the default methods inherit the default methods 2. Redeclare the default method (makes the method an abstract method) 3. Redefine the default method overrides it. Interface declaration can contain: 4. Method signatures 5. Default methods 6. Static methods 7. Constant definitions 10

Annotations Annotation does not affect the program semantics Annotations are used by development tools to generate new artifacts or to check the properties of class / methods, etc. Previous annotation were defined in JavaDoc such as: @author @version Annotation types are imported in the same fashion as classes and interfaces 11

Annotation Example and Use Example of using the annotation @Override public class Example { @Override public int hashcode { return tostring().hashcode(); Annotation can be used anywhere you use a type (starting with Java SE 8) @NotNull String str; Annotation type definitions public @interface Preliminary {//Marker annotation public @interface Copyright { String value(); //Single member annotation public @interface Name { String first(); String last(); 12