What is Encapsulation?
Encapsulation is one of the concepts in OOPs concepts; it is the process that binds together
the data and code into a single unit and keeps both from being safe from outside
interference and misuse. In this process, the data is hidden from other classes and can be
accessed only through the current class’s methods. Hence, it is also known as data hiding.
Encapsulation acts as a protective wrapper that prevents the code and data from being
accessed by outsiders. These are controlled through a well-defined interface.
Encapsulation is achieved by declaring the variables as private and providing public setter
and getter methods to modify and view the variable values. In encapsulation, the fields of a
class are made read-only or write-only. This method also improves reusability. Encapsulated
code is also easy to test for unit testing.
Example:
1class Animal {
2// private field
3private int age;
4//getter method
5Public int getage ( ) {
6return age;
7}
8//setter method
9public void setAge ( int age ) {
10this. Age = age;
11}
12}
13class Main {
14public static void main (String args []);
15//create an object of person
16Animal a1= new Animal ();
17//change age using setter
18a1.setAge (12);
19// access age using getter
20System.out.println(“ animal age is ” + a1. getage ( ) );
21}
22}
Output: Animal age is 12
In this example, we declared a private field called age that cannot be accessed outside of
the class.
To access age, we used public methods. These methods are called getter and setter
methods. Making age private allows us to restrict unauthorized access from outside the
class. Hence this is called data hiding.
What is Inheritance?
Inheritance is a method in which one object acquires/inherits another object’s properties,
and inheritance also supports hierarchical classification. The idea behind this is that we can
create new classes built on existing classes, i.e., when you inherit from an existing class, we
can reuse methods and fields of the parent class. Inheritance represents the parent-child
relationship. To know more about this concept check the free inheritance in java course.
For example, a whale is a part of the classification of marine animals, which is part of class
mammal, which is under that class of animal. We use hierarchical classification, i.e., top-
down classification. If we want to describe a more specific class of animals such as
mammals, they would have more specific attributes such as teeth; cold-blooded, warm-
blooded, etc. This comes under the subclass of animals whereas animals come under the
superclass. The subclass is a class which inherits properties of the superclass. This is also
called a derived class. A superclass is a base class or parental class from which a subclass
inherits properties.
We use inheritance mainly for method overriding and R:
To inherit a class, we use the extend keyword.
There are five types of inheritance single, multilevel, multiple, hybrid and hierarchical.
Single level
In this one class i.e., the derived class inherits properties from its parental class. This
enables code reusability and also adds new features to the code. Example: class b inherits
properties from class a.
Class A is the base or parental class and class b is the derived class.
Syntax:
1Class a {
2…
3}
4Class b extends class a {
5…
6}
Multilevel
This one class is derived from another class which is also derived from another class i.e., this
class has more than one parental class, hence it is called multilevel inheritance.
Syntax:
1Class a {
2….
3}
4Class b extends class a {
5….
6}
7Class c extends class b {
8…
9}
Hierarchical level
In this one parental class has two or more derived classes or we can say that two or more
child classes have one parental class.
Syntax:
1Class a {
2…
3}
4Class b extends class a {
5..
6}
7Class c extends class a {
8..
9}
Hybrid inheritance
This is the combination of multiple and multilevel inheritances and in java, multiple
inheritances are not supported as it leads to ambiguity and this type of inheritance can only
be achieved through interfaces.
Consider that class a is the parental or base class of class b and class c and in turn, class b
and class c are parental or a base class of class d. Class b and class c are derived classes from
class a and class d is derived class from class b and class c.
The following program creates a superclass called add and a subclass called sub, using
extend keyword to create a subclass add.
1// a simple example of inheritance
2//create a superclass
3Class Add {
4int my;
5int by;
6void setmyby (int xy, int hy) {
7my=xy;
8by=hy;
9}
10}
11/create a sub class
12class b extends add {
13int total;
14void sum () {
15public Static void main (String args [ ] ) {
16b subOb= new b ( );
17subOb. Setmyby (10, 12);
18subOb. Sum ( ) ;
19System.out.println(“total =” + subOb. Total);
20}
21}
It gives output as – total = 22
What is Polymorphism?
Polymorphism refers to many forms, or it is a process that performs a single action in
different ways. It occurs when we have many classes related to each other by inheritance.
Polymorphism is of two different types, i.e., compile-time polymorphism and runtime
polymorphism. One of the examples of Compile time polymorphism is that when we
overload a static method in java. Run time polymorphism also called a dynamic method
dispatch is a method in which a call to an overridden method is resolved at run time rather
than compile time. In this method, the overridden method is always called through the
reference variable. By using method overloading and method overriding, we can perform
polymorphism. Generally, the concept of polymorphism is often expressed as one interface,
and multiple methods. This reduces complexity by allowing the same interface to be used as
a general class of action.
Example:
1public class Bird {
2…
3Public void sound ( ) {
4System.out.println ( “ birds sounds “ );
5}
6}
7public class pigeon extends Bird {
8…
9@override
10public void sound ( ) {
11System.out.println( “ cooing ” ) ;
12}
13}
14public class sparrow extends Bird ( ) {
15….
16@override
17Public void sound ( ){
18System.out.println( “ chip ” ) ;
19}
20}
In the above example, we can see common action sound () but there are different ways to
do the same action. This is one of the examples which shows polymorphism.
Polymorphism in java can be classified into two types:
1. Static / Compile-Time Polymorphism
2. Dynamic / Runtime Polymorphism
What is Compile-Time Polymorphism in Java?
Compile-Time polymorphism in java is also known as Static Polymorphism. to resolved at
compile-time which is achieved through the Method Overloading.
What is Runtime Polymorphism in Java?
Runtime polymorphism in java is also known as Dynamic Binding which is used to call an
overridden method that is resolved dynamically at runtime rather than at compile time.