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

Similar documents
Comp 249 Programming Methodology

Chapter 7. Inheritance

Comp 249 Programming Methodology

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

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

9/10/2018 Programming Data Structures Inheritance

Inheritance and Polymorphism

Inheritance -- Introduction

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

Java Object Oriented Design. CSC207 Fall 2014

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

INHERITANCE. Spring 2019

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

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

CS-202 Introduction to Object Oriented Programming

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

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

What is Inheritance?

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

Java Fundamentals (II)

ITI Introduction to Computing II

5/24/12. Introduction to Polymorphism. Chapter 8. Late Binding. Introduction to Polymorphism. Late Binding. Polymorphism and Abstract Classes

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

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

ITI Introduction to Computing II

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

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Advanced Placement Computer Science. Inheritance and Polymorphism

Classes and Inheritance Extending Classes, Chapter 5.2

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

Computer Science 210: Data Structures

Arrays Classes & Methods, Inheritance

Inheritance (Part 5) Odds and ends

JAVA MOCK TEST JAVA MOCK TEST II

Practice for Chapter 11

Comp 249 Programming Methodology Chapter 8 - Polymorphism

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

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

Lecture 18 CSE11 Fall 2013 Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming in C# Inheritance and Polymorphism

CS260 Intro to Java & Android 03.Java Language Basics

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

Inheritance Motivation

CS111: PROGRAMMING LANGUAGE II

CMSC 132: Object-Oriented Programming II

Inheritance, Polymorphism, and Interfaces

Inheritance (continued) Inheritance

Inheritance, and Polymorphism.

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Inheritance (Part 2) Notes Chapter 6

CS111: PROGRAMMING LANGUAGE II

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

Class, Variable, Constructor, Object, Method Questions

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

Inheritance and Polymorphism

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

Programming overview

Lecture 2: Java & Javadoc

Chapter 11: Inheritance

EXERCISES SOFTWARE DEVELOPMENT I. 09 Objects: Inheritance, Polymorphism and Dynamic Binding 2018W

Introduction to Object-Oriented Programming

ECE 122. Engineering Problem Solving with Java

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

Programming II (CS300)

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

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

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

PROGRAMMING LANGUAGE 2

Chapter 5. Inheritance

What are the characteristics of Object Oriented programming language?

Inheritance and Interfaces

More about inheritance

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Object Orientated Programming Details COMP360

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

Java: introduction to object-oriented features

CLASS DESIGN. Objectives MODULE 4

Polymorphism and Inheritance

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

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Making New instances of Classes

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

C++ Important Questions with Answers

Inheritance. Chapter 7. Chapter 7 1

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

BBM 102 Introduction to Programming II

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

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Transcription:

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

Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation Inheritance Polymorphism

Inheritance Examples of inheritance Bicycle -> Mountain Bike, Racing bicycle Student -> high school student, undergraduate student, graduate student User -> Administrator, normal user, child account Employees -> hourly employees, salaried employees

A class hierarchy 2012 Pearson Addison-Wesley

Inheritance Inheritance allows a class (derived class) to use the properties and methods of another class (base class) The base class is first defined and compiled, and then more specialized version of the class (derived class) are defined by adding instance variables and methods The new class: derived class, subclass, or child class The original class: base class, superclass, or parent class

Inheritance A derived class automatically has all the instance variables and methods that the base class has. Can have additional instance variable and methods as well Codes can be reused without having to copy it into the definitions of the derived classes 2012 Pearson Addison-Wesley

Inheritance The Java Platform Class Hierarchy The object class (defined in the java.lang package) defines and implements behaviors commons to all classes. E.g., Hierarchy for package java.util http://docs.oracle.com/javase/7/docs/api/java/util/packa ge-tree.html

Inheritance Syntax public class ParentClass{ public class ChildClass extends ParentClass{

Inheritance // A class to display the attributes of the vehicle class Vehicle { String color; int speed; int size; void attributes() { System.out.println("Color : " + color); System.out.println("Speed : " + speed); System.out.println("Size : " + size); http://beginnersbook.com/2013/03/inheritance-in-java/

Inheritance // A subclass which extends for vehicle class Car extends Vehicle { int CC; int gears; void attributescar() { System.out.println("Color of Car : " + color); System.out.println("Speed of Car : " + speed); System.out.println("Size of Car : " + size); System.out.println("CC of Car : " + CC); System.out.println("No of gears of Car : " + gears); http://beginnersbook.com/2013/03/inheritance-in-java/

Inheritance public class Test { public static void main(string[] args) { Car b1 = new Car(); b1.color = "Blue"; b1.speed = 200 ; b1.size = 22; b1.cc = 1000; b1.gears = 5; b1.attributescar(); http://beginnersbook.com/2013/03/inheritance-in-java/

Inheritance // A class to display the attributes of the vehicle class Vehicle { String color; private int size; public int getsize() { return size; public void setsize(int i) { size = i; http://beginnersbook.com/2013/03/inheritance-in-java/

Inheritance // A subclass which extends for vehicle class Car extends Vehicle { int CC; int gears; int color; void attributescar() { // Error due to access violation //System.out.println("Size of Car : " + size); http://beginnersbook.com/2013/03/inheritance-in-java/

Inheritance public class Test { public static void main(string[] args) { Car b1 = new Car(); b1.color = 500; b1.setsize(22); b1.cc = 1000; b1.gears = 5; System.out.println("Color of Car : " + b1.color); System.out.println("Size of Car : " + b1.getsize()); System.out.println("CC of Car : " + b1.cc); System.out.println("No of gears of Car : " + b1.gears); http://beginnersbook.com/2013/03/inheritance-in-java/

Method Overriding A derived class inherits methods from the base class, but it can change or override an inherited method. In order to override a method definition, a new definition of the method is simply placed in the class definition, just like any other method that is added to the derived class 2012 Pearson Addison-Wesley

Method Overriding A method can only be written in Subclass The argument list should be exactly the same as that of the overridden method The return type should be the same or a subtype of the return type declared in the original overridden method in the super class http://crunchify.com/java-method-overriding-examples-and-concepts-overriding-rules/

Method Overriding class Company{ public void address(){ System.out.println( Address of Company ); Class ebay extends Company{ public void address(){ System.out.println( Address of ebay ); http://crunchify.com/java-method-overriding-examples-and-concepts-overriding-rules/

Method Overriding public class CompnayTest{ public static void main(string[] args){ // Company reference and object Company a = new Company(); // Company reference but ebay object Company b = new ebay(); a.address(); b.address(); http://crunchify.com/java-method-overriding-examples-and-concepts-overriding-rules/

Changing the Access Permission of an Overridden Method The access permission of an overridden method can be changed from private in the base class to public (or some other more permissive access) in the derived class However, the access permission of an overridden method can not be changed from public in the base class to a more restricted access permission in the derived class 2012 Pearson Addison-Wesley

Changing the Access Permission of an Overridden Method Given the following method header in a base case: private void dosomething() The following method header is valid in a derived class: public void dosomething() However, the opposite is not valid Given the following method header in a base case: public void dosomething() The following method header is not valid in a derived class: private void dosomething() 2012 Pearson Addison-Wesley

The final modifier If the modifier final is placed before the definition of a method, then that method may not be redefined in a derived class It the modifier final is placed before the definition of a class, then that class may not be used as a base class to derive other classes 2012 Pearson Addison-Wesley

The super constructor A derived class uses a constructor from the base class to initialize all the data inherited from the base class In order to invoke a constructor from the base class, it uses a special syntax: public derivedclass(int p1, int p2, double p3) { super(p1, p2); instancevariable = p3; In the above example, super(p1, p2); is a call to the base class constructor 2012 Pearson Addison-Wesley

The super constructor A call to the base class constructor can never use the name of the base class, but uses the keyword super instead A call to super must always be the first action taken in a constructor definition An instance variable cannot be used as an argument to super 2012 Pearson Addison-Wesley

The super constructor If a derived class constructor does not include an invocation of super, then the no-argument constructor of the base class will automatically be invoked This can result in an error if the base class has not defined a no-argument constructor Since the inherited instance variables should be initialized, and the base class constructor is designed to do that, then an explicit call to super should always be used 2012 Pearson Addison-Wesley

The this constructor Within the definition of a constructor for a class, this can be used as a name for invoking another constructor in the same class The same restrictions on how to use a call to super apply to the this constructor If it is necessary to include a call to both super and this, the call using this must be made first, and then the constructor that is called must call super as its first action 2012 Pearson Addison-Wesley

The this constructor Often, a no-argument constructor uses this to invoke an explicit-value constructor No-argument constructor (invokes explicit-value constructor using this and default arguments): public ClassName() { this(argument1, argument2); Explicit-value constructor (receives default values): public ClassName(type1 param1, type2 param2) {... 2012 Pearson Addison-Wesley

class Shape { private int length; private int breadth; // default Constructor Shape() { length = 0; breadth = 0; // Parameterized Constructor Shape(int len, int bdth) { length = len; breadth = bdth; void showattributes() { System.out.println("length : " + length); System.out.println("breadth : " + breadth); http://beginnersbook.com/2013/03/inheritance-in-java/

// A subclass which extends for shape class Rectangle extends Shape { private String type; // default Constructor Rectangle() { type = null; // Parameterized Constructor Rectangle(String ty, int len, int bdth) { super(len,bdth); type = ty; void showattributes() { // showattributes() of class Shape is called super.showattributes(); System.out.println("type : " + type); http://beginnersbook.com/2013/03/inheritance-in-java/

public class Test { public static void main(string args[]) { Rectangle rect = new Rectangle("Blue",5,7); // showattributes() in rectangle is called rect.showattributes(); http://beginnersbook.com/2013/03/inheritance-in-java/

Protected Access If a method or instance variable is modified by protected (rather than public or private), then it can be accessed by name Inside its own class definition Inside any class derived from it In the definition of any class in the same package The protected modifier provides very weak protection compared to the private modifier It allows direct access to any programmer who defines a suitable derived class Therefore, instance variables should normally not be marked protected 2012 Pearson Addison-Wesley

Access Levels Modifier Class Package Subclass World Public Y Y Y Y Protected Y Y Y N package (No modifier) Y Y N N private Y N N N

Multiple supers JAVA does not allow to use multiple supers. It is only valid to use super to invoke a method from a direct parent Repeating super will not invoke a method from some other ancestor class For example, if the Employee class were derived from the class Person, and the HourlyEmployee class were derived form the class Employee, it would not be possible to invoke the tostring method of the Person class within a method of the HourlyEmployee class super.super.tostring() // ILLEGAL! 2012 Pearson Addison-Wesley

The class object Every class is a descendent of the class Object in Java Even though a class is defined that is not explicitly a derived class of another class, it is still automatically a derived class of the class Object The class Object is in the package java.lang which is always imported automatically. 2012 Pearson Addison-Wesley

The class object Every Java class can call methods of the class Object E.g., equals and tostring methods Default implementation of equals() class provided by java.lang.object compares memory location and only return true if two reference variable are pointing to same memory location i.e. essentially they are same object. 2012 Pearson Addison-Wesley

Overriding the equals method public boolean equals(object otherobject) { if(otherobject == null) return false; if(getclass()!= otherobject.getclass()) return false; Car othercar = (Car)otherObject; return (color.equals(othercar.color) && speed == othercar.speed && size == othercar.size && CC == othercar.cc && gears == othercar.gears);