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

Similar documents
Programming Language Concepts: Lecture 2

CLASS DESIGN. Objectives MODULE 4

INHERITANCE. Spring 2019

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Inheritance (Part 5) Odds and ends

Programming overview

Java Fundamentals (II)

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

Inheritance (Part 2) Notes Chapter 6

Programming Language Concepts: Lecture 2

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

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

Java Object Oriented Design. CSC207 Fall 2014

C12a: The Object Superclass and Selected Methods

CS260 Intro to Java & Android 03.Java Language Basics

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Inheritance. Transitivity

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

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

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

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

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

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

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

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

Java Magistère BFA

More about inheritance

Super-Classes and sub-classes

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

Comp 249 Programming Methodology

Example: Count of Points

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

Object-Oriented Concepts

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

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

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

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

Rules and syntax for inheritance. The boring stuff

Inheritance -- Introduction

CS313D: ADVANCED PROGRAMMING LANGUAGE

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

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

CIS 110: Introduction to computer programming

AP CS Unit 6: Inheritance Notes

Overriding Variables: Shadowing

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

CS-202 Introduction to Object Oriented Programming

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Inheritance Motivation

CMSC 132: Object-Oriented Programming II

Instance Members and Static Members

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Lecture 2: Java & Javadoc

Chapter 5. Inheritance

C# Programming for Developers Course Labs Contents

Arrays Classes & Methods, Inheritance

Computer Science 210: Data Structures

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

What is Inheritance?

Declarations and Access Control SCJP tips

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Abstract Classes and Interfaces

Class, Variable, Constructor, Object, Method Questions

Distributed Systems Recitation 1. Tamim Jabban

COP 3330 Final Exam Review

JAVA MOCK TEST JAVA MOCK TEST II

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

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Lecture 18 CSE11 Fall 2013 Inheritance

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

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

Introduction to Inheritance

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

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

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

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

1 Shyam sir JAVA Notes

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Accelerating Information Technology Innovation

Inheritance and Polymorphism

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

PROGRAMMING LANGUAGE 2

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and Polymorphism

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

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

Introduction to Programming Using Java (98-388)

Chapter 7. Inheritance

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

Objects and Classes. Objects and Classes

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS Programming I: Inheritance

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Making New instances of Classes

Transcription:

1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type Argument list Then the new method is said to override the old one. So, what is the objective of subclass? Example Public class Employee protected String name; protected double salary; protected Date birthdate; public String getdetails() return Name: + name + \n + Salary: + salary; public class Manager extends Employee protected String department; Public String getdetails() return Name: + name + \n + Salary: + salary + "\n" + Manager of: + department; Page 1

The Manager class has a getdetails method by definition because it inherits one from the Employee class. However, the original has been replaced, or overridden by the child class version. 2. Overridden Methods Cannot Be Less Accessible Public class Parent Public void dosomething() Public class Child extends Parent Private void dosomething() // illegal public class UseBoth public void dootherthing() Parent p1 = new Parent(); Parent p2 = new Child(); p1.dosomething(); p2.dosomething(); The Java programming language semantics dictate that p2.method () results in the child version of the method being executed, because the method is declared private, p2 ( declared as parent ) cannot access it, this the semantics language is violated 3. Invoking Overridden Methods A subclass method may invoke a superclass method using the super keyword: The keyword super is used in a class to refer to its superclass. Page 2

The keyword super is used to refer to the members of superclass, both data attributes and methods. Behavior invoked does not have to be in the superclass; it can be further up in the hierarchy. public class Employee private String name; private double salary; private Date birthdate; public String getdetails() return "Name: " + name + "\nsalary: " + salary; public class Manager extends Employee private String department; public String getdetails() // call parent method return super.getdetails() \ndepartment: " + department; The Object Class The object class is the root of all classes in the Java technology programming language. If a class is declared with no extends clause, then the compiler adds implicitly the code extend Object to the declaration for example Public class Employee // code here Is equivalent to Public class Employee extends Object // code goes here Page 3

Two important methods are: equals tostring 1. The equals Method The == operator determines if two references are identical to each other (that is, refer to the same object). The equals method determines if objects are equal but not necessarily identical. The Object implementation of the equals method uses the == operator User classes can override the equals method to implement a domain-specific test for equality. Note: You should override the hashcode method if you override the equals method. Example public class MyDate1 private int day; private int month; private int year; // constructor public MyDate1(int day2, int month2, int year2) this.day = day2; this.month = month2; this.year = year2; Page 4

public boolean equals(object o) boolean result = false; if ( (o!= null) && (o instanceof MyDate1) ) MyDate1 d = (MyDate1) o; if ( (day == d.day) && (month == d.month)&& (year == d.year) ) result = true; // end if // end if return result; // end method public int hashcode() return (day ^ month ^ year); // end method hashcode public class TestEquals public static void main(string[] args) MyDate1 date1 = new MyDate1(10, 10, 1976); MyDate1 date2 = new MyDate1(10, 10, 1976); if ( date1 == date2 ) System.out.println("date1 is identical to date2"); else System.out.println("date1 is not identical to date2"); Page 5

if ( date1.equals(date2) ) System.out.println("date1 is equal to date2"); else System.out.println("date1 is not equal to date2"); System.out.println("set date2 = date1;"); date2 = date1; if ( date1 == date2 ) System.out.println("date1 is identical to date2"); else System.out.println("date1 is not identical to date2"); This example generates the following output: date1 is not identical to date2 date1 is equal to date2 set date2 = date1; date1 is identical to date2 2. The tostring method It has the following characteristics: This method converts an object to a String. Use this method during string concatenation. Page 6

Override this method to provide information about a userdefined object in readable format. class Bank String n; String add; int an; int bal; int dep; public Bank(String n,string add,int an,int bal) this.add=add;this.bal=bal; this.an=an;this.n=n; public String tostring() return "Name of the customer.:" + this.n+",, "+"Address of the customer.:"+this.add +",, "+"A/c no..:"+this.an+",, " +"Balance in A/c..:"+this.bal; Q/ how we convert primitive types to a String using tosting method?? Page 7