OO Programming Lecture 5
OO Programming Lecture 5
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.
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
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.
9
…cont’d
Example to show how the extends achieve this.
▪ Reptile is - A Animal.
▪ Dog is A – Mammal.
➢ 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.
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.
15
There are two types of polymorphism in java:
➢ If you overload static method in java, it is the example of compile time polymorphism.
16
Method Overloading
➢ If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.
17
There are two ways to overload the method in java.
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.
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
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.
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
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.
Abstract class
➢ A class that is declared with abstract keyword , is known as abstract class in java.
28
…cont’d
➢ All other functionality of the class still exists, and its fields, methods, and
constructors are all accessed in the same manner.
➢ If a class is abstract and cannot be instantiated, the class does not have much use
unless it is subclass.
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.
30
…cont’d
➢ Abstract method would have no definition, and its signature is followed by a
semicolon, not curly braces.
➢ Any child class must either override the abstract method or declare itself abstract.
➢ 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{
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
36
…cont’d
Declaring interface
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