0% found this document useful (0 votes)
6 views40 pages

OO Programming Lecture 5

The document provides an overview of Object Oriented Programming (OOP) concepts, including encapsulation, inheritance, polymorphism, and abstraction. It explains each concept with definitions, advantages, and examples in Java programming. The document also discusses the differences between abstract classes and interfaces, as well as the types of inheritance.

Uploaded by

melakusatana
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)
6 views40 pages

OO Programming Lecture 5

The document provides an overview of Object Oriented Programming (OOP) concepts, including encapsulation, inheritance, polymorphism, and abstraction. It explains each concept with definitions, advantages, and examples in Java programming. The document also discusses the differences between abstract classes and interfaces, as well as the types of inheritance.

Uploaded by

melakusatana
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/ 40

Object Oriented Programming

010101010101010100010100010111001010101010010101010101010001010100010111001

Lecture Five
010101010010101010101010100010100010111001010101010010101010101010100010100
010111001010101010010101010101010100010100010111001010101010010101010101010
100010100010111001010101010010101010101010100010100010111001010101010010101
010101010100010100010111001010101010010101010101010100010100010111001010101
010010101010101010100010100010111001010101010010101010101010100010100010111

OOP Concepts
001010101010010101010101010100010100010111001010101010010101010101010100010
100010111001010101010010101010101010100010100010111001010101010010101010101
010100010100010111010101010010101010100101010101010101000101000101 Since
0101010101010101010101010101010101010101010
101010101000101000101110010101010100101010101010101000101000101110010101010
100101010101010101000101000101110010101010100101010101010101000101000101110
01010101010010101010101010100010100010111001010101010010
BY Basha K | Faculty of Computing and Software Engineering |AMIT
Lecture Five
OOP Concepts

1
Topics to be covered
♣ Encapsulation

♣ Inheritance

♣ Polymorphism

♣ Abstraction

2
1. Encapsulation
➢ Encapsulation is one of the four fundamental OOP concepts.

➢ The other three are inheritance, polymorphism, and abstraction.

➢ Encapsulation is a process of wrapping code and data together into a single


unit, for example capsule i.e. mixed of several medicines.

3
…cont’d
➢ It is the technique of making the fields in a class private and providing access to the fields
via public methods.
➢ If a field is declared private, it cannot be accessed by anyone outside the class, thereby
hiding the fields within the class.
➢ For this reason, encapsulation is also referred to as data hiding.
➢ We can create a fully encapsulated class in java by making all the data members of the
class private.
➢ Now we can use setter and getter methods to set and get the data in it.
4
Advantage of encapsulation in java
❖ The fields of a class can be made read-only or write-only.

❖ A class can have total control over what is stored in its fields.

❖ The users of a class do not know how the class stores its data.

❖ A class can change the data type of a field and users of the class do not need to
change any of their code.

5
public class Student public void setAge(int newAge)
{ {
private String name; this.age=newAge;}
private int age; public int getAge()
private String sex; {
public void setName(String return this.age;
newName) }
{ public void setSex(String newSex)
this.name=newName; {
} this.sex=newSex;
public String getName() }
{ public String getSex()
return this.name; {
} return this.sex;
}}
public class RunEncap

public static void main(String args[])

Student encap = new Student( );


encap.setName("Basha");

encap.setAge(20);

encap.setSex("M");

System.out.println("Your Name : " +encap.getName()+ " Age : "+ encap.getAge()+" Sex : "+ encap.getSex());

7
2. Inheritance
➢ Inheritance is defined as the process where one object acquires the properties of
another object.

➢ In other words, the derived class inherits the states and behaviors from the base
class.

➢ The derived class is also called sub-class and the base class is also known as super
class.
8
➢ With the use of inheritance the information is made manageable in a hierarchical
order.

➢ The extends keyword is used to determine whether one object IS - A type of


another.

➢ When we are using inheritance there is IS – A relationship.

➢ IS-A is a way of saying : This object is a type of that object.

9
…cont’d
Example to show how the extends achieve this.

public class Animal{


}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
}
10
…cont’d
➢ Now, from the above example, if we consider the IS – A relationship, we can say:
▪ Mammal is - A Animal.

▪ Reptile is - A Animal.

▪ Dog is A – Mammal.

▪ Hence, Dog is Animal as well.

➢ With use of the extends keyword the subclasses will be able to inherit all the properties of the
super class except for the private properties of the super class.

11
Types of inheritance
➢ On the basis of class, there can be three types of inheritance: single, multilevel and
hierarchical in java.
1. Single inheritance: is when a class extends another one class.

2. Multi level inheritance: is when one can inherit from derived class, thereby making
this derived class the base class for the new class.

3. Hierarchal inheritance: when one class is inherited by many classes.

12
…cont’d
➢ When a class extends multiple classes i.e. known as multiple inheritance.
➢ Java supports only single inheritance.

13
…cont’d

14
3. Polymorphism
➢ Polymorphism in java is a concept by which we can perform a single action by
different ways.

➢ Polymorphism is derived from two Greek words: poly and morphs.

➢ The word “poly” means many and “morphs” means forms.

➢ So polymorphism means many forms.

15
There are two types of polymorphism in java:

1. Compile time polymorphism ( Method Overloading).

2. Runtime polymorphism (Method Overriding).

➢ We can perform polymorphism in java by method overloading and method overriding.

➢ If you overload static method in java, it is the example of compile time polymorphism.

➢ Here, we will focus on runtime polymorphism in java.

16
Method Overloading
➢ If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.

Advantage of Method overloading

➢ If we have to perform only one operation, having same name of the


methods increases the readability of the program.

17
There are two ways to overload the method in java.

1. By changing number of arguments

2. By changing the data type

18
Method overloading: changing no. of arguments
➢ In this example, we are created two methods, first add( ) method performs
addition of two numbers and second method performs addition of three
numbers.

➢ In this example, we are creating static methods

…..so that we don’t need to create instance for calling methods.

19
class Adder{
public static int add(int a, int b){
return a+b;
}
Public static int add(int a,int b,int c)
{
return a+b+c;
}
Public static void main(String args[])
{
System.out.println(add(3,4));
System.out.println(add(4,5,6));
}
}
20
Method Overloading: changing data type of arguments

▪ In this example, we have created two methods that differs in data type.

▪ The first add method receives two integer arguments and second add

method receives two double arguments.

21
class Adder{
public static int add(int a, int b)
{
return a+b;
}
public static double add(double a, double b)
{
return a+b;
}
Public static void main(String args[]){
System.out.println(add(3,4));
System.out.println(add(2.4,8.6));
}
} 22
2. Run time polymorphism
➢ Run time polymorphism is a process in which a call to an overridden
method is resolved at runtime rather than compile time.

➢ In this process, an overridden method is called through the reference


variable of a super class.

➢ The determination of the method to be called is based on the object being


referred to by the reference variable.

23
Example of java runtime polymorphism
➢ In this example, we are creating two classes Vehicle and Bike.

➢ Bike class extends vehicle class and overrides its run method( ).

➢ We are calling the run method by the reference variable of parent class.

24
…cont’d
➢ Since it refers to the subclass object and subclass method overrides the

parent class method, sub class method is invoked at runtime.

➢ Since method invocation is determined by the JVM not compiler, it is

known runtime polymorphism.

25
public class Vehicle {
public void run( ){
System.out.println("Vehicle is running");
}}
class Bike extends Vehicle{
@Override
public void run( ){
System.out.println("Bike is running");
}}
class MainClass{
public static void main(String args[])
{
Vehicle v=new Vehicle( );
v.run( );
Bike b=new Bike( );
v.run( );
Vehicle c=new Bike();
c.run( );
}
26
4. Abstraction
➢ Abstraction is a process of hiding the implementation details and showing
only functionality to the user.

➢ Another way, it shows only important things to the user and hides the
internal details for example sending sms, you just type the text and send
the message.

➢ Data abstraction provides the outside world with only essential


information, in a process of representing essential features without
including implementation details
27
…cont’d
➢ In java, abstraction is achieved using abstract classes and interfaces.

Abstract class
➢ A class that is declared with abstract keyword , is known as abstract class in java.

➢ Abstraction refers to the ability to make a class abstract in OOP.

➢ It can have abstract and non-abstract methods (method with body).

➢ An abstract class is one that cannot be instantiated.

28
…cont’d
➢ All other functionality of the class still exists, and its fields, methods, and
constructors are all accessed in the same manner.

➢ You just cannot create an instance of the abstract class.

➢ If a class is abstract and cannot be instantiated, the class does not have much use
unless it is subclass.

➢ The abstract keyword is used to declare abstract class in java.

29
…cont’d
Abstract methods:
➢ If you want a class to contain a particular method but you want the actual
implementation of that method to be determined by child classes, you can declare
the method in the parent class as abstract.

➢ The abstract keyword is also used to declare a method as abstract.

➢ An abstract method consists of a method signature, but no method body.

30
…cont’d
➢ Abstract method would have no definition, and its signature is followed by a
semicolon, not curly braces.

➢ If a class contains an abstract method, the class must be abstract as well.

➢ Any child class must either override the abstract method or declare itself abstract.

➢ A child class that inherits an abstract method must override it.

➢ If they do not, they must be abstract and any of their children must override it.

31
Example
package pack; public class Dashen extends Bank{

public abstract class Bank { public float interest() {

public void deposit() System.out.println("Dashen bank interest


implimentetion...");
{
return 0.0f;
System.out.println("common deposit method across all
}
banks...");
}
}
public class Wegagen extends Bank{
public void withdraw()
public float interest()
{
{
System.out.println("common withdraw method across all
System.out.println("Wegagen bank interest
banks...");
implimentetion...");
}
return 0.0f;
public abstract float interest(); }
} }
32
public class Demo {
public static void main(String args[])
{
Bank w=new Wegagen();
w.deposit();
w.withdraw();
w.interest();

Bank d=new Dashen();


d.deposit();
d.withdraw();
d.interest();
}
}
33
Interface
➢ An interface is a collection of abstract methods.

➢ A class implements an interface, thereby inheriting the abstract methods of the


interface.

➢ A class describes the attributes and behaviors of an object.

➢ An interface contains behaviors that a class implements.

34
… cont’d
➢ The purpose of an interface is to specify behavior for a class.

➢ In other words, we can say that interface is a design contract. It specifies methods
and classes can implement the interface and thereby sign the contract.

35
…cont’d

Differences and similarity between abstract class and interface


➢ You can not instantiate an interface.
➢ An interface does not contain any constructor.
➢ All of the methods in an interface are abstract.
➢ An interface can not contain instance fields.
➢ The only fields that can appear in an interface must be both static and final.
➢ A class can implement more than one interface at a time.

36
…cont’d

Declaring interface

➢ The interface keyword is used to declare an interface.

➢ A class uses the implements keyword to implement an interface.

➢ By default all methods in interface are public and abstract.

37
Example
package pack;
interface Animal { public class Demo {
void run(); public static void main(String
void eat(); args[])
} {
class Dog implements Animal{ Animal animal=new Dog();
@Override animal.run();
public void run() {
animal.eat();
}
System.out.println("Dogs can run");
}
@Override
public void eat() {
System.out.println("Dogs can eat too");
}
}
38
THE END
Q?

39

You might also like