Lecture 8
Lecture 8
Lecture 8
OOP Basics 3
Abstraction, Encapsulation, Inheritance, Polymorphism,
Abstract Class and Methods, Interfaces in Java
2
Abstraction
• One of the most fundamental concept of OOPs is Abstraction.
• Abstraction is a process where you show only “relevant” data and “hide”
unnecessary details of an object from the user.
• For example, when you login to your Amazon account online, you enter
your user_id and password and press login, what happens when you
press login, how the input data sent to amazon server, how it gets verified
is all abstracted away from the you.
3
Encapsulation
• Encapsulation is:
– Binding the data with the code that manipulates it.
– It keeps the data and the code safe from external interference
4
Encapsulation
• Similarly, same concept of encapsulation can be applied to code.
Encapsulated code should have following characteristics:
5
Encapsulation
• Encapsulation simply means binding object state(fields) and
behaviour(methods) together. If you are creating class, you are doing
encapsulation
• This way data can only be accessed by public methods thus making the
private fields and their implementation hidden for outside classes. That’s
why encapsulation is known as data hiding. Lets see an example to
understand this concept better. 6
Encapsulation
class EncapsulationDemo{
private String empName;
Output:
Employee Name: Super Mario
7
Inheritance
• The process by which one class acquires the properties(data members)
and functionalities(methods) of another class is called inheritance.
• Child Class:
– The class that extends the features of another class is known as child
class, sub class or derived class.
• Parent Class:
– The class whose properties and functionalities are used(inherited) by
another class is known as parent class, super class or Base class.
8
Inheritance
• Inheritance is the mechanism by which an object acquires the some/all
properties of another object.
• It supports the concept of hierarchical classification.
• For example: Car is a four wheeler vehicle so assume that we have a class
FourWheeler and a sub class of it named Car. Here Car acquires the
properties of a class FourWheeler. Other classifications could be a jeep, lorry,
van etc.
• FourWheeler defines a class of vehicles that have four wheels, and specific
range of engine power, load carrying capacity etc. Car (termed as a sub-class)
acquires these properties from FourWheeler, and has some specific
properties, which are different from other classifications of FourWheeler, such
as luxury, comfort, shape, size, usage etc.
• A car can have further classification such as an open car, small car, big car
etc, which will acquire the properties from both Four Wheeler and Car, but will
still have some specific properties. This way the level of hierarchy can be
extended to any level.
9
Inheritance
• Inheritance is a process of defining a new class based on an existing class
by extending its common data members and methods.
• Inheritance allows us to reuse of code, it improves reusability in your java
application.
• Note: The biggest advantage of Inheritance is that the code that is already
present in base class need not be rewritten in the child class.
• This means that the data members(instance variables) and methods of the
parent class can be used in the child class as.
• For example, lets say we have a class Animal that has a method sound().
Since this is a generic class so we can’t give it a implementation like:
Roar, Meow, Oink etc. We had to give a generic message.
11
Polymorphism
• Now lets say we two subclasses of Animal class: Horse and Cat that extends (see
Inheritance) Animal class. We can provide the implementation to the same method
like this:
• and
public class Cat extends Animal{
...
@Override
public void sound(){
System.out.println("Meow");
}
}
12
Polymorphism
• As you can see that although we had the common action for all
subclasses sound() but there were different ways to do the same action.
• Thus we can say that the action this method performs is based on the type
of object.
13
Polymorphism
• Polymorphism is the capability of a method to do different things based on
the object that it is acting upon.
• In other words, polymorphism allows you define one interface and have
multiple implementations. As we have seen in the above example that we
have defined the method sound() and have the multiple implementations
of it in the different-2 sub classes.
• Types of polymorphism
– 1. Method Overloading in Java
– 2. Method Overriding in Java
14
Method Overloading
• Method Overloading is a feature that allows a class to have more than one
method having the same name, if their argument lists are different.
• It is similar to constructor overloading in Java, that allows a class to have
more than one constructor having different argument lists.
• let’s get back to the point, when I say argument list it means the
parameters that a method has:
• For example the argument list of a method add(int a, int b) having two
parameters is different from the argument list of the method add(int a, int
b, int c) having three parameters.
15
Method Overloading
• Three ways to overload a method
• In order to overload a method, the argument lists of the methods must
differ in either of these:
1. Number of parameters.
add(int, int)
add(int, int, int)
16
Method Overriding
• Declaring a method in sub class which is already present in parent class
is known as method overriding. Overriding is done so that a child class
can give its own implementation to a method which is already provided by
the parent class. In this case the method in parent class is called
overridden method and the method in child class is called overriding
method.
• The purpose of Method Overriding is clear here. Child class wants to give
its own implementation so that when it calls this method, it prints Boy is
eating instead of Human is eating.
17
Method Overriding
class Human{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
• Output: 18
Boy is eating
Abstract Class & Abstract Method
• A class that is declared using “abstract” keyword is known as abstract
class. It can have abstract methods(methods without body) as well as
concrete methods (regular methods with body). A normal class(non-
abstract class) cannot have abstract methods.
• An abstract class can not be instantiated, which means you are not
allowed to create an object of it.
19
Abstract Class & Abstract Method
• So when we know that all the animal child classes will and should override
this method, then there is no point to implement this method in parent
class. Thus, making this method abstract would be the good choice as by
making this method abstract we force all the sub classes to implement this
method( otherwise you will get compilation error), also we need not to give
any implementation to this method in parent class.
• Since the Animal class has an abstract method, you must need to declare
this class abstract.
• Now each animal must have a sound, by making this method abstract we
made it compulsory to the child class to give implementation details to this
method. This way we ensures that every animal has a sound.
20
Abstract Class & Abstract Method
• Abstract class Example
//abstract parent class • Output:
abstract class Animal{
Woof
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{
• Points to Remember
– An abstract class must be declared with an abstract keyword.
– It can have abstract and non-abstract methods.
– It cannot be instantiated.
– It can have constructors and static methods also.
– It can have final methods which will force the subclass not to change the body
of the method.
22
Interface
• Interface looks like a class but it is not a class. An interface can have
methods and variables just like the class but the methods declared in
interface are by default abstract (only method signature, no body, see:
Java abstract method). Also, the variables declared in an interface are
public, static & final by default. We will cover this in detail, later in this
guide.
23
Interface
• Syntax:
• Interfaces are declared by specifying a keyword “interface”. E.g.:
interface MyInterface
{
/* All the methods are public abstract by default
* As you see they have no body
*/
public void method1();
public void method2();
}
24
Example of an Interface in Java
• This is how a class implements an interface. It has to provide the body of
all the methods that are declared in interface or in other words you can
say that class has to implement all the methods of interface.
interface MyInterface
{
/* compiler will treat them as:
* public abstract void method1();
* public abstract void method2();
*/
public void method1();
public void method2();
}
25
Example of an Interface in Java
class Demo implements MyInterface
{
/* This class must have to implement both the • Output:
abstract methods else you will get compilation error */ implementation of method1
27
The relationship between classes and
interfaces
• As shown in the figure given below, a class extends another class, an
interface extends another interface, but a class implements an interface.
28
Example of an Interface extends Interface
interface Inf1{
public void method1();
}
interface Inf2 extends Inf1 {
public void method2();
}
public class Demo implements Inf2{
/* Even though this class is only implementing the
* interface Inf2, it has to implement all the methods
* of Inf1 as well because the interface Inf2 extends Inf1
*/
public void method1(){
System.out.println("method1");
}
public void method2(){
System.out.println("method2");
}
public static void main(String args[]){
Inf2 obj = new Demo();
obj.method2();
}
}
29
Multiple inheritance in Java by
interface
• If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
30
Example class implements multiple
interfaces
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
Output: Hello
Welcome
31