1 Tirgul no. 9 Topics covered: H Inheritance, the basics: - Inheritance Trees - base class vs. sub class. - Constructor invocation. - Overriding vs. Overloading.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
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 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.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Topic 4 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Programming in Java CSCI-2220 Object Oriented 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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
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)
Topics Instance variables, set and get methods Encapsulation
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
BY:- TOPS Technologies
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Data Structures and Algorithms revision
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Week 8 Lecture -3 Inheritance and Polymorphism
Object Oriented Programming
Computer Science II Exam 1 Review.
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
null, true, and false are also reserved.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Extending Classes.
Java Programming Language
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance CT1513.
Constructors under inheritance Variable Shadowing
Chapter 11 Inheritance and Encapsulation and Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Computer Science II for Majors
Presentation transcript:

1 Tirgul no. 9 Topics covered: H Inheritance, the basics: - Inheritance Trees - base class vs. sub class. - Constructor invocation. - Overriding vs. Overloading. - Inheritance examples.

2 Object Oriented Programming H 3 major Object Oriented Programming Concepts: Encapsulation: An object is responsible for its internal state. Access to data members is moderated and is usually by using the object’s methods. Inheritance: A class can extend an existing class, and will then inherit all of the extended classes’ data members and methods. Polymorphism: Any object may be referred to from different points of view.

3 Inheritance Tree Example: Cars Car PrivateCar CommercialCar Mazda Toyota Subaru Mazda121 Mazda323 Mazda626 MazdaLantis … … …

4 Inheritance: Keywords These concepts come from set theory,super set and sub set. super class - The class we inherited from. sub class - The class that inherited, a more specialized class. upcasting - using an object instead of it's base (super class) type. }

5 Base Class Example public class Matrix { private double data[][]; public Matrix(int rows,int cols) {...} public Matrix transpose() {...} public double elementAt(int row,int col) {..} public add(Matrix right) {...} : private subRows(int row1,int row2); }

6 Sub Class Example public class SquareMatrix extends Matrix { public SquareMatrix(int size) { super(size,size); } public boolean isIdentity() { for(int row=0 ; row<rowNum() ; row++) { for(int col=0 ; col<colNum() ; col++) { if(row == col) { if(elementAt(row,col)!=1) return false; } else if(elementAt(row,col)!=0) return false; } return true; }

7 Access Privileges * A sub-class has access to all methods and fields that are declared public in its super-class. * A sub-class cannot access any methods/fields that are declared private in its super-class. protected - this new specifier means only a sub-class or other objects in the same package have access to the field/method.

8 Constructor Invocation * The constructors are called either explicitly or implicitly from base-class to sub-class down the inheritance hierarchy. * The compiler forces invocation of base-class constructor as the first thing that happens in the sub-class constructor, this means you cannot catch any exception thrown in the base-class's constructor.

9 Inheritance hierarchy public class NoInheritence { public NoInheritence() { System.out.println("No Inheritance."); } public class OneInheritence extends NoInheritence { public OneInheritence() { System.out.println("One Inheritance."); } public class TwoInheritence extends OneInheritence { public TwoInheritence() { System.out.println("Two Inheritance."); } public static void main(String args[]) { TwoInheritence ti = new TwoInheritence(); } Invocation of const’ As previously described the constructors are invoked down the hierarchy from super-classes down to sub-classes. The result of running the TwoInheritance class will be : No Inheritance. One Inheritance. Two Inheritance.

10 Inheritance: More Examples Matrix SquareMatrix Employee SecretaryManager Director Vehicle Motor Vehicle PlaneBoatCar Racing carTruck Motor JetDieselgas

11 Method Overloading vs. Method Overriding Overloading - Several methods have the same name but different parameter lists and possibly different return types. We have been using this all the time without thinking about inheritance and it keeps working the same if the new method is defined in a sub-class. Overriding - This applies only to inheritance, a method in a sub-class that has the same name,parameter list and return type as a method in the super-class. The method in the sub-class hides the method in the super-class.

12 Overriding &Overloading Example public class SuperClass { public void method1(int i) { System.out.println("got: "+i); } public class SubClass extends SuperClass { public void method1(int i) { System.out.println("double is: "+ 2*i); } public String method1(int i, int j) { return ("got: " + i + " and "+j); } overriding overloading

13 Inheritance Example no. 1 public class Rectangle { private int length, width; // as expected, data members are marked 'private' public Rectangle() { this(10,10); } public Rectangle(int l, int w) { length=l; width= w; } public void print() { System.out.println("length= "+length); System.out.println("width= "+width); } public int area() { return length*width; }

14 Inheritance Example (cont.) import java.awt.*; // for using the class 'Color' public class ColorRectangle extends Rectangle{ private Color color; // the rest of the data members are already inside! public ColorRectangle(int l, int w, Color c) { super(l,w); // first, create the base class object. color= c; // then, add whatever extra features this new class has. } public ColorRectangle() { super(); // we usually omit this line color= Color.blue; // take a look at class Color API. } public Color getColor() { return color; } public void print() { super.print(); System.out.println(color); }

15 Inheritance Example no. 2 public class Account { private String name; private long number; private float balance; public Account(String name, long number) { this.name= name; this.number= number; balance= 0.0f; } public String getName(){ return name; } //continued on next slide…

16 Account class (cont.) //continuation of class Account… public long getNumber(){ return number; } public float getBalance(){ return balance; } public void deposit(float sum) { balance+=sum; } // returns 'boolean' for reasons yet to come public boolean withdraw(float sum) { balance-=sum; return true; } //cont. on next slide…

17 Account class (cont.) public void print() { System.out.println("name: " + name); System.out.println("number: " + number); System.out.println("balance: " + balance); } public String toString(){ return("account no. " + number + " name: " + name + " balance= " + balance); } } //end of class Account

18 Extending the Account Class public class RealAccount extends Account { private int credit; //constructors are not inherited! public RealAccount(String name, long number, int credit) { super(name,number); this.credit= credit; } //method overriding: public boolean withdraw(float sum) { if(getBalance()-sum<credit) return(false); else return(super.withdraw(sum)); } //cont. on next slide…

19 RealAccount (cont.) //method overriding – notice use of super public String toString(){ return(super.toString() + " credit= " + credit); } } //end of class RealAccount

20 Using the Account classes import java.util.*; public class Bank{ //data member definition: private Vector accounts; //default constructor - data member intialization: public Bank(){ accounts= new Vector(); } public void addAccount(Account ac){ accounts.addElement(ac); } //cont on next slide…

21 Bank class (cont.) public Account findAccount(long num){ for(int i=0; i<accounts.size(); i++){ //Account ac=(Account)accounts.elementAt(i); Object obj= accounts.elementAt(i); Account tempAcc=null; if(obj instanceof Account){ tempAcc= (Account)obj; if(tempAcc.getNumber() == num) return(tempAcc); } return null; } public String toString(){ return(accounts.toString()); } }//end of class Bank