12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 211 Inheritance AAA.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
What is inheritance? It is the ability to create a new class from an existing class.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
BY:- TOPS Technologies
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Final and Abstract Classes
Inheritance and Polymorphism
Data Structures and Algorithms
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Interface.
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Programming, Second Edition
Java Inheritance.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
CSG2H3 Object Oriented Programming
Presentation transcript:

12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming

12-CRS-0106 REVISED 8 FEB 2013 What is Inheritance?

12-CRS-0106 REVISED 8 FEB 2013 Inheritance The ability of a java class to ‘pass down’ all or some of their fields, attributes, and methods to another class that will be referred to as their child class Child class will be able to recognize the inherited fields and methods, and can use it without declaring or defining again

12-CRS-0106 REVISED 8 FEB 2013 Inheritance Inheritance is an implementation of generalization-specialization class relationship or “is-a” relationship In short : it’s the mechanism by which one class acquires the properties of another class

12-CRS-0106 REVISED 8 FEB 2013 Inheritance Denoted by arrows (hollow and triangular head) The classes that passed down the attributes and methods are called –parent class, super class, or base class The classes that inherit the attributes and methods are called –Child class, subclass, or derived class Parent Class Child Class

12-CRS-0106 REVISED 8 FEB 2013 Types of Inheritance Single Inheritance –Child class inherit from 1 parent class Multiple Inheritance –Child class inherit from many parent classes parent classchild class parent class-1parent class-n child class...

12-CRS-0106 REVISED 8 FEB 2013 Java Class Inheritance Single inheritance only Using keyword extends All non-private attributes and methods are passed on To access the properties of parent –Use “super” keyword class ChildClass extends ParentClass{ // child class attributes // child class methods }

12-CRS-0106 REVISED 8 FEB 2013 Multiple Inheritance in Java Multiple Inheritance is handled using staged single inheritance EF Base Multiple Inheritance Multiple Inheritance using Staged Single Inheritance ABCD ABCD AB AB CD ABCD EF

12-CRS-0106 REVISED 8 FEB 2013 Example - Method Inheritance public class Parent{ public String methodParent(){ return "this is method Parent"; } public class Child extends Parent{ public String methodChild(){ return "this is method Child"; } public class Driver{ public static void main(String args[]){ Parent p = new Parent(); Child c = new Child(); System.out.println(p.methodParent()); System.out.println(c.methodParent()); System.out.println(c.methodChild()); System.out.println(p.methodChild()); } } > This is method Parent > This is method Child > error: cannot find symbol Child can access public method of Parent, but not vice versa

12-CRS-0106 REVISED 8 FEB 2013 Example – Variable Inheritance public class Parent{ public int publicInt; private int privateInt; int defaultInt; } public class Child extends Parent{ public void methodChild(){ publicInt = 10; privateInt = 20; defaultInt = 30; } error: privateInt has private access

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ public int publicInt; private int privateInt; int defaultInt; public void setPrivateInt(int privateInt){ this.privateInt = privateInt; } } public class Child extends Parent{ public void methodChild(){ publicInt = 10; setPrivateInt(20); defaultInt = 30; } }

12-CRS-0106 REVISED 8 FEB 2013 Example package test; public class Parent{ public int publicInt; private int privateInt; int defaultInt; public void setPrivateInt(int privateInt){ this.privateInt = privateInt; } } import test.Parent; public class Child extends Parent{ public void methodChild(){ publicInt = 10; setPrivateInt(20); defaultInt = 30; } } error: defaultInt is not public in Parent; cannot be accessed from outside package

12-CRS-0106 REVISED 8 FEB 2013 Access Modifier Protected All class in the same package All subclass (child class) even in different package

12-CRS-0106 REVISED 8 FEB 2013 Example package test; public class Parent{ public int publicInt; private int privateInt; int defaultInt; protected int protectedInt; public void setPrivateInt(int privateInt){ this.privateInt = privateInt; } import test.Parent; public class Child extends Parent{ public void methodChild(){ publicInt = 10; setPrivateInt(20); protectedInt = 30; } } public class Driver{ public static void main(String args[]){ Child c = new Child(); c.methodChild(); } } Modification to protectedInt done by Child class

12-CRS-0106 REVISED 8 FEB 2013 Example package test; public class Parent{ public int publicInt; private int privateInt; int defaultInt; protected int protectedInt; public void setPrivateInt(int privateInt){ this.privateInt = privateInt; } } import test.Parent; public class Child extends Parent{ } error: protectedInt has protected access in parent (protectedInt is owned by Parent in different package) public class Driver{ public static void main(String args[]){ Child c = new Child(); c.publicInt = 10; c.setPrivateInt(20); c.protectedInt = 40; } }

12-CRS-0106 REVISED 8 FEB 2013 Overriding Redefining field or method inherited by Parent class in Child class (Polymorphism) subclass can implement a parent class method more specifically based on its requirement. When overriding a field or method, the Child class will have access to both the original parent field/method and the new redefined child field/method Call parent’s field/method using keyword “super” –Keyword “super” only available inside the child class

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ public String toString(){ return “this is method Parent”; } public class Child extends Parent{ public String toString(){ return “this method overridden by Child”; } > this method overridden by Child > this is method Parent public class Driver{ public static void main(String args[]){ Child c = new Child(); Parent p = new Parent(); System.out.println(c.toString()); System.out.println(p.toString()); } }

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ public String toString(){ return “this is method Parent”; } public class Child extends Parent{ public String toString(){ return “this method overridden by Child”; } public String toStringParent(){ return super.toString(); } } > this method overridden by Child > this is method Parent public class Driver{ public static void main(String args[]){ Child c = new Child(); Parent p = new Parent(); System.out.println(c.toString()); System.out.println(p.toString()); System.out.println(c.toStringParent()); } }

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ protected int number; } public class Child extends Parent{ private int number; public void methodChild(){ number = 10; super.number = 20; System.out.println(number); System.out.println(super.number); } } > 10 > 20 public class Driver{ public static void main(String args[]){ Child c = new Child(); c.methodChild(); } }

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ protected int number; public void setNumber(int number){ this.number = number; } public int getNumber(){ return number; } } public class Child extends Parent{ private int number; public void methodChild(){ number = 10; super.number = 20; } > 20 public class Driver{ public static void main(String args[]){ Child c = new Child(); c.methodChild(); System.out.println(c.getNumber()); } } setNumber() and getNumber() is owned by parent, accessing number value from parent (super)

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ protected int number; public void setNumber(int number){ this.number = number; } public int getNumber(){ return number; } public class Child extends Parent{ private int number; public void methodChild(){ number = 10; super.number = 20; } public int getNumber(){ return number; } } > 10 public class Driver{ public static void main(String args[]){ Child c = new Child(); c.methodChild(); System.out.println(c.getNumber()); } If it’s overridden, the method will access child field first

12-CRS-0106 REVISED 8 FEB 2013 Non-Access Modifier Final To limit the access to modify the class, field, or method Final modifier in method –Method cannot be overridden by child class, for use only –public final void finMethod(){ … } public class Parent{ public void publicMethod(){ } final public void finMethod(){ } public class Child extends Parent{ public void publicMethod(){ } public void finMethod(){ } //ok // forbidden

12-CRS-0106 REVISED 8 FEB 2013 Non-Access Modifier Final Final modifier in class –Class cannot be extended (subclassed) by other class –public final class FinClass{ … } Public final class FinalParent{ } public class Child extends FinalParent{ } // forbidden

12-CRS-0106 REVISED 8 FEB 2013 Non-Access Modifier Final Final modifier in field/variable –Field/variable is constant, value set when declared or initiated in constructors, and cannot be changed afterward –private final double finDouble –represent final constants in all uppercase, using underscore to separate words public class Driver{ public static void main(String args[]){ final int a = 5; final int b; b = 8; a = 15; b = 21; } } // forbidden

12-CRS-0106 REVISED 8 FEB 2013 Non-Access Modifier Final Final modifier in Reverence Variable –Once the variable refers to an object, the variable cannot be re-bound to reference another object. –But the object that it references is still mutable, if it was originally mutable. –private final ClassName finObject;

12-CRS-0106 REVISED 8 FEB 2013 Non-Access Modifier Final public class Parent{ int number; public void setNumber(int number){ this.number = number; } public int getNumber(){ return number; } public class Driver{ public static void main(String args[]){ Parent p = new Parent(); final Parent fp1 = new Parent(); final Parent fp2; fp2 = p; fp1 = p; fp2 = new Parent(); fp2.setNumber(5); fp1.setNumber(20); } } // forbidden

12-CRS-0106 REVISED 8 FEB 2013 Constructor is not inherited If there is only non-default constructor(s) in parent class –There must be at least one child constructor that invokes one parent constructor –Every constructors in child class must invoke one parent constructor –Invoke parent constructor using method super(parameter) If there is a default constructor in parent class –the child class does not have to invoke the parent constructor

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ protected int vP1; protected double vP2; public Parent(){ } public void methodParent(){ } } public class Child extends Parent{ private int vC; public Child(int vC){ this.vC = vC; } public void methodChild(){ } } If there is a default constructor or no constructor at all present at parent class, then child class does not have to invoke parent class constructor child class may or may not have its own constructor

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ protected int vP1; protected double vP2; public Parent(int vP1){ this.vP1 = vP1; } public Parent(int vP1,double vP2 ){ this.vP1 = vP1; this.vP2 = vP2; } public Parent(double vP2 ){ this.vP2 = vP2; } public void methodParent(){ } } public class Child extends Parent{ private int vC; public Child(int vC, double vP){ this.vC = vC; super(vP); } public void methodChild(){ } } If there is only non-default constructor(s) in parent class, There must be at least one child constructor that invokes one parent constructor Use keyword super() to invoke parent constructor

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ protected int vP1; protected double vP2; public Parent(int vP1){ this.vP1 = vP1; } public Parent(int vP1,double vP2 ){ this.vP1 = vP1; this.vP2 = vP2; } public Parent(double vP2 ){ this.vP2 = vP2; } public void methodParent(){ } public class Child extends Parent{ private int vC; public Child(int vC, double vP){ this.vC = vC; super(vP); } public Child(double vP){ super(5, vP); } public Child(){ super(2.5); } public void methodChild(){ } } All child constructor must invoke parent constructor

12-CRS-0106 REVISED 8 FEB 2013 Example public class Parent{ protected int vP1; protected double vP2; public Parent(){ vP1 = 50; } public Parent(int vP1,double vP2 ){ this.vP1 = vP1; this.vP2 = vP2; } public Parent(double vP2 ){ this.vP2 = vP2; } public void methodParent(){ } } public class Child extends Parent{ private int vC; public Child(int vC, double vP){ super(vC, vP); } public Child(int vC){ this.vC = vC; } public Child(){ this.vC = 25; } public void methodChild(){ } } If parent class has default constructor, then the child class may or may not invoke the parent constructor at all Child constructors that did not invoke any parent constructor will automatically invoke the default parent constructor

12-CRS-0106 REVISED 8 FEB 2013 When to Use Specialization –Top-down view Generalization –Bottom-up view

12-CRS-0106 REVISED 8 FEB 2013 Specialization Bird

12-CRS-0106 REVISED 8 FEB 2013 Specialization Bird GullEagleDove

12-CRS-0106 REVISED 8 FEB 2013 Generalization ? EraserPenRuler

12-CRS-0106 REVISED 8 FEB 2013 Generalization Stationery EraserPenRuler

12-CRS-0106 REVISED 8 FEB 2013 Specialization or Generalization ? Stationery EraserPenRuler

12-CRS-0106 REVISED 8 FEB 2013 Generalisasi & Spesialisasi

12-CRS-0106 REVISED 8 FEB 2013 When to use Top-Down View –to create a new classes with some behavior that already exists in other class (or similar with other class) –to create more specific classes or modification from a base class –to break down a large class to more specific classes –to create a form of class hierarchy For those reasons we can create subclasses that inherit the base class

12-CRS-0106 REVISED 8 FEB 2013 When to use Bottom-Up View –When there several classes that generally the same (many methods and fields are the same or similar) but not necessary that one of the class is to be the child of another –To create a module to interacts between classes For those reasons we can create a parent class that collects the other classes as its child classes

12-CRS-0106 REVISED 8 FEB 2013 Benefit of Inheritance Reusability –Reuse the methods and data of the existing class Extends –Adding new data and new methods to the existing class Modify –Modify the existing class by overloading its methods with your won implementation Reduction (Grouping and Organizing)

12-CRS-0106 REVISED 8 FEB things you might find in an Inheritance Hierarchy: superclass is too general to declare all behavior, so each subclass adds its own behavior superclass legislates an abstract behavior and therefore delegates implementation to its subclasses superclass specifies behavior, subclasses inherit behavior superclass specifies behavior, subclasses can choose to override behavior completely –just because a subclass inherits a method doesn’t mean that it must act in the same way as its superclass –subclass can choose to reject its superclass’ implementation of any method and “do it my way” superclass specifies behavior, subclasses can choose to override behavior in part –called partial overriding

12-CRS-0106 REVISED 8 FEB 2013 Question?

12-CRS-0106 REVISED 8 FEB 2013 THANK YOU Credits M usic : Yonezawa Madoka - Oui! Ai Kotoba (Instrumental)