Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

Object Oriented Programming with Java
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Inheritance using Java
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
What is inheritance? It is the ability to create a new class from an existing class.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
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.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Object-Oriented Programming: Inheritance
Advanced Programming in Java
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
MSIS 670 Object-Oriented Software Engineering
Advanced Programming Behnam Hatami Fall 2017.
انجمن جاواکاپ تقدیم می‌کند دوره برنامه‌نويسی جاوا
Presentation transcript:

Sadegh Aliakbary

Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Agenda Inheritance Class Hierarchies is-a relationship UML Class Diagram protected members Abstract Methods super keyword Initialization in inheritance 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Class Hierarchies 5JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Class Hierarchies (2) 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Class Hierarchies (3) 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

General and Specific Types Faculty and Employee are both classes Faculty is a more specific type of Employee Employee is a more general type of Faculty A Faculty instance is also a Employee instance Dr. Felani is a faculty member He is also an employee of university 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

is-a Relationship More general class : Superclass More specific class : Subclass Subclass is inherited from superclass Faculty is inherited from Employee Rectangle is inherited from Shape A rectangle is also a shape Cat is inherited from Animal Maloos is a cat, she is also an animal 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Inheritance (2) Name some other hierarchies… Note: More specific type is also a class Not an object Ali Karimi is not a more specific type of FootballPlayer He is an object Not a class 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Class Implementation How do you implement Employee class? How do you implement Faculty class? 11JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Inheritance in OOP Java offers inheritance like any object oriented programming language Inheritance is used for implementing more specific classes Duplicate code is eliminated Inheritance provides code reuse is-a relationship 14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Faculty & Employee Faculty is inherited from Employee Faculty extends Employee Attributes and behaviors of Employee is inherited to Faculty Inheritance provides code reuse 15JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

UML Class Diagram 16JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

UML Class Diagram 17JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

class Shape{ int color; int positionX, positionY; } class Circle extends Shape{ private int radius; public double getArea(){ return 3.14*radius*radius; } } class Rectangle extends Shape{ private int width, length; public double getArea(){ return width*length; } } 18JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Terminology Class Rectangle is inherited from class Shape Rectangle is a subclass of Shape Rectangle is a child of Shape Rectangle is a derived class of Shape Shape is the super-class of Rectangle Shape is the parent of Rectangle Shape is the base-class of Rectangle Rectangle is-a Shape 19JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Inheritance in java Every class is inherited from class Object Primitive-types are not objects Object class has some operations equals() toString() … Every class adds some operations And may changes some operations toString() 20JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Subclass extends superclass. Subclass: More specialized group of objects Behaviors inherited from superclass Or changed and customized Additional behaviors 21JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Class Hierarchy Direct superclass Inherited explicitly (one level up hierarchy) Indirect superclass Inherited two or more levels up hierarchy Single inheritance Inherits from one superclass Multiple inheritance Inherits from multiple superclasses Java does not support multiple inheritance 22JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

is-a relationship Object of subclass “is a” object of super-class Example: Rectangle is quadrilateral. Class Rectangle inherits from class Quadrilateral Quadrilateral: superclass Rectangle: subclass Superclass typically represents larger set of objects than subclasses superclass: Vehicle subclass: Car Smaller, more-specific subset of vehicles 23JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

More Examples 24JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Subclasses may… Add new functionality New members Use inherited functionality Software reuse Override inherited functionality Change parent methods 25JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Software reuse Software reuse is at the heart of inheritance Using existing software components to create new ones Capitalize on all the effort that went into the design, implementation, and testing of the existing software 26JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Bad smell Avoid Copy & Paste! Please, Avoid Copy & Paste! 27JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

public class Person { private String name; private Long nationalID; public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getNationalID() { return nationalID; } public void setNationalID(Long nationalID) { this.nationalID = nationalID; } public void show() { System.out.println("Person: name=" + name + ",nationalID=" + nationalID); } 28JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

class Student extends Person{ private String studentID; public void setStudentID(String studentID) { this.studentID = studentID; } public String getStudentID() { return studentID; } public void takeCourse(Course course){... } public void show(){ System.out.println("Student: name=" + getName() + ",nationalID=" + getNationalID() + ",studentID=" + studentID); } 29 New properties New methods (more behavior) Change parent methods (Override) Use parent methods (Software Reuse) JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Person p1 = new Person(); p1.setName("Ali Alavi"); p1.setNationalID( L); p1.show(); Student st = new Student(); st.setName("Ali Alavi"); st.setNationalID( L); st.setStudentID(" "); st.show(); 30 Defined in Parent Added in Child Changed in Child JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Abstract Behaviors Does any animal swim? No. So “to swim” is not a behavior of animals. Any animal has a voice “to talk” is a behavior of animals But what is the voice of an animal? How does an animal “talk”? It depends to the specific type of animal Dog: Hop! Hop! Cat: Mewww! 32JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Abstract Behaviors (2) “talk” is an abstract behavior of Animal All animals can “talk” But we can not specify how an animal talks It depends to the specific class of animal “talk” is a concrete behavior of Dog Hop! Hop! “swim” is not a behavior of Animal All animals can not swim “swim” is a concrete behavior of Fish 33JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Remember Shape Classes 34JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

35JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

36JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

37JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Shapes Example Shape is an abstract class Some methods are undefined in Shape Some methods should be defined in sub-classes getArea() getPerimeter() These methods are abstract methods Remember abstract behaviors 38JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Abstract Methods Shape classes getArea() draw() Animals Talk() getName() is not abstract! How do you implement an abstract method? We can implement these methods by simple dummy operations Better way : abstract methods 39JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Abstract Methods (2) abstract method : no implementation A class containing abstract methods: an abstract class You can not instantiate abstract classes Why? If a sub-class do not implement the abstract method It will be abstract too 40JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Animal Example 41JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Animal Example (2) 42JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Access modifier in inheritance You can not override a public method as a private method Why? It violates the “is-a” rule You can not reduce accessibility of methods in subclasses 43JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Accessibility of Members Subclass has access to public members of parent class Public methods and properties are accessible in subclass Student used getName() and getStudentID() Private members are not accessible in subclass Student has no access to name and studentID properties What if you want to let subclasses access a member, but not other classes? 44JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Protected Members Intermediate level of protection between public and private protected members accessible by subclass members Class members in the same package protected members are also package accessible friendly Protected variables and methods are shown with a # symbol in UML diagrams 45JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

UML Class Diagram 46JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Access Levels Public Accessible by all classes Protected Accessible by subclasses Accessible by classes of the same package Package access (friendly) Accessible by classes of the same package Private No access 47JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

super Keyword Access to parent members The super reference can be used to refer to the parent class super.f() invokes f() from parent class Why we need it? When the method is overridden in subclass super is also used to invoke the parent's constructor 48JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Application of super Keyword class Student extends Person{ public void show(){ super.show(); System.out.println( ",studentID=" + studentID); } 49JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Variable Scopes 50JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Object class methods equals() toString() finalize() … We can override these methods in our classes 51JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Multiple Inheritance Java supports single inheritance Derived class can have only one parent class Multiple inheritance allows a class to be derived from two or more classes inheriting the members of all parents Collisions, such as the same variable name in two parents, have to be resolved Java does not support multiple inheritance The use of interfaces usually gives us aspects of multiple inheritance without the overhead 52JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Initialization Constructors are not inherited even though they have public visibility We often want to use the parent's constructor to set up the "parent's part" of the object 53JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Constructors A child’s constructor is responsible for calling the parent’s constructor It is done using super keyword The first statement of a child’s constructor should be the super reference to call the parent constructor Otherwise, default constructor is implicitly invoked If default constructor does not exist? (how?!) A syntax error You should explicitly call an appropriate parent constructor 54JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

class Person { private String name; private Long nationalID; public Person(String name, Long nationalID) { this.name = name; this.nationalID = nationalID; } class Student extends Person{ private String studentID; public Student(String name, Long naID, String stID) { super(name, naID); this.studentID = stID; } Person p1 = new Person("Ali Alavi", L); Student st = new Student("Ali Alavi", L, " "); 55JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Order of initialization Once per class Static variable declaration of parent Static block of parent Static variable declaration Static block Once per object variable declaration of parent Initialization block of parent Constructor of parent variable declaration Initialization block Constructor 56JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

public class Parent { static int a = A(); static{ a=B(); } int b = E(); { b = F(); } public Parent(){ b = G(); } 57 class Child extends Parent{ static int c = C(); static{ c=D(); } int b = H(); { b = I(); } public Child(){ b = J(); } class Child extends Parent{ static int c = C(); static{ c=D(); } int b = H(); { b = I(); } public Child(){ b = J(); } What happens if we invoke: new Child(); JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Some Notes As the first statement of a constructor, we can invoke parent constructor Using super keyword Only once First line? No. first statement. We can not use properties in this super invocation super(this.name) Why? 58JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Exercise Design these classes and draw a UML class diagram for: Object Person (name, phoneNumber) Student (average, entranceYear) MS Student (thesis title) Instructor (offeredCourses) 59JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

60JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source