Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Polymorphism & Interfaces
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance in the Java programming language J. W. Rider.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
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.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Advanced Programming in Java
Objects as a programming concept
Inheritance and Polymorphism
Class vs Abstract vs Interface
Object Oriented Programming
More inheritance, Abstract Classes and Interfaces
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
CS18000: Problem Solving and Object-Oriented Programming
Advanced Programming in Java
Java – Inheritance.
Java Inheritance.
Advanced Programming in Java
CISC/CMPE320 - Prof. McLeod
Chapter 14 Abstract Classes and Interfaces
Advanced Programming in Java
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Inheritance 2 Mehdi Einali Advanced Programming in Java 1

2 Abstract behavior

3 Abstract Behaviors Animal -name:String -age:int #talk():String #swim():void #getAge():int Dog +talk():String Cat +talk():String Fish +swim():void

4 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!

5 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! “swim” is not a behavior of Animal All animals can not swim “swim” is a concrete behavior of Fish

6 Remember Shape Classes

7

8

9

10 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

11 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

12

13 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

14

15 More about inheritance

16 Sample 1

17 Sample 2

18 Sample 3

19 Sample 4

20 Sample 5

21 Sample 6

22 Sample 7

23 Sample 8

24 Sample 9

25 Sample 10

26 Sample 11

27 Sample 12

28 Final

29 Final Methods You can not override final methods final keyword Static method binding for final methods Private methods are implicitly final Static methods are implicitly final Static methods are statically bound Invoked reference is not important No polymorphism for static variables

30 Final Variables You can define variables as final The value of final variable will remain constant You can not change the value of final variables You should immediately assign a value to final variables Final parameter Final local variable Final property Final static variable

31 Final variables

32 Key notes about Final variable A not initialized final field of a class must be definitely assigned in every constructor of the class A not initialized final static variable must be definitely assigned in a static initializer of the class in which it is declared If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable

33 Final Classes You can not inherit from final classes No class can extend final classes

34 Review of final Keyword Final data Const Local variables Method parameters Member variables Primitives  constant values Objects  constant references A compile-time constant that won’t ever change A value initialized at run time that you don’t want changed

35 Review of final Keyword (2) Final Methods No override Final Class No sub-class final keyword on data Different from final classes & methods

36 Finalism and performance Final methods can be invoked inline Compiler can bind final methods statically Static binding So it may bring a better performance… It is now discouraged to use final to try to help the optimizer Especially with Java 6+ Don’t worry about performance Java optimizer

37 Robert Cecil Martin colloquially known as Uncle Bob Famous quots It is not enough for code to work. Say what you mean. Mean what you say The problem isn’t the simplicity of the code but the implicity of the code (to coin a phrase): the degree to which the context is not explicit in the code itself. Is not the language that makes programs appear simple. It is the programmer that make the language appear simple