Pillars of OOPs
Pillars of OOPs
Programming
in 60 sec
WhatisOOP
l. Class
2. Object
3. Inheritance
4. Abstraction
5. Encapsulation
6. Polymorphism
demonprogrammer swipe ➔
2. Inheritance
Inheriting or acquiring properties from the parent
class by a child class is know as Inheritance
• There are mainly 5 types:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Example: As child of your father you have authority
to get the share in property or assets of your family,
that mean a child class can access the methods
and properties of parent class.
class Parent {
String familyName = "xyz" ;
int wealth= 200000 ;
}
public class Main {
public static void main {String[] args) {
Child obj= new Child{);
String familyName = obj.familyName;
int wealth= obj.wealth;
System.out. println (familyName+ " " + wealth);
swipe ➔
demonprogrammer
3. Abstraction
Hiding the complex implementations and showing
only the functionality to user is know as Abstraction
// abstraction of buttons
abstract class TVRemote{
abstract void turnOnTV ();
abstract void turnOffTv ();
}
//implementation
class Buttons extends TVRemote {
void turnOnTV () {
System.out. println ( "Turning on TV" );
}
public class Main {
public static void main (String[] args) {
Buttons remote= new Buttons();
remote. turnOnTV ();
remote. turnOffTv ();
swipe ➔
demonprogrammer
4. Encapsulation
Group the data and methods in a particular class
and not allowing other classes to access the data
directly but only possible by methods of the class.
we have to define setter and getter to methods to
initialize data and to get the data.
class Person {
public void setName (String myName){ // method to initialize or to change the value
name= myName;
class Main {
public static void main (String[J args) {
Person obj= new Person();
obj. setName ( "John Wick" ); // setting the value by methods of person class
String name= obj. getName (); //getting th value of person name
System.out. println ( "My name is "+name);
swipe ➔
demonprogrammer
5. Polymorphism
Poly means many and Morph means shape, having
ability to change the execution of the programme
according the different functionalities is know
as Polymorphism
• There are mainly two types
1. Compile time polymorphism - the state of
execution will be declared at complie time which
is static in nature
• a. Method overloading - can be defined as
methods with same name but different
parameters
class Calculator {
int add (int a, int b){ // method with two parameters but same name
return a+b;
int add (int a, int b, int c){ //method with three parameters but same name
return a+b+c;
swipe ➔
demonprogrammer
b. Operator Overloading - java doesn't supprots
operator overloading
2. Runtime polymorphism - the execution state of
programme will be declared at runtime which is
dynamic in nature
a. Method Overriding - Overriding the execution of a
method by another method having same name
and same parameters but belongs to different
class is known as method overriding.
II method overriding
class calculator {
int add (int a, int b){
return a+b;
}
class AdvanceCalculator extends calculator {
int add (int a, int b) {
return a+b+10;
}
public class Main {
public static void main (String[] args) {
AdvanceCalculator obj= new AdvanceCalculator();
int result= obj. add ( l , 4 );
System.out. println (result);
demonprogrammer