Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Programming in Java

Similar presentations


Presentation on theme: "Advanced Programming in Java"— Presentation transcript:

1 Advanced Programming in Java
Inheritance1 Mehdi Einali

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

3 This

4 Constructor overloading

5 This() Use this() to invoke other Constructor

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

7 Introduction to Inheritance

8 Class Hierarchies-1

9 Class Hierarchies-2

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

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

12 terminology

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

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

15 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

16 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

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

18 Inheritance in java

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

20

21

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

23 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

24 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

25 UML Class Diagram-1

26 Uml class diagram-2

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

28 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

29 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

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

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

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

33 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(); Defined in Parent Added in Child Changed in Child

34 Access modifiers in inheritance

35 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?

36 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

37

38 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

39 Uml notation

40 super

41 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

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

43 Variables in inheritance

44 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

45 Super()

46 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

47 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

48 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, " ");

49 why

50 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?

51 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

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

53 end


Download ppt "Advanced Programming in Java"

Similar presentations


Ads by Google