Sca Oops
Sca Oops
What is OOP?
Part-1
Object
Oriented
Programmin
g
What is object-oriented
programming?
1. It is one of the most popular programming paradigm.
● Functional Programming
Class
Enhanced Security
Object
● Class
● Object
● Encapsulation
● Abstraction Simplicity
● Polymorphism
● Inheritance
Lecture - 2
Breed
Weight
Object
Question -
2. It contains
a. Data members/Attributes
b. Methods/Behaviour
Example -
Class
Syntax of Object Oriented Programming
<access modifier> class <class Name>
{
// Declaration of Data Types
<access modifier> <data_Type> <variable_Name> = value;
............... ......... ............ = .....;
............... ......... ............ = .....;
// Declaration of Methods
// Method Body
}
Driver
Class
Sample code of Object
Oriented programming class UseStudent
{
public static void main(String [] args)
Entity {
Class Student s;
s=new Student();
class Student
S.roll = 101;
{ S.name = “Amit”;
int roll; S.per = 82.9;
String name; System.out.println(“Roll:” + s.roll);
System.out.println(“Per:” + s.per);
}
}
}
Lecture - 3
Encapsulation
Part-3
Object
Oriented
Programmin
g
What Is
1. Encapsulation ? or wrapping the data and
Encapsulation is the process of binding
the codes that operates on the data into a single entity
● This means that your bank details are private, so that anyone cannot see your
account details.
● Only you will have access to your account details and that too using method
defined inside that class and this method will ask your account id and
password for authentication.
How to achieve or
implement Encapsulation
in Java
❖ There are two steps required to achieve or implement encapsulation
in Java program.
int roll;
String name;
double per;
}
Driver Class
class UseStudent
{
public static void main(String [] args)
{
Student s;
s=new Student();
s.roll = 101;
s.name = “Amit”;
s.per = 82.9;
System.out.println(“Roll:” + s.roll);
System.out.println(“Name:” + s.name);
System.out.println(“Per:” + s.per);
}
}
Correction In Previous Code
class Student{
System.out.println(“Roll no is ” +roll);
System.out.println(“Name is ” +name);
System.out.println(“Percentage is ”+per);
}
}
Correction In Previous Code
class UseStudent
{
public static void main(String [] args)
{
Student s;
s=new Student();
s.setData(101, “Amit”, 82.9);
s.showData();
}
}
Advantages Of Encapsulation
Lecture - 4
Abstraction
Part-4
Object
Oriented
Programmin
g
What Is Abstraction ?
Without Abstractio
Abstraction n
Another example to
Understand Abstraction
Without Abstractio
Abstraction n
Let's Understand Abstraction
With
public class Car { Code
private void moveBreakPads() {
}
private void changePistonSpeed(){
}
private void createSpark(){
}
public void turnOnCar(){
createSpark();
}
public void accelerate(){
changePistonSpeed();
}
public void brake(){
moveBreakPads();
}
}
Let's Understand Abstraction
With
//Driver Class Code
public class UseCar {
public static void main(String[] args) {
Car C=new Car(); //Create Object
C.turnOnCar();
C.accelerate();
C.brake();
}
}
Difference Between
Abstraction And Encapsulation
Abstraction Encapsulation
Hides the implementation details to Hides data for the purpose of data
reduce the code complexity protection
Constructor
Part-5
Object
Oriented
Programmin
g
What Is
Constructor?
A constructor is a special method of a class in object-
oriented programming that initializes a newly created
object of that class.
Constructors are methods
which…
1. Have the same name as that of the class.
class Account {
class Account { private int accid;
private int accid; private String name;
private String name; private double balance;
private double balance;
} public Account() {
}
}
A.show();
System.out.println(balance);
} }
} }
Entity
Class Parametrized
class Account Constructor
{
private int accid; Driver
private String name;
private double balance; Class
public Account(int id, String s, double b) {
accid=id; class CreateAccount
name=n;
balance=b; {
} public static void main(String [] args)
public void show() {
System.out.println(accid); {
System.out.println(name);
Account A;
System.out.println(balance);
} A=new Account(101,”Ravi”,56000);
}
A.show();
}
Lecture - 6
Getter &
Setter
Part-6
Object
Oriented
Programmin
g
What Is Getter and Setter
Vehicle v1;
public String getColor() {
return color;
v1=new Vehicle();
}
v1.setColor("Red");
Method
Overloadin
Part-7
g
Object
Oriented
Programmin
g
What Is Method Overloading
?
In Overloading?
Java, we do method overloading in three ways:
}
Lecture - 10
Part-
10
this
Keyword
Object
Oriented
Programmin
g
What Is this keyword ?
➢ “this” is a special object reference, which is also a keyword.
● Single
● Multilevel
● Multiple
● Hierarchical
● Hybrid
● Note: Java does not support multiple inheritance and
thus it also does not support hybrid inheritance
Single Inheritance:
In Single Inheritance one class extends another class (one class
only).
Multiple Inheritance:
Multiple Inheritance is one of the inheritance in OOP where one class
extends more than one class. Java does not support multiple
inheritance.
Multilevel Inheritance:
In Multilevel Inheritance, one class can inherit from a derived class.
Hence, the derived class becomes the base class for the new class.
Hierarchical Inheritance:
In Hierarchical Inheritance, one class is inherited by many sub classes.
Hybrid Inheritance:
Hybrid inheritance is one of the inheritance types in OOP which is a
combination of Single and Multiple inheritance.
SYNTAX OF INHERITANCE IN JAVA
class <class_name>
{
//body of class
//body of class
}
Driver
Let’s Understand
Class
With Example
public class Hospital {
public static void main(String args[]) {
Entity Class Surgeon s = new Surgeon();
s.qualification();
s.specialist();
class Doctor { }
public void qualification() {
System.out.println("Qual Details...");
}
}
Object
Oriented
Programmin
g
What Is “super”?
}
class B extends A
{
public void show()
{
display();
print();
}
}
Uses of “super”
Object
Oriented
Programmin
g
What is Method Overriding
Answer :-
To provide better/correct implementation
of that method in child class
System.out.println(“Sweet”);
}
class UseFruit{ }
➔ In this case he will use super and the syntax will be:
➔ super.<method_name>(<list_of_arg>);
Method Overriding V/s Method Overloading
Object
Oriented
Programmin
g
What is “final” keyword in java
final
class Circle {
private int radius;
private final double pi = 3.14; //Now this value is
fixed
class Circle {
private int radius;
private final double pi;
class Circle {
private int radius;
private final double pi = 3.14;
}
“final” method
// Driver Class
class UseBike {
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
“final” class
}
Some predefined final classes
provide by java
➔ Math
➔ String
➔ All Wrapper Classes like
◆ Integer
◆ Double
◆ Character
◆ etc.
Lecture -17 Part-
Polymorphis
m 17
Object
Oriented
Programmin
g
What is
➔
Polymorphism
The derivation of the word Polymorphism is from two different Greek
words- poly and morph.
➔ Runtime polymorphism
Compile time
polymorphism
➢ Compile-time polymorphism is also known as static polymorphism.
Object
Oriented
Programmin
g
Runtime
Polymorphism
➢ Runtime polymorphism is also called as Dynamic Method
Dispatch in java.
Object
Oriented
Programmin
g
Early Binding
❖ If the method being called is a static method, then
java selects the method by looking at the (class) of
the reference used in the call and not the type of
object to which the reference is pointing.
Let’s Understand
Entity Class
With Example
class Parent
{
Driver
public static void show()
{
Class
...
}
}
public class UseShape
class Child extends Parent
{
{
public static void main(String[] args)
public static void show()
{
{
Parent p = new Child
...
();
}
p.show();
}
}
Which method will }
execute ?
Late Binding
class Language{
}
}
➔ The problem with the code is that we have left the body
of the method greetings() empty
For ex:-
Object
Oriented
Programmin
g
Some important points
★ To make a method abstract it is compulsory to use the
keyword abstract in the method prototype.
class A
{
public abstract void show();
}
Error
!
abstract class A
{
public
Nowvoid
abstract it show();
is OK }
Some important points
A obj;// Ok
obj=new A();// Error
Some important points
★ An abstract class can CONTAIN concrete methods as well
as Constructors.
OR
➢ Constructors
➢ Private methods
➢ Final methods
Interface Part-
24
Object
Oriented
Programmin
g
What is an interface?
interface <interface_name>
{
<data_type> <var_name>=<value>;
.
.
.
<return_type> <method_name> (list_of_arg);
.
.
.
}
Let’s Understand With Example
Interface declaration
@Override
public void eat() {
System.out.println("Elephant eats grasses and leaves");
}
}
Let’s Understand With Example
Entity class
@Override
public void eat() {
System.out.println("Lion eats flesh");
}
}
Let’s Understand With Example
Driver class
class UseAnimal{
public static void main(Sring []args){
Animal obj;
3) Abstract class can have final, non- 3) Interface has only static and final
final, static and non-static variables. variables.
Abstract Interface
Class
4) Abstract class can provide the 4) Interface can't provide the
implementation of interface. implementation of abstract class.
9) Example: 9) Example:
2. Association (HAS-A)
What Is Inheritance ?
1. Inheritance (IS-A)
★ For Example:
★ A HAS-A relationship signifies that a class is associated with that is it holds object(s)
of another class in its body
★ For Example:
★ For instance, class A holds class B’s reference and can access all properties of class
B.
Entity Class Let’s Understand
class Engine { With Example
public void start() {
//Some Code Driver
}
} Class
class MusicPlayer {
public void start() {
//Some Code public class UseCar{
}
}
public static void main(String
class Car { args[]){
Engine engine = new Engine();
MusicPlayer player = new
MusicPlayer(); Car car = new Car();
car.startEngine();
public void startEngine() { car.startMusicPlayer();
engine.start();
}
public void startMusicPlayer() { //Some More Code
player.start(); }
}
}
}
Types of Relationship
Types
Part-
of
Association
27
Object
Oriented
Programmin
g
What Is Association ?
A HAS-A relationship signifies that a class is associated with that is it holds object(s)
of another class in its body
★ For Example:
★ In simple words we can say that, class A holds class B’s reference and can
access methods of class B.
Types Of
Association
Aggregation
★ Aggregation is often represented using a "has-a" relationship.
class MusicPlayer {
public void start() {
//Some Code
}
}
class Car {
private String name;
private MusicPlayer player;
}
Types
Part-
of
Association
28
Object
Oriented
Programmin
g
Composition
➔ A composition in Java between two objects associated with each other exists
when there is a strong relationship between one class and another.
➔ Means contained object cannot exist without the owner or container object.