Download presentation
Presentation is loading. Please wait.
1
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
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
3 Inheritance Tree Example: Cars Car PrivateCar CommercialCar Mazda Toyota Subaru Mazda121 Mazda323 Mazda626 MazdaLantis … … …
4
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
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
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
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
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
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
10 Inheritance: More Examples Matrix SquareMatrix Employee SecretaryManager Director Vehicle Motor Vehicle PlaneBoatCar Racing carTruck Motor JetDieselgas
11
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
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
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
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
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
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
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
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
19 RealAccount (cont.) //method overriding – notice use of super public String toString(){ return(super.toString() + " credit= " + credit); } } //end of class RealAccount
20
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
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.