4 Java Poo
4 Java Poo
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.
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
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.
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.
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 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.
Date type Overload method can have different data type for argument
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
Access
Must not be more restrictive. Can be less restrictive.
Modifier
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.