ATS Application Programming: Java Programming

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Road Map Introduction to object oriented programming. Classes
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science I Inheritance Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
“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
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
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)
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Java Inheritance.
Advanced Programming in Java
Week 4 Object-Oriented Programming (1): Inheritance
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
ATS Application Programming: Java Programming
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Encapsulation & Abstraction
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
Class Inheritance (Cont.)
Inheritance Basics Programming with Inheritance
Java Inheritance.
MSIS 670 Object-Oriented Software Engineering
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming Behnam Hatami Fall 2017.
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Java Inheritance.
Presentation transcript:

ATS Application Programming: Java Programming Inheritance 3.2 Inheritance Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods Constructor Chaining Casting © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming Objectives 3.2 Inheritance Define inheritance Identify relationships of inheritance Describe the rules of inheritance Describe the difference between this and super references Describe the difference between this() and super() methods Describe how to create constructors Describe the steps in constructor chaining Describe how to convert data types using casting © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming Defining Inheritance 3.2 Inheritance Inheritance is the ability to derive new classes from existing ones. A derived class ("subclass") inherits the instance variables and methods of the base class ("superclass"), and may add new instance variables and methods. Inheritance defines a hierarchical relationship among classes wherein one class shares the attributes and methods defined in one or more classes. Inheritance is a relationship among classes in which one class shares the structure and behavior of another. A subclass inherits from a superclass. Notes: We provided several definitions just to be comprehensive. Inheritance—a way of organizing classes Term comes from inheritance of traits like eye color, hair color, and so on. Classes with properties in common can be grouped so that their common properties are only defined once. When you extend from an existing class, a new class is created that has the behaviors / and public attributes of the class it’s derived from Explain why the subclass will change if the superclass is changed © Accenture 2005 All Rights Reserved Course Code #Z16325

Relationships of Inheritance ATS Application Programming: Java Programming Relationships of Inheritance 3.2 Inheritance “is-a” relationship a subclass can be used wherever a superclass can be used implemented in Java by extending a class “has-a” relationship a whole-class relationship between a class and its parts also known as composition or aggregation implemented in Java by instantiating an object inside a class “is-a” relationship Vehicle Car Bus Car Engine Wheel “has-a” relationship © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming INHERITANCE 3.2 Inheritance Acquiring the properties of an existing Object into newly creating Object to overcome the redeclaration of properties in deferent classes. These are 3 types: 1.Simple Inheritance SUPER SUPER extends extends SUB SUB 1 SUB 2 © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming INHERITANCE 3.2 Inheritance 2. Multi Level Inheritance 3. Multiple Inheritance SUPER 1 SUPER 2 SUPER implements extends SUPER 1 SUPER 2 SUB SUB extends implements extends SUB SUB SUB © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming extends 3.2 Inheritance Is a keyword used to inherit a class from another class Allows to extend from only one class class One { int a=5; } class Two extends One { int b=10; } © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming super 3.2 Inheritance ‘super’ is a keyword used to refer to hidden variables of super class from sub class. super.a=a; It is used to call a constructor of super class from constructor of sub class which should be first statement. super(a,b); It is used to call a super class method from sub class method to avoid redundancy of code super.addNumbers(a, b); © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming Rules of Inheritance 3.2 Inheritance A class can only inherit from one class (known as single inheritance). A subclass is guaranteed to do everything the superclass can do. A subclass inherits members from its superclass and can modify or add to its behavior and properties. A subclass can define members of the same name in the superclass, thus hiding the superclass members. Inheritance is transitive (i.e., class A inherits from class B, including what B inherited from class C). All classes inherit from the Object class - the highest in the inheritance hierarchy. private members, hidden members, and constructors are not inherited by subclasses. © Accenture 2005 All Rights Reserved Course Code #Z16325

Inheritance Hierarchy ATS Application Programming: Java Programming Inheritance Hierarchy 3.2 Inheritance Inheritance allows you to define a very general class, then later define more specialized classes by adding new detail. The general class is called the base or parent class. The specialized classes inherit all the properties of the general class. Specialized classes are derived from the base class. They are called derived or child classes. After the general class is developed you only have to write the “difference” or “specialization” code for each derived class. A class hierarchy: classes can be derived from derived classes (child classes can be parent classes) Any class higher in the hierarchy is an ancestor class Any class lower in the hierarchy is a descendent class © Accenture 2005 All Rights Reserved Course Code #Z16325

Implementing Inheritance ATS Application Programming: Java Programming Implementing Inheritance 3.2 Inheritance Inheritance is implemented in Java by the extends keyword class Student extends Person { String course; double grade; /* * other members of Person class * can be accessed here */ } public static void main(String[] args) { Student junior = new Student(); // access current class members junior.course = "Computer Science"; junior.grade = 1.5; // access superclass members junior.setName("Andrew"); junior.setAge(21); junior.setSex('M'); class Person { // set variables to private private static int maleCount; private static int femaleCount; private String name; private char sex; private int age; /* * setters & getters, set to public */ public int getAge() { return age;} public void setAge(int a) { age = a;} public String getName() { return name;} public void setName(String n) { name = n;} public char getSex() { return sex;} public void setSex(char s) { sex = s;} * other methods here } Talking Points: © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming this and super 3.2 Inheritance this is a reference to the object of the current class It can be used to distinguish instance variables from local variables It can be assigned to other references, or passed as a parameter, or cast to other types It cannot be used in a static context super is a reference to the object of a superclass Used to access hidden members of the superclass It cannot be assigned to other references, nor passed as a parameter, nor cast to other types © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming Using this and super 3.2 Inheritance class Student extends Person { private String course; private double grade; static int studentCount; // setters & getters public String getCourse(){return course;} public void setCourse(String course) { this.course = course; } public double getGrade(){return grade;} public void setGrade(double grade) { this.grade = grade; // access superclass members public void printInfo() { System.out.println(super.getName()); System.out.println(super.getSex()); System.out.println(super.getAge()); class Person { // set variables to private private static int maleCount; private static int femaleCount; private String name; private char sex; private int age; /* * setters & getters, set to public */ public int getAge() { return age;} public void setAge(int a) { age = a;} public String getName() { return name;} public void setName(String n) { name = n;} public char getSex() { return sex;} public void setSex(char s) { sex = s;} * other methods here } Talking Points: © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming Constructor Chaining 3.2 Inheritance Constructor chaining is invoking all the constructors in the inheritance hierarchy. Constructor chaining guarantees that all superclass constructors are called. Constructors are invoked starting with the current class up to the Object class, then they are executed in reverse order. © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming this() and super() 3.2 Inheritance this(<optional parameters>) used to call constructors within the same class used to implement local chaining of constructors must be the first statement in a constructor and can only be used in a constructor super(<optional parameters>) used to invoke constructors in the immediate superclass used to implement chaining of subclass-to-superclass constructors Talking Points: © Accenture 2005 All Rights Reserved Course Code #Z16325

Implementing Constructor Chaining ATS Application Programming: Java Programming Implementing Constructor Chaining 3.2 Inheritance class Person { private String name; private char sex; private int age; Person() { // default constructor sex = 'M'; age = 0; } class Student extends Person { String course; double grade; static int studentCount; Student() { // default constructor super(); // chain to superclass studentCount++; Student(String n) { // overloaded constructor (non-default) this(); // local chain super.setName(n); public static void main(String[] args) { Student junior = new Student("Andrew"); Talking Points: © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming Casting 3.2 Inheritance Casting is converting from one data type to another Implicit casting is an implied casting operation Explicit casting is a required casting operation Primitive casting is converting a primitive data type to another data type Widening conversion is casting a narrower data type to a broader data type Narrowing conversion is casting a broader data type to a narrower data type Reference casting is converting a reference data type to another Upcasting is conversion up the inheritance hierarchy Downcasting is conversion down the inheritance hierarchy Casting between primitive and reference type is not allowed In Java, casting is implemented using () operator © Accenture 2005 All Rights Reserved Course Code #Z16325

Reference Casting Flow ATS Application Programming: Java Programming Reference Casting Flow 3.2 Inheritance “Upcasting” Vehicle “Downcasting” Car Boat Plane Imagine it as a container! Vehicle Car Boat Plane © Accenture 2005 All Rights Reserved Course Code #Z16325

Reference Casting Rule ATS Application Programming: Java Programming Reference Casting Rule 3.2 Inheritance Operation Conversion Type string concatenation implicit upcasting parameter passing implicit upcasting (if formal parameter is a superclass) (all others) explicit casting (downcasting or upcasting ) © Accenture 2005 All Rights Reserved Course Code #Z16325

Implementing Reference Casting ATS Application Programming: Java Programming Implementing Reference Casting 3.2 Inheritance public class Vehicle {} public class Car extends Vehicle {} public class Boat extends Vehicle {} public class Plane extends Vehicle {} public static void main(String args[]) { Vehicle v = new Vehicle(); Car c = new Car(); Boat b = new Boat(); Plane p = new Plane(); Vehicle vc = new Car(); v = c; // This will compile. c = v; // This will not compile!!! b = p; // will this compile? b = (Boat) v; // will this compile? c = (Car) vc; // will this compile? } Faculty Notes: Line 9 will compile because Car extends Vehicle, therefore an object of type Car can be assigned to a reference of type Vehicle. Line 10 will not compile because the compiler will require an explicit down casting. Line 11 will not compile because class Boat and class Plane do not have any relationship of inheritance. To demonstrate this in code, the Answer.java file can be found in the Sample Code folder. © Accenture 2005 All Rights Reserved Course Code #Z16325

ATS Application Programming: Java Programming Key Points 3.2 Inheritance Inheritance is deriving a new class (subclass) from an existing class (superclass) Inheritance can exhibit an “is-a” or “has-a” relationship In Java, a class can only inherit from a single class All classes inherit from Object class – the highest in the inheritance hierarchy In Java, inheritance is implemented by the extends keyword this is a reference to the object of the current class super is a reference to the object of a superclass this() is used to call constructors within the same class super() is used to invoke constructors in the immediate superclass Casting is converting from one data type to another © Accenture 2005 All Rights Reserved Course Code #Z16325