1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation. Inheritance. Abstract Classes.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Computer Science I Inheritance Professor Evan Korth New York University.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“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
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
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 (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Classes, Interfaces and Packages
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Java Inheritance.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 9 Inheritance and Polymorphism
Review of Object-Oriented Concepts in JAVA
Interface.
Extending Classes.
Everything the light touches, Simba, will be yours
Inheritance Cse 3rd year.
Review of Object-Oriented Concepts in JAVA
Object-Oriented Programming
Presentation transcript:

1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object: The father of all classes l Casting and Classes

2 Inheritance ensures reusability l One of the good features of Java is reusability. That is once a class is defined, it can be used is any application where its method are required. l Java further extends the notion of re- usability through inheritance. l If we need to write a class and if there is an existing class that almost satisfies our requirements, then we do not need to start from scratch. We fully take advantage of the existing class by writing a class that provides only the additional or modified requirements. l The class we write is called an extension, child or sub-class of the existing class. l The existing class is called parent, ancestor, super or base class.

3 Example of Inheritance Consider the following Student class: class Student { protected long id; protected String name; protected double gpa; public Student(long id, String name, double gpa) { this.id = id; this.name = name; this.gpa = gpa; } public Student() { this(999999, "No name", 0.0); } public void changeGPA(double newGPA) { gpa = newGPA; } public void print() { System.out.print(id+" "+name+ " "+gpa); }

4 Example of Inheritance (Cont’d) Class TestStudent { public static void main (String[] args) { Student s1 = new Student(); s1.print(); Student s2 = new Student(991234,"Al- Ghamdi Ahmed", 3.45); s2.changeGPA(3.75); s2.print(); } l Now suppose we wish to write another class to represent Research Assistant. l Research Assistant is also a student but with the additional field for work load. l Instead of writing another class from the scratch, we extend the Student class as follows.

5 Example of Inheritance (Cont’d) class ResearchAssistant extends Student { private int workLoad; // in hours ResearchAssistant(long id, String name, double gpa,int workLoad){ super(id, name, gpa); this.workLoad = workLoad; } ResearchAssistant() { super(); workLoad = 0; } public void print() { super.print(); System.out.print(" " + workLoad); }

6 Example of Inheritance (Cont’d) class RunreserchAssistant { public static void main (String[] args) { ResearchAssistant s1 = new ResearchAssistant(); s1.print(); ResearchAssistant s2 = new ResearchAssistant(991234,"Al- Ghamdi Ahmed",3.45,15); s2.changeGPA(3.75); s2.print(); }

7 What is actually inherited? l Non-private fields and methods of the parent class are automatically inherited by the sub- class except those that are overridden by the subclass. l If a method is re-defined in the subclass with the same signature as that in the parent class, it is said to override the method in the parent class. l Constructors are not inherited, yet we often want to use the parent's constructor to set up the "parent's part" of the object. l The super reference can be used to refer to the parent class, and is often used to invoke the parent's constructor as shown in the above example. l Giving a field in a class the same name as a field in an ancestor hides the ancestor's field. The field exists, but cannot be accessed by its simple name. If has to be called using the super keyword.

8 Overloading vs. Overriding l Overloading deals with multiple methods in the same class with the same name but different signatures l Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature l Overloading lets you define a similar operation in different ways for different data l Overriding lets you define a similar operation in different ways for different object types

9 Object: The father of all classes l All classes inherit from the Java standard class, Object. Thus, class Parent { int size; } Is short hand for class Parent extends Object { int size; } Consider the following example: class TestObject { public static void main(String[] args) { Parent watchThis = new Parent(); String s = watchThis.toString(); System.out.println(s); // Where does toString() comes from? }

10 Some methods of the Object class clone() Creates a clone of the object. equals(Object) Compares two Objects for equality. Uses "==" to test for equality finalize() Code to perform when this object is garbage collected. getClass() Returns the Class of this Object. hashCode() Returns a hashcode for this Object. toString() Returns a String value of this Object. The toString and equals methods are often overridden when classes are created

11 Casting and Classes l Just like Java allows a smaller primitive type (e.g. int) to be assigned to a bigger type (e.g. double), It also allow a reference to a subclass object to be assigned to a reference of super class object. l Similarly, a reference of a super class can be casted to that of a subclass. class Parent { int data; } class Child extends Parent { String name; } class Casting { public static void main( String[] args ) { Object object; Parent parent; Child child = new Child(); parent = child; object = child; parent = (Parent) object; child = (Child) object; }