Advanced Programming in Java

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Inheritance Inheritance Reserved word protected Reserved word super
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.
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.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming Pillars Introduction to Object- Oriented Programming.
What is inheritance? It is the ability to create a new class from an existing class.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
© 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.
Introduction to Object-oriented Programming
Inheritance.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Objects as a programming concept
Final and Abstract Classes
Inheritance and Polymorphism
Object-Oriented Programming
03/10/14 Chapter 9 Inheritance.
Week 4 Object-Oriented Programming (1): Inheritance
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
ATS Application Programming: Java Programming
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Inheritance Chapter 5.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
MSIS 670 Object-Oriented Software Engineering
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
انجمن جاواکاپ تقدیم می‌کند دوره برنامه‌نويسی جاوا
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
CIS 110: Introduction to computer programming
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Advanced Programming in Java Inheritance1 Mehdi Einali

agenda this Inheritance Class Hierarchies is-a relationship UML Class Diagram protected members super keyword Initialization in inheritance

This

Constructor overloading

This() Use this() to invoke other Constructor

Overloading as reuse tool Key Note: Use overloading to avoid copy/past : reuse code DRY Dave Thomas The Pragmatic Programmer 1999 Coupling/Cohesion

Introduction to Inheritance

Class Hierarchies-1

Class Hierarchies-2

Why inheritance? good tool for abstraction Reuse Better model real world Increase cohesion/decrease coupleling

Bad smell Avoid Copy & Paste! Please, Avoid Copy & Paste!

terminology

inheritance Key points: Inheritance is about hierarchical abstraction Inheritance is about is-a relationship Inheritance is about common attributes جهان آسیا خاورمیانه ایران عراق آسیای میانه خاور دور اوروپا اسکاندیناوی

Abstraction hierarchy It is about role, aspects, meanings and concepts It means in context (like other aspects of design)

Person Teacher Manager Human Male Female Patient Chronic Acute Thing Student Staff Human Male Female Thing Physical Mental Person Teacher Manager Patient Chronic Acute Citizen Employee Employer Me

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

Object is is-a hierarchy Athlete Wrestler Freestyle Wrestler Gecko-Roman Wrestler Hamid Soorian Football Player Goalkeeper Halfback

Inheritance in java

Class Implementation How do you implement Employee class? How do you implement Faculty class?

Faculty & Employee Faculty is inherited from Employee Faculty extends Employee Attributes and behaviors of Employee is inherited to Faculty Inheritance provides code reuse

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

Liskov substitsion principle In a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.) ربارا لیسکف (به انگلیسی: Barbara Liskov) ، (باربارا جِین هابرمن) ‏ زاده ۱۹۳۹، دانشمند علوم رایانه است. او در سال ۲۰۰۸ جایزه تورینگ را به علت «ابداعات اساسی در طراحی زبان برنامه‌نویسی» دریافت کرد.[۱] محتویات   [نهفتن]  ۱ زندگینامه ۲ جستارهای وابسته ۳ منابع ۴ پیوند به بیرون زندگینامه[ویرایش] او «استاد موسسه» (به انگلیسی: Institute Professor) در دانشگاه‌ام‌آی‌تی است که بالاترین درجه استادی در این دانشگاه به حساب می‌آید. در سال ۲۰۰۸ او دومین زنی است که جایزه تورینگ را دریافت کرده است. در سال ۲۰۰۴ نیز مدال جان فون نیومن را دریافت کرد. لیسکف زبان برنامه‌نویسی سی‌ال‌یو و آرگوس را ابداع کرده و همراه جانت ویگ، اصل جانشینی لیسکف را اثبات کرده است. او نویسنده سه کتاب و صدها مقاله تخصصی است. Is-a

UML Class Diagram-1

Uml class diagram-2

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; return width*length;

Adam in objects 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

Class Hierarchy Direct superclass Indirect 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

Subclasses may… Add new functionality Use inherited functionality New members Use inherited functionality Software reuse Override inherited functionality Change parent methods

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);

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); New methods (more behavior) Use parent methods (Software Reuse) Change parent methods (Override)

Person p1 = new Person(); p1.setName("Ali Alavi"); p1.setNationalID(1498670972L); p1.show(); Student st = new Student(); st.setName("Ali Alavi"); st.setNationalID(1498670972L); st.setStudentID("89072456"); st.show(); Defined in Parent Added in Child Changed in Child

Access modifiers in inheritance

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?

Protected member 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

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 Meaning of extends Parent granted privilege can not be revoked by child

Uml notation

super

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

Application of super Keyword class Student extends Person{ public void show(){ super.show(); System.out.println( ",studentID=" + studentID); }

Variables in inheritance

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

Super()

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

construction 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

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", 1498670972L); Student st = new Student("Ali Alavi", 1498670972L, "89072456");

why

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?

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

1 3 2 4 8 14 5 11 9 15 6 12 10 16 7 13

end