1 tirgul no. 9 topics covered: h inheritance, the basics: - inheritance trees - base class vs. sub...

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

Post on 20-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

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

1

Tirgul no. 9Tirgul no. 9

Topics covered:

Inheritance, the basics:

- Inheritance Trees

- base class vs. sub class.

- Constructor invocation.

- Overriding vs. Overloading.

- Inheritance examples.

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

2

Object Oriented ProgrammingObject Oriented Programming

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.

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

3

Inheritance Tree Example: CarsInheritance Tree Example: CarsCar

PrivateCar CommercialCar

MazdaToyota Subaru

Mazda121 Mazda323 Mazda626

MazdaLantis

……

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

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.

}

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

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);}

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

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; }}

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

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.

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

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.

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

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.

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

10

Inheritance: More Examples

Matrix

SquareMatrix

Employee

Secretary Manager

Director

Vehicle

Motor Vehicle

Plane BoatCar

Racing car Truck

Motor

Jet Diesel gas

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

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.

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

12

Overriding &OverloadingOverriding &OverloadingExampleExample

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

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

13

Inheritance Example no. 1public 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; }}

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

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); }}

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

15

Inheritance Example no. 2Inheritance Example no. 2public 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…

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

16

Account class (cont.)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 comepublic boolean withdraw(float sum) {

balance-=sum;return true;

} //cont. on next slide…

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

17

Account class (cont.)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

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

18

Extending the Account ClassExtending the Account Classpublic 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…

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

19

RealAccount (cont.)RealAccount (cont.)//method overriding – notice use of super

public String toString(){

return(super.toString() + " credit= " + credit);

}

} //end of class RealAccount

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

20

Using the Account classesUsing the Account classesimport 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…

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

21

Bank class (cont.)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