You are on page 1of 22

(i)Encapsulation (ii)Inheritance (iii)Polymorphism

Mechanism to bind the code and the data it manipulates to protect it from outside interference and misuse. 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 implementation details .He can use the interfaces provided by u through public methods. Tight encapsulation is highly desirable
Aspire Institute - 9737966223

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

Resuabilty of code No Multiple inheritance Multi level Inheritance

Deadly Diamond of death


A 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)

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

Private Member :-can be accessed only within the class .They are not inherited . Default Member :- can be accessed by classes in same package only Protected Member :-can be accessed by classes in same package + all subclasses in other packages but that too through inheritance Public Member :-can be assessed by all classes in any package.

Access

modifiers are applicable to only members of

class. 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 possible private String name; // valid public String getName(){ // valid private int i ; // I is local variable so invalid code } }

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

Application require high cohesion More specialization More specialized the class , the more it is useful in other projects and application Also easier to maintain

class BudgetReport { void connectToRDBMS(){ } void generateBudgetReport() { } void saveToFile() { } void print() { } }

class BudgetReport { Options getReportingOptions() { } void generateBudgetReport(Options o) { } } class ConnectToRDBMS { DBconnection getRDBMS() { } } class PrintStuff { PrintOptions getPrintOptions() { } } class FileSaver { SaveOptions getFileSaveOptions() { } }

Can be applied to classes , methods and variables Final Class- cannot be inherited Eg String Final Method- cannot be overridden Final Variable -Constant

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

Use of same thing for different Purpose Real World Example

(i)Compile Time Method Overloading(in same class as well as through inheritance) For eg: In same class class A{ int add(int a,int b){ } int add(int a,int b,int c){ } } Through Inheritance Class A{ Int add(int a,int b){ } } Class B extends A{ Int add(int a,int b,int c){ } // different signature }

(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 signature return 2*(a+b); } }

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 original method does not throw any. But if original method throws an exception it can throw only its subclass exc.

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 Animal class for eg Dog ,Cat and Horse . At compile time it is impossible to decide which object will be passed as an argument.Also if u add a new subclass of Animal , the method will still work

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??..

You might also like