Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
Abstract Classes and Interfaces
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Intro to OOP with Java, C. Thomas Wu
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
What is inheritance? It is the ability to create a new class from an existing class.
Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty.
Lecture 3: More Constructors, Scope, Wrappers, Inheritance, and Object Oriented Design. Java for the Impatient.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
CSC 142 Computer Science II Zhen Jiang West Chester University
Some Object-Oriented Programming (OOP) Review. Let’s practice writing some classes Write an Employee class with methods that return values for the following.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 4 Inheritance.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Advanced Programming in Java
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
Data Structures and Algorithms
Advanced Programming in Java
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Web Design & Development Lecture 5
ATS Application Programming: Java Programming
Extending Classes.
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
انجمن جاواکاپ تقدیم می‌کند دوره برنامه‌نويسی جاوا
Chapter 11 Inheritance and Polymorphism
Constructors under inheritance Variable Shadowing
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4

Slide - 2 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng  Extending classes & inheriting fields/methods  Using this and super  Constructor chaining  Single and multiple inheritance Class Extension and Inheritance

Slide - 3 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Inheritance: Natural, Hierarchical Way of Organizing Things Staff Member Employee Volunteer Hourly Salaried Consultant Think in terms of “is a” relationships: An Employee is a Staff Member, An Hourly worker is a Employee. A Consultant is a(n) Hourly employee. (subclass of Hourly) (subclass of Employee) (subclass of Staff) (superclass)

Slide - 4 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng class Animal { protected String strName = “”; protected String strNoise = “”; protected int iNumTimesPerformed = 0; // constructors, accessors & modifiers go here public void identifySelf( ) { System.out.println(“My name is “ + strName); } // of identifySelf public void perform ( ) { doYourThing( ); iNumTimesPerformed++; } // of perform public void doYourThing( ) { ; // ‘no-op’ method } // of doYourThing } // of Animal Example Inheritance: Sample [1/4]

Slide - 5 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Subclasses (Dog extends Animal i.e. “A dog is an animal” or “All dogs are animals”) class Dog extends Animal { public Dog() { strNoise = “Woof”; } // of constructor public void doYourThing ( ) { identifySelf(); System.out.println(“I am a dog”); System.out.println(strNoise); } // of doYourThing } // of Dog Recall: The Animal class had a no-op method for doYourThing() Animal Dog Cat Human Inheritance: Sample [2/4]

Slide - 6 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Subclasses (Cat extends Animal i.e. “A cat is an animal” or “All cats are animals”) class Cat extends Animal { public Cat() { strNoise = “Miaow”; } // of constructor public void doYourThing ( ) { identifySelf(); System.out.println(“I am a cat”); System.out.println(strNoise); } // of doYourThing } // of Cat Animal Dog Cat Human Inheritance: Sample [3/4]

Slide - 7 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Inheritance: Sample [4/4] Dog pickles = new Dog(); pickles.setName(“Pickles”); pickles.doYourThing(); // output: // “My name is Pickles” // “I am a dog” // “Woof” Cat abby = new Cat(); abby.setName(“Abby”); abby.doYourThing(); // output: // “My name is Abby” // “I am a cat” // “Miaow”

Slide - 8 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Inheritance and Hierarchy Variables (e.g. strNoise): Java first examines current method, looks for local variable or parameter; Java then examines current class (e.g. Dog); Java then examines superclass (e.g. Animal); Java continues up the class hierarchy until no more superclasses to examine. Methods (e.g. doYourThing() or identifySelf()): Java first examines current class; Java then examines superclass; Java continues up inheritance hierarchy until no more superclasses to examine.

Slide - 9 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Specifying Scope [1/2] Java allows you to override the scope rules by saying which variable/method you’re referring to: Keyword super: keyword for specifying method or variable from superclass, e.g., super.doYourThing( ) Keyword this: keyword for specifying method or variable in current object, e.g., this.doYourThing( )

Slide - 10 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng class Dog extends Animal { public Dog() { super.strNoise = “Woof”; } // of constructor public void doYourThing ( ) { super.identifySelf(); System.out.println(“I am a dog”); System.out.println(strNoise); } // of doYourThing } // of Dog Same (in this case) as strNoise = “Woof”; and this.strNoise = “Woof”; Same (in this case) as identifySelf(); or this.identifySelf(); Specifying Scope [2/2]

Slide - 11 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Using Super class Dog extends Animal { // constructor as before public void doYourThing() { identifySelf(); System.out.println(strNoise); } // of doYourThing public void identifySelf() { super.identifySelf(); System.out.println(“I am a dog”); } // of identifySelf } // of Dog Animal Dog Cat I.e. this.identifySelf() (newly defined below) I.e. the identifySelf() (defined in Animal)

Slide - 12 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng class Shape { public final double PI = ; protected String name; public String getName () { return (this.name); } // getName public int area () { return (0); } // area } // Shape A Geometry Example [1/2] Shape CircleRectangle

Slide - 13 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng class Rectangle extends Shape { private int length, width; Rectangle () { this(0, 0); } // constructor Rectangle (int l, int w) { this( l, w, “rectangle”); } // constructor Rectangle (int l, int w, String n) { length = l; width = l; name = n; } // constructor public int area () { return (length * width); } // area public String getName () { if (length == width) return ("square"); else return (super.getName()); } // getName public String toString () { String s; s = new String ("A " + getName() + " with length " + length + " and width " + width); return (s); } } // toString } // Rectangle A Geometry Example [2/2]

Slide - 14 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Java’s rule: If first line of constructor is not an explicit call to a superclass constructor, Java will implicitly put super( ) as the first line, calling the superclass default constructor. public Dog() { strNoise = “Woof”; } // of constructor An exception to this rule: chained constructor call to this(params) will defer super( ) call To use superclass constructors with params, call them explicitly, e.g., super(strName) Constructors and Inheritance implied call to Animal() here

Slide - 15 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Inheritance and Scoping [1/3] Examples: super(xxx) // calls a superclass constructor super.xxx // accesses superclass’ variable super.xxx( ) // calls superclass’ method this(xxx) // calls a current-class constructor this.xxx // accesses current class’s variable this.xxx( ) // calls current class’ method Note: cannot do super.super (can achieve this effect via casting, but rarely should; details later...)

Slide - 16 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng [2/3] Inheritance and Scoping [2/3] class StaffMember { String strName; public StaffMember( ) { System.out.println (“in default StaffMem constr; No Name”); setName(“No Name”); } // of constructor public StaffMember(String strName) { System.out.println (“in 2nd StaffMem constructior; have a Name”); setName(strName); } // of constructor public void setName(String strName) { this.strName = strName; } // of setName } // of StaffMember

Slide - 17 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng class Employee1 extends StaffMember { public Employee1(String strName) { setName(strName); } // of constructor } // of Employee1 What happens ??? [3/3] Inheritance and Scoping [3/3] Note: Employee has no local setName() method class Employee2 extends StaffMember { public Employee2(String strName) { setName(strName); } // of constructor public void setName(String strName) { super.setName(strName); System.out.println (“Name set”); } } // of Employee2

Slide - 18 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Class Object Java provides a base class, Object All classes that do not have an extends clause implicitly inherit directly fromclass java.lang.Object Examples: public boolean equals (Object o) public boolean String toString () When you create your own toString( ) method for a class, you are overriding the toString( ) provided by Object.

Slide - 19 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Object Hierarchy Animal Dog Cat Object Employee Salaried Hourly class Object methods: String toString() boolean equals(Object obj) and a few others... Animal Dog Cat Human? Object Employee Salaried Hourly Or how about...

Slide - 20 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng The End QUESTIONS & COMMENTS ?