0% found this document useful (0 votes)
34 views6 pages

4 Java Poo

Uploaded by

Rouahmed17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

4 Java Poo

Uploaded by

Rouahmed17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Abstraction in java

Abstraction is a concept of exposing only essential details and hiding implementation details. It is one of the
essential OOPs concept apart from encapsulation, inheritance and polymorphism. Abstraction retains
only information which is most relevant for the specific purpose.

Ways to achieve abstraction


You can achieve abstraction using two ways in java.

 Abstract class(0 to 100% abstraction)


 Interface (100% abstraction)

Abstract class in java


An abstract class is the class which is declared abstract and can have abstract or non abstract
methods. It should be extended by child class and should implement abstract method.

Abstract method in java:


Abstract method is the method which do not have implementation i.e. it does not have any body.

Interface in java
Interface is generally used to provide contract for class to implement. Interface do not have implementation of
any method.A class implements an interface, thereby inheriting the abstract methods of the interface. So it is kind of
signing a contract, you agree that if you implement this interface, then you have to use its methods.It is just a
pattern, it can not do anything itself.

Abstraction example
When you run above program, you will get below output:

Playing cricket
=================
Playing football

Abstraction vs Encapsulation

Encapsulation is process of binding related data(variables and methods) into a


class.We can achieve encapsulation using access modifers such as public, private,
protected.

Encapsulation is implementation of desired abstraction.

Abstraction is more of design level concept and helps you to provide only essential
details and hiding implementation details. We can achieve abstraction by abstract
class and interface.

Encapsulation in java with example


Encapsulation in java is the process of binding related data(variables) and
functionality(methods) into a single unit called class.
Encapsulation can be achieved by using access modifier such as public, private, protected or default, so your
class will be safe from unauthorized access by others and will be simple to maintain.
We can create fully encapsulated class by

 Making variables private


 Providing getters and setters methods for the accessing the variables.
 Encapsulation is also termed as data hiding because you are making variables private and variables can be
only accessed through public getters and setters.

Advantages of Encapsulation in java:

 It provides control over data. For example: If You want to check if age of
employee is greater than 18 in setter method(setAge(int age)). You can easily do
it in setter method without breaking any code.
 Increase reusability.
 Makes class easy to use for other clients.
 It helps the developer to write code more flexible and maintainable by binding
them into a single unit and use appropriate access modifier to access the code as
per need.

Difference between abstraction and encapsulation in java:

 Encapsulation means data hiding using getter and setters. Abstraction means
hiding implementation details using [abstract class and interface]
 Abstraction is more of design level concept and Encapsulation is more of
implementation level concept.
Polymorphism in java with example
Polymorphism means one name many forms. In Java, polymorphism can be achieved by method
overloading and method overriding.
There are two types of polymorphism in java.
 Compile time polymorphism.
 Run time polymorphism.

Compile time Polymorphism

Compile time Polymorphism is nothing but method overloading in java. You can define various
methods with same name but different method arguments.

If two or more methods have same name , but different argument then it is called method overloading.

Rules of Method overloading :


Number of
Overloaded method can have different number of arguments
Arguments

Date type Overload method can have different data type for argument

Return type can be changed but either number of argument or


Return type
data type of argument should also be changed..

Order of If you change sequence of arguments then it is also valid method


arguments overloading provided you have different data types arguments.

Constructor Can be overloaded

So you can overload method using three ways:

 By changing number of arguments


 By changing data type of arguments
 By changing sequence of arguments if they are of different types

If we change only return type, it will become ambiguous for compiler to figure out which method to call.That is why
you can not change only return type.

Runtime Polymorphism

Runtime Polymorphism is nothing but method overriding in java.If subclass is having same method as
base class then it is known as method overriding Or in another word, If subclass provides specific implementation to
any method which is present in its one of parents classes then it is known as method overriding.
If subclass is having same method as base class then it is known as method overriding Or in another words, If
subclass provides specific implementation to any method which is present in its one of parents classes then it is
known as method overriding

Rules for method overriding


Arguments Must not change

Return type Can’t change except for covariant (subtype) returns

Access
Must not be more restrictive. Can be less restrictive.
Modifier

Can reduce or eliminate but must not throw new/broader checked


Exceptions
exceptions

Contructor Can not be overriden

Static method Can not be overriden

final method Can not be overriden


Inheritance

The word Inheritance is quite familiar with everyone. In common terms, the word means
the bequeathing of property and characteristics from generation to generation. For
example, the property or characteristics of parents are handed down to their children
and the forthcoming generations.

Object Oriented Programming (commonly OOP) concepts are based on real life
examples, where every entity in existence can be represented as an object. Thus,
being one of the fundamental concepts in OOP, Inheritance is based on the example
we discussed earlier – bequeathing of properties and characteristics from parents to
their children.
Inheritance in Java, as derived from the concept of OOP, is defined as the process by
which a child class or object (known as subclass) inherits the behaviors and
properties(methods and variables) from its predecessors or parent class(known
as super class). Let us delve a little deeper into the concepts of Inheritance in java with
the following sections.

Basic Syntax

Inheritance in Java is implemented by the use of the keyword extends. This special
word makes the Java compiler understand that the current class is inheriting or
extending another class. Let us look at the following snippet example to understand the
basic syntax.

You might also like