Object Orientated Programming Details COMP360

Similar documents
What is Inheritance?

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

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

Inheritance and Polymorphism

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

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Inheritance. Transitivity

Chapter 5. Inheritance

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Inheritance and Polymorphism

Inheritance (an intuitive description)

Practice for Chapter 11

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

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

Inheritance (continued) Inheritance

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

CS-202 Introduction to Object Oriented Programming

ECE 122. Engineering Problem Solving with Java

Instance Members and Static Members

CS Programming I: Inheritance

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

COMP 110/L Lecture 19. Kyle Dewey

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

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

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

Example: Count of Points

Chapter 5 Object-Oriented Programming

Object-Oriented Programming More Inheritance

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

More Relationships Between Classes

Chapter 11 Inheritance and Polymorphism

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Lecture Notes Chapter #9_b Inheritance & Polymorphism

C++ Important Questions with Answers

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

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

Inheritance (Part 5) Odds and ends

Java Object Oriented Design. CSC207 Fall 2014

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

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

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

Solutions to Sample JAC444 Midterm Test

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

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

CS1150 Principles of Computer Science Objects and Classes

About 1. Chapter 1: Getting started with oop 2. Remarks 2. Examples 2. Introduction 2. OOP Introduction 2. Intoduction 2. OOP Terminology 3.

Comp 249 Programming Methodology

Arrays Classes & Methods, Inheritance

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

Inheritance and Encapsulation. Amit Gupta

COMP 250: Java Object Oriented Programming

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

AP CS Unit 6: Inheritance Notes

Inheritance and Polymorphism

Classes and Inheritance Extending Classes, Chapter 5.2

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

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

Full file at Chapter 2 - Inheritance and Exception Handling

First IS-A Relationship: Inheritance

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

Object-Oriented Concepts

Inheritance -- Introduction

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

CHAPTER. CHAPTER CONTENTS 6.1 What Is Inheritance? Animation 6.12 Advanced Graphics (Optional) 6.14 Summary

Programming II (CS300)

COMP 110/L Lecture 20. Kyle Dewey

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

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

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

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

K.Yellaswamy Assistant Professor CMR College of Engineering & Technology

Example: Count of Points

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

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

Object-Oriented ProgrammingInheritance & Polymorphism

COMP 250 Fall inheritance Nov. 17, 2017

Java Fundamentals (II)

Binghamton University. CS-140 Fall Dynamic Types

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

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

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

CS 112 Introduction to Programming

Exercise: Singleton 1

C++ Inheritance and Encapsulation

Admin. q Admin and recap q Class inheritance

Inheritance (Outsource: )

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

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

Create a Java project named week10

Inheritance, polymorphism, interfaces

Accelerating Information Technology Innovation

Coverage of Part 2. A Brief Introduction to Java for C++ Programmers: Part 2. import: using packages

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Transcription:

Object Orientated Programming Details COMP360

The ancestor of every action is a thought. Ralph Waldo Emerson

Three Pillars of OO Programming Inheritance Encapsulation Polymorphism

Inheritance Inheritance allows a program to build upon existing classes and methods Child classes inherit or extend the methods and class data values of the parent class If kitten inherits from cat, then a kitten object is also a cat object Some languages require the programmer to have access to the source code of the parent class (i.e. C++) while others do not (i.e. Java)

Encapsulation and Data Abstraction Classes are described by their interface, that is, what they can do and how they are used The internal data and operation are hidden In this way if the internal operation is changed, programs using the class will not be impacted as long as the interface remains consistent

Accessing public Instance Values public class Car { double miles, gallons; public double mileage; // other data and method are not shown // in another program Car junker = new Car(); double mpg = junker.mileage;

Accessing Instance Values by get Method public class Car { double miles, gallons; private double mileage; public double getmileage() { return mileage; // other data and method are not shown // in another program Car junker = new Car(); double mpg = junker.getmileage();

Accessing Instance Values by a Method public class Car { double miles, gallons; public double getmileage() { return miles / gallons; // other data and method are not shown // in another program Car junker = new Car(); double mpg = junker.getmileage();

Polymorphism Polymorphic methods can be applied to arguments of different types, but behave differently depending on the type of the argument You can have multiple methods with the same name, but different number or type of parameters

Java Constructors The constructor method is called when an object is first created Constructors are called by other classes after the keyword new If a class does not have a constructor, Java assumes a constructor with no arguments that does nothing

Polymorphic Constructors A class may have multiple constructors with different number or type of arguments public Goat { public Goat( ) { public Goat(String cat) { public Goat(double dog) {

Removing the default If you create a constructor with parameters, then the default no parameter constructor does not work You must create a default no parameter constructor if you want one with other constructors

Missing Default Constructor public class Aardvark { public Aardvark(int ant) { in another class // This does not work Aardvark termite = new Aardvark();

Calling One Constructor from Another One constructor can call another constructor of the same class using the method this( ) When used as a method name, this is the constructor for this class

Constructors calling Constructors public class Vulture { public Vulture() { this("dead"); public Vulture(String possum) {

Calling Constructor of all Ancestors Constructing an instance of a class invokes all the superclasses constructors up the inheritance chain This is called constructor chaining class Animal class Mammal extends Animal class Dog extends Mammal If you create an object of the Dog class, Java will call the constructors of Animal, Mammal and Dog in that order

Constructors are not Inherited Unlike all other methods, constructors are not inherited by subclasses If a class does not have a constructor, the constructor of the super classes will be called in order

What is displayed? class Crow { public Crow() { this("crow "); System.out.print("caw "); public Crow(String starling) { System.out.print(starling); class Raven extends Crow { public Raven() { System.out.print("Raven "); public class ConOrder { public static void main(string[] x) { Raven bird = new Raven(); A. Raven B. caw Raven C. caw Crow Raven D. Crow caw Raven E. Raven Crow caw

Using the Keyword super The keyword super refers to the superclass of the class in which super appears This keyword can be used in two ways: To call a superclass constructor To call a superclass method 19

Superclass Constructor is super You must use the keyword super to call the superclass constructor Invoking a superclass constructor s name in a subclass causes a syntax error Java requires that the statement that uses the keyword super appear first in the constructor 20

super must be first public class Fish { public Fish(String dog) { System.out.println( dog ); public class Trout { public Trout( String cat ) { System.out.println("trout"); super( cat ); // Incorrect

super must be first public class Fish { public Fish(String dog) { System.out.println( dog ); public class Trout { public Trout( String cat ) { super( cat ); // correct System.out.println("trout");

Implicit Calls to Parent Constructors If a constructor does not call the super class constructor, Java will automatically call it public class Frog extends Amphibian { public Frog() { System.out.println("frog"); Is the same as public class Frog extends Amphibian { public Frog() { super( ); System.out.println("frog");

Implicit Constructor Call public class ImplicitCon { public static void main(string[] x) { Child kid = new Child(); class Parent { public Parent() { System.out.println("Parent"); class Child extends Parent { public Child() { System.out.println("Child"); Displays Parent child

Implicit Constructor Call public class ImplicitCon { public static void main(string[] x) { Child kid = new Child(); class Parent { public Parent() { System.out.println("Parent"); class Child extends Parent { // no Child constructor Displays Parent

Will this work? public class Apple extends Fruit { class Fruit { public Fruit(String name) { System.out.println("Fruit"+name); A. It should work fine B. Apple needs a no argument constructor C. Fruit needs a no argument constructor D. Both Apple and Fruit need a no argument constructor

Constructor Chaining public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 27

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); 1. Start from the main method public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 28

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); 2. Invoke Faculty constructor public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 29

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); 3. Invoke Employee s no-arg constructor class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 30

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); 4. Invoke Employee(String) constructor class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 31

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); 5. Invoke Person() constructor class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 32

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); 6. Execute println class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 33

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); 7. Execute println class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 34

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); 8. Execute println class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 35

Trace Execution public class Faculty extends Employee { public static void main(string[] args) { new Faculty(); public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); 9. Execute println class Employee extends Person { public Employee() { this("(2) Invoke Employee s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); public Employee(String s) { System.out.println(s); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 36

Overriding Methods in the Superclass A subclass inherits methods from a superclass Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass This is referred to as method overriding 37

Private Restriction A method can be overridden only if it is accessible Thus a private method cannot be overridden, because it is not accessible outside its own class If a method defined in a subclass is private in its superclass, the two methods are completely unrelated 38

Override A method is overridden when a child class creates a method of the same name and the same number and type of parameters The method of the child class will be used

Overload A method is overloaded when a child class creates a method of the same name but different type or number of parameters When a program calls the method, Java will select the method with the correct parameter type

Override public class OverRide { public static void main(string[] x) { Cat kitten = new Cat(); kitten.dog( 3 ); kitten.dog( 5.0 ); class Mammal { public void dog( double goat ) { System.out.println("Mammal "+goat); class Cat extends Mammal { public void dog( double cow ) { System.out.println("Cat "+cow);

Overload public class OverLoad { public static void main(string[] x) { Cat kitten = new Cat(); kitten.dog( 3 ); kitten.dog( 5.0 ); class Mammal { public void dog( double goat ) { System.out.println("Mammal "+goat); class Cat extends Mammal { public void dog( int cow ) { System.out.println("Cat "+cow);

What is displayed? public class OverRide { public static void main(string[] x) { Cat kitten = new Cat(); kitten.dog( 3 ); kitten.dog( 5.0 ); class Mammal { public void dog( double goat ) { System.out.print("Mammal "+goat); class Cat extends Mammal { public void dog( double cow ) { System.out.print("Cat "+cow); A. Mammal 5.0 Cat 3 B. Cat 3 Mammal 5.0 C. Cat 3.0 Cat 5.0 D.Mammal 3.0 Cat 5.0

What is displayed? public class OverLoad { public static void main(string[] x) { Cat kitten = new Cat(); kitten.dog( 3 ); kitten.dog( 5.0 ); class Mammal { public void dog( double goat ) { System.out.print("Mammal "+goat); class Cat extends Mammal { public void dog( int cow ) { System.out.print("Cat "+cow); A. Mammal 5.0 Cat 3.0 B. Cat 3 Mammal 5.0 C. Cat 3 Cat 5.0 D.Mammal 3.0 Cat 5.0

@override and @overload The @override and @overload statements are similar to comments They specify that the following method is overridden or overloaded If you put @override or @overload before a method that is not overridden or overloaded, you will get an error

Polymorphism Subtype polymorphism allows a function to be written to take an object of a certain class Parent, but also work correctly if passed an object that belongs to a class Child that is a subclass of Parent Ad Hoc polymorphism allows functions to be overloaded so that the same function can take parameters of different types and perform differently

Subtype Polymorphism Example abstract class Animal { abstract String talk(); class Cat extends Animal { String talk() { return "Meow!"; class Dog extends Animal { String talk() { return "Woof!"; void letshear(animal goat) { System.out.println( goat.talk() ); public static void main( ) { letshear( new Cat() ); letshear( new Dog() );

Three Pillars of OO Programming Inheritance Encapsulation Polymorphism