Lect 4 Inheritance Polymorphism Final Access Modifiers
-
Upload
kalgi-patel -
Category
Documents
-
view
216 -
download
0
Transcript of Lect 4 Inheritance Polymorphism Final Access Modifiers
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
1/22
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
2/22
(i)Encapsulation
(ii)Inheritance
(iii)Polymorphism
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
3/22
Mechanism to bind the code and the data itmanipulates to protect it from outside interference andmisuse.
Class must make its fields/instance varaibles private . Class should provide public methods(getters and
setters) for accessing those private members. For eg:- When u driver a car u actually dont know
how the engine works
User of ur class must not worry about implementationdetails .He can use the interfaces provided by uthrough public methods.
Tight encapsulation is highly desirable
Aspire Institute - 9737966223
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
4/22
Class Customer{ private int height; private int weight; public int getHeight(){ Return this.height;
} public int getWeight(){ Return this.weight; } public void setHeight(int height){ // user of Customer class is not
//worried about how the programmer sets the value
this.height=height; }public void setWeight(int weight){
this.weight=weight; } }
Aspire Institute - 9737966223
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
5/22
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
6/22
Resuabilty of code
No Multiple inheritance
Multi level Inheritance
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
7/22
Deadly Diamond of deathA
Int Add(int a,int b)
B
Overriding
Int Add(int a,int b)
C
Overriding
Int Add(int a,int b)
D
super.add();
Int Add(int a,int b)
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
8/22
Class Person { String name; String address; String contact; }
Class Employee extends Person{ String designation; Int empid; Int salary; } Class Customer extends Person{ String email; String noofvisits; }
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
9/22
Private Member :-can be accessed onlywithin the class .They are not inherited .
Default Member :- can be accessed byclasses in same package only
Protected Member :-can be accessed byclasses in same package + all subclasses in
other packages but that too throughinheritance Public Member :-can be assessed by all
classes in any package.
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
10/22
Access modifiers are applicable to only members ofclass.
A non inner class can only be public or default Access modifiers are not applicable to local variable
of method
For eg:- class Customer{ // default or public only possibleprivate String name; // valid
public String getName(){ // validprivate int i ; // I is local variable so invalid code } }
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
11/22
Class Animal { }
Class Dog extends Animal {
Int height,weight; } Class BullDog extends Dog{
}
// Dog IS-A animal
// BullDog IS-A Dog and animal both
// Dog HAS-A height and weight
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
12/22
Application require high cohesion
More specialization
More specialized the class , the more it isuseful in other projects and application
Also easier to maintain
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
13/22
class BudgetReport {
void connectToRDBMS(){ }
void generateBudgetReport() { } void saveToFile() { }
void print() { }
}
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
14/22
class BudgetReport { Options getReportingOptions() { } void generateBudgetReport(Options o) { } } class ConnectToRDBMS { DBconnection getRDBMS() { } } class PrintStuff { PrintOptions getPrintOptions() { } } class FileSaver { SaveOptions getFileSaveOptions() { } }
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
15/22
Can be applied to classes , methods and
variables
Final Class- cannot be inherited
Eg String
Final Method- cannot be overridden
Final Variable -Constant
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
16/22
class Animal{ final void eat(){ // subclass of animal cannot override
it }
void sleep(){ } } final class Horse extends Animal{
// Horse class cannot be etended
Public final WEIGHT=10; // WEIGHT is constant Overriding nonfinal sleep method Public void sleep{ } }
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
17/22
Use of same thing for different Purpose Real World Example
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
18/22
(i)Compile TimeMethod Overloading(in same class as well as through inheritance)For eg:In same classclass A{int add(int a,int b){ }int add(int a,int b,int c){ }}Through InheritanceClass A{Int add(int a,int b){ }}Class B extends A{Int add(int a,int b,int c){ } // different signature}
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
19/22
(ii)Run Time Method Overriding (Only through inheritance) =
Dynamic method dispatch For eg:Class A{int add(int a,int b) {return (a+b); }}Class B extends A {int add(int a,int b) { // same signaturereturn 2*(a+b);}}
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
20/22
For Over-riding (i)signature must be same (ii)return type must be same or subtype of the
type returned (iii)Access modifier must be same or less
stricter than that in original
(iv)can throw an exception if originalmethod does not throw any. But if originalmethod throws an exception it can throwonly its subclass exc.
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
21/22
c1 c =new c2(); // Reference is of Parent class but object is of
subclass Why it is allowed? Suppose u want a method which takes as an
argument one object of anysubclass of Animalclass for eg Dog ,Cat and Horse . At compile time
it is impossible to decide which object will bepassed as an argument.Also if u add a newsubclass of Animal , the method will still work
-
7/28/2019 Lect 4 Inheritance Polymorphism Final Access Modifiers
22/22
If u dont have a source code for a class and
there is one method named Abc() inside that
class which u want to behave in ur own way.All the other methods and variable u dont
want to change .What will u do??..