0% found this document useful (0 votes)
7 views10 pages

Oops

The document provides an overview of Object-Oriented Programming (OOP) concepts, including data hiding, the four pillars of OOP (abstraction, encapsulation, inheritance, and polymorphism), and various types of relationships such as Is-A and Has-A. It explains inheritance with examples, discusses method overloading and overriding for polymorphism, and outlines abstraction through abstract classes and interfaces. The document also highlights the advantages of these OOP principles in terms of security, reusability, and maintainability.

Uploaded by

Chetan Vispute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Oops

The document provides an overview of Object-Oriented Programming (OOP) concepts, including data hiding, the four pillars of OOP (abstraction, encapsulation, inheritance, and polymorphism), and various types of relationships such as Is-A and Has-A. It explains inheritance with examples, discusses method overloading and overriding for polymorphism, and outlines abstraction through abstract classes and interfaces. The document also highlights the advantages of these OOP principles in terms of security, reusability, and maintainability.

Uploaded by

Chetan Vispute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

OOPS

 Data Hiding :-

- Our Internal data should not go outside directly i.e. outside person cannot Access our internal
data directly.

- By using private modifier we can implement Data Hiding.

 Pillars of Oops :-
01. Abstraction - User friendliness.
02. Encapsulation - Security
03. Inheritance – Reusability
04. Polymorphism – Flexibility

 Inheritance :-

I] Is-A Relationship / Parent-Child Relationship :-

- We can implement Parent-Child Relationship by using 'extends' keyword.

- The main use of Parent-Child Relationship is reusability.

- Whatever parent has by default available to child, but Child has by default not available to

parent.

- On the child reference we can call both parent and child class methods but on the parent

Reference we can only call parent specific methods.

- parent class reference can be used to hold child class object but by using that we can only

call parent specific method not child methods.

- child class reference can not be use to hold parent class object.

- Private method on parent class object can not be available in child class.
i] Single Level inheritance - Parent

Child

ii] Multi Level inheritance - Parent

Child

GrandChild

package parentChildRelationship;

public class Parent {


public static void main(String[] args) {

Parent p = new Parent();


p.methodOne();
// p.methodTwo(); methodTwo undefined

Child c = new Child();


c.methodOne();
c.methodTwo();
c.methodThree();

Parent p1 = new Child(); // we can hold Child object by using Parent reference.
p1.methodOne();

// Child c1 = new Parent(); // we can not hold Parent object by using Child reference.

GrandChild gc = new GrandChild();


gc.methodOne();
gc.methodTwo();
gc.methodThree();
gc.methodFour();

Child c2 = new GrandChild();


c2.methodOne();
c2.methodTwo();
c2.methodThree();

Parent p2 = new GrandChild();


p2.methodOne();

}
public static void methodOne() {
System.out.println("Parent Method One");
}
}
package parentChildRelationship; Console :

public class Child extends Parent { Parent Method One


public static void methodTwo() { Parent Method One
System.out.println("Child Method Two"); Child Method Two
} Child Method Three
public static void methodThree() { Parent Method One
System.out.println("Child Method Three"); Parent Method One
} Child Method Two
} Child Method Three
Child Method Four
Parent Method One
package parentChildRelationship; Child Method Two
Child Method Three
public class GrandChild extends Child { Parent Method One
public static void methodFour() {
System.out.println("Child Method Four");
}
}

iii] Multiple inheritance - Parent Parent

|____________|

Child

- Having more than one parent at the same level is called as Multiple inheritance.

- Any class can extends only one class at a time ie. can not extends more than one class

simultaneously hence java not support for multiple inheritance.

class A {}

class B {}

class C extends A,B {} <---- invalid. ie. Error - Ambiquity Problem.

interface A {}
interface B {}

interface C {} extends A,B {} <----- Valid

iv] Cyclic inheritance -

Parent <----> Child

ie. class A extends B{}

class B extends A{} <----- Not allowed in Java

II] HAS-A Relationship :-

- HAS-A Relationship also know as Composition & Aggregation.

- There is no specific keyword to implement HAS-A Relationship but mostly use 'new'

operator.

- The main Advantage of HAS-A Relationship is Reusablity.

- The main Disadvantage of HAS-A Relationship is increase dependency.

eg. : class Engine {

Engine Specific Functionality.

class Car {

Engine e = new Engine(); // We can use Engine Specific Functionality by

using Engine reference e.


}

Composition :-

- Without existing container object there is no chance of existing contained object.

- Then the relationship between container and contained object is called Composition.

- It is also called as Strong Association.

- Relation between Car and Engine is Strong Bonding ie. Composition.

Car

Music Player Engine

Aggregation :-

- It is also called as week Association.

- Relation between Car and Music player is week Bonding ie. Aggregation.

 Polymorphism :-
Same name with different forms.

I] Compile Time / Static / Early binding polymorphism -


This achive by using method overloading.
Method Over Loding :-
- Two methods are said to be over loded if and only if having same name but different
Argument type.
eg. MyMethod(int);
MyMethod(float);
MyMethod(String);
- While resolving over loading method exact match with argument always get high
priority.
- While resolving over loading method Child class will get more priority with parent
class.
- In over loading the method resolution is based on the reference type not on run time
object.
- Automatic Promoion :-

package methodOverLoding; Console :


public class MethodOverLoding {
Print My Data with int
Print My Data with char
public static void main(String[] args) {
Print My Data with float
Print My Data with String
MethodOverLoding ml = new MethodOverLoding();
ml.MyData(30);
ml.MyData('a');
ml.MyData(3.14f);
ml.MyData("Swapnil");
}
public void MyData(int i) {
System.out.println("Print My Data with int");
}
public void MyData(char ch) {
System.out.println("Print My Data with char");
}
public void MyData(float f) {
System.out.println("Print My Data with float");
}
public void MyData(String s) {
System.out.println("Print My Data with String");
}

II] Run Time / Dynamic / Late binding polymorphism -


This achive by using method overriding.
Method Over Riding :-
- Whataver parent has by default avalable to child by inheritance. If the child is not
satisfied with parent class method implementation then child is allow to redefine that
parent class method in child class by its own way this is called Overriding.
- In Over Riding method resolution is depend on run time object.
Rules :-
01. In overriding the method name and argument should be same.
02. Up to 1.4 version the return type must be same but from 1.5 onwaeds the co-varient
return type are allowed [Parent-Child].
03. We can't override private method.
04. While overriding we can't reduce the scop of overriding method.
- public -> private <--- Not Applicable.
- public -> protected <--- Not Applicable.
- private -> public <--- private we cant access out side the class.
- protected -> protected/public <--- Applicable.
- public -> public <--- Applicable.
- default -> protected/public/default <--- Applicable.
05. We can't override final method as non final.
06. But we can override non final method as final.
07. We can't reassigned final variable.
08. We can use final keyword at variable, method & class.
09. We can't override static method of parent class
10. We can't override non static method as static.
11. Overriding concept is not applicable for variable. because variable resolution is
totally based on reference type.

package methodOverRiding;

public class Demo { Console :


public static void main(String[] args) { 10 Lac Rs
10 Lac Rs + 10kg Gold
parent p = new parent(); Marrage
p.property(10); 10 Lac Rs + 10kg Gold

Child c = new Child();


c.property(50);
c.marry();

parent p1 = new Child();


p1.property(70);
}
}

package methodOverRiding;

public class parent {

public Object property(int pro) {

System.out.println("10 Lac Rs");


return new Object();
}
public void marry() {
System.out.println("Marrage");
}
}
package methodOverRiding;

public class Child extends parent {

@Override
public String property(int pro) {

System.out.println("10 Lac Rs + 10kg Gold");


return new String();
}
}

Abstraction :-
- To Hide internal implementation and just highlight the set of Services is called Abstraction.

- By using abstract class and interface we can implement Abstraction.

Example :- By using ATM User Interface Screen bank people highlighting the set of services

that they are offering without highlighting internal implementation.

Advantages of Abstraction :-

- We can achieve Security as we are not highlighting internal implementation.

- Enhancement will become very easy without affecting the end user as we can perform any

change in our internal system.

- It provides more flexibility to end user to use system very easily.

- It improves the maintainability of the application.

We can achive Abstraction by using 2 ways

01. Abstract keyword -

- abstract is keyword.

- We can use abstract keyword with class and method.

Abstract class :-
- If we used abstract keyword with class then that class is considered as Abstract class

and we can extend Abstract class.

- We can't create object of Abstract class but Abstract class has default constructor.

- Abstract class can have non Abstract method, hence it provides 0 to 100% Abstraction.

Abstract Method -

- Abstract Method dosent have body.

- We can't use static, private, final keyword with Abstract Method.

- Abstract class is nesesory to exicute Abstract Method.

- When action is common but implementation are different then we used Abstract Method.

package abstractKeyWord; Console :


public abstract class Demo {
10 Lac Rs
public abstract void myMethod(); // Abstract Method 10 Lac Rs + 10kg Gold
Marrage
public void demo() { // Non Abstract Method 10 Lac Rs + 10kg Gold
System.out.println("Non Abstract Method");
}
}

package abstractKeyWord;

public class Test extends Demo {

public static void main(String[] args) {


Test t = new Test();
t.myMethod();
t.demo();

}
@Override
public void myMethod() {
System.out.println("My Method Override");
}
}

02. Interface -

- We can create variable inside the interface.


- Every variable inside the interface is by default public static final.

- Every method inside the interface is public and abstract.

- We can't write non-abstract method in interface.

- We can't create an object in interface.

- Interface dosen't content an constructor.

- From java 1.8 we can write static method in side the interface.

- From java 1.8 we can write default method in side the interface.
package interfaceDemo;

public class Demo {

public static void main(String[] args) {


Student s = new StudentService(); Console :

s.showStudentData(); Show Student Data Method Get Called


s.getStudentData(); Get Student Data Method Get Called
s.registerStudentData(); Register Student Data Method Get Called

}
}

package interfaceDemo;

public interface Student {


int i=10; // By default public static final

void showStudentData(); // By default public and abstract


void getStudentData();
void registerStudentData();

package interfaceDemo;

public class StudentService implements Student {

@Override
public void showStudentData() {
System.out.println("Show Student Data Method Get Called");
}

@Override
public void getStudentData() {
System.out.println("Get Student Data Method Get Called");
}

@Override
public void registerStudentData() {
System.out.println("Register Student Data Method Get Called");
}
}

You might also like