Season 2
Season 2
It helps to design an object better and seems very close to the real world
Examples: studentName, RollNo, Gender(non static variables)
Study, read, write (non-static methods)
There are four basic pillars of OOPs:
1) Encapsulation
2) Inheritance
3) Polymorphism
4) Abstraction
Encapsulation
The process of binding or wrapping the states and behavior of an object in single unit is
known as Encapsulation.
State refers to non static variable and behavior refers to non static method.
Encapsulation can be achieved with the help of class.
The advantage of encapsulation is Data hiding.
Data hiding
It is the process of restricting direct access to the data member by providing indirect
secure access via methods.
With data hiding we can provide security for our data members.
The advantage of data hiding is verification and validation.
Step 1 - Prefix the data member with private keyword / Access modifier.
Example - private int age;
Step 2 - Use getter and setter methods to access the data member of another class.
Note
If you want to get the data then create only getter() method.
If you want to modify the data then only create the setter() method.
If you want to get the data as well as modify the data then create both getter() and
setter() method.
If you don't want to get the data or as well as modify the data then don't create any
getter() and setter() method.
Inheritance
Inheritance comes under IS-A relationship.
The relationship which is similar to parent and child is known as IS-A relationship
In IS-A relationship, all the properties of parent class will get inherited in the child class.
In Java we can achieve inheritance with the help of extends keyword and implements
keyword.
Using inheritance we can achieve generalization and specialization.
Parent class is known as generalized class and Child class is known as specialized class.
All the properties will get inherited from parent to child class except constructor, private
data members and multiline initializers.
In inheritance the properties of parent get inherited in the child class but the properties of
child class will never get inherited in the parent class.
Using the parent class reference variable, we can access only parent class members but not
child class member. Using child class reference variable we can access both parent class
member as well as the child class member.
Terminologies
Parent class
Types of inheritance
If the inheritance has more than one level, it’s known as multi level inheritance.
Example - class A1 {
String s1;
}
class B1 extends A1{
String s2;
}
class C1 extends B1{
String s3;
}
class D1 extends C1{
String s4;
}
Way - 1
Way - 2
Way - 3
3) Hierarchical inheritance
If a parent class has more than one child class at the same level, it’s hierarchical inheritance.
4) Multiple inheritance
If a child class has more than one parent at the same level, it’s multiple inheritance.
5) Hybrid inheritance
Hybrid inheritance is a combination of hierarchical inheritance and multiple inheritance
This is also not supported for Java (in class) due to diamond problem in multiple
inheritance
Example - class P1 {
String s;
}
class A1 extends P1{
String s1;
}
class B1 extends P1{
String s2;
}
class C1 extends A1, B1{
String s3;
}
To load all the non static member of parent class into the object created.
With the help of super() call statement we can pass data to non static members of a
parent class from the child class.
Note
We can access the member of student object by using the reference variable S1 or S2 or
S3 because all three reference variables are pointing to the same object.
We can copy the reference of one variable to another variable only when both the
variables are the same data type.
In case both reference variables are of different data types then we need to convert from
one reference to another reference .
The process of converting one type of object reference to another type of reference is
known as non primitive type casting.
Also known as derived type casting.
To convert one type of object reference to another type of reference, there should exist
IS-A relationship.
Or there should exist a common child.(Multiple inheritance)
Upcasting
The process of converting child type reference variable to parent type reference variable
is known as upcasting.
Example: Child ch=new Child();
Parent p1=ch; //upcasting
(Or) The process of creating an object for child and store it in parent type reference
variable is known as upcasting.
Example: Parent p1=new Child(); //upcasting
Once the reference variable gets upcasted using upcasted reference variable, we can
only access parent class member, not the child class members. (Disadvantage)
If we try accessing child class members using parent type reference variable, we get
compile time error.
Upcasting can be performed implicitly by our compiler.
We can perform Upcasting using type caste operator as well.
The process of converting parent type reference variable to child type reference variable
is known as downcasting.
Downcasting cannot be done implicitly by the compiler we need to perform down casting
explicitly using typecast operator.
Advantage of downcasting: we can access child class members.
ClassCastException
It is a runtime exception.
During downcasting we get ClassCastException which stops the program abnormally.
When we try to convert a reference variable to specific type (class), if the object does not
contain the instance of that particular type then we get a ClassCastException .
We can verify whether the object contain the instance of that particular type or not by
using instanceof operator.
Syntax
referenceVariable instanceof ClassType;
Note
The return type of instanceof operator is boolean.
If the object contain instance of that particular type then instance of operator will return
true, if not present it will return false.
You can only downcast an object if it was originally an instance of the child class.
In your case, p1 is an instance of Parent, not Child.
So, Child ch = (Child) p1; is invalid because p1 is not actually a Child instance.
Java will allow the explicit cast (Child) p1; at compile time.
But at runtime, when Java tries to cast p1 (which is purely a Parent object) to Child, it will
fail and throw:
Exception in thread "main" java.lang.ClassCastException: methodPro.Parent cannot be
cast to methodPro.Child
Instance of
Polymorphism
The word polymorphism is derived from two different Greek words:
poly = many/ various/numerous
morphism = forms/behavior
Polymorphism is the ability of an object to behave in many ways.
There are two types of polymorphism :
Compile time polymorphism
Runtime polymorphism
If the association between method called statement and the method declaration
statement is achieved to during compile time and same behavior is executed is known as
compile time polymorphism.
It is also known as static polymorphism.
We can achieve compile time polymorphism by the following ways :
1) Method Overloading
2) Constructor Overloading
3) Method Shadowing
4) Variable Shadowing
5) Operator Overloading (not supported by Java)
1) Method Overloading
If a class has more than one method with same name but different formal argument, it’s
known as method overloading.
Method overloading can be achieved with both static and non-static methods.
We can achieve method overloading in two ways:
1) by changing number of arguments.
2) by changing data type of arguments.
Method overloading is always based on method name and formal argument, but not on
return type of method.
For method overloading, if no suitable method is found - compiler tries to perform type
promotion. If type promotion is available, the respective method is called, if not - we get
a compile time error.
Type promotion is possible in case of widening but not in case of narrowing.
CTE
Type promotion will be always done by the compiler to the nearest data type.
CTE (Ambiguity error)
In case of type promotion if there exists more than one method, we get Ambiguity error.
CTE
CTE
3) Method shadowing
If the parent class and child class have the same static method with same method
declaration, it’s known as method shadowing.
Method shadowing is example of compile time polymorphism.
In method shadowing, all the methods should be static.
In method shadowing, the method gets executed based on the reference variable data
type.
4) Variable shadowing
If the parent class and child class have same variable with the same name, it’s variable
shadowing.
Variable shadowing can be achieved using both static variable as well as non static
variable.
Variable shadowing is an example of compile time polymorphism.
In case of variable shadowing the value will be used based on the reference variable type.
Runtime Polymorphism
The association between method call statement and the method declaration statement
which happens during runtime (execution of a program) is known as runtime
polymorphism.
It is also known as dynamic polymorphism.
Run time polymorphism can be achieved with the help of method overriding.
Method Overriding
Changing the implementation of parent class method inside the child class is known as
method overriding.
If the parent class and child class contain the same non static method with the same
declaration it’s known as method overriding.
Rules for achieving method overriding
Abstract
Abstract method
Any method which is prefix with the abstract modifier is known as abstract method.
Abstract methods are incomplete methods because they do not contain any method
implementation.
Syntax - [Access Modifier] [Modifier] returntype methodName([formal argument]);
Abstract Class
Any class which is prefixed with abstract keyword is known as abstract class.
If a class contains at least one abstract method or more than one abstract method, we
should make the classes abstract.
If a class does not contain any abstract methods then making the class abstract is
optional.
Note
Concrete class
Concrete method
The method which is not prefixed with abstract keyword is known as concrete method.
Concrete methods always contain implementation.
If a class is inheriting an abstract class, child class should provide implementation for all
the abstract methods of parent class.
In case the child class doesn’t provide implementation for abstract method of parent
class, it is mandatory to make the child class as abstract.
Steps to provide implementation for abstract method
Syntax
What are all the members that can be declared inside an interface?
From an interface static method will never get inherited to the class or interface.
An interface can inherit any number of interfaces at the same time with the help of extends
keyword.
CTS
When an interface is inheriting from another interface, child interface need not provide
implementation for abstract methods of parent interface.
Interface can inherit any number of interfaces at the same time(multiple inheritance).
CTS
Using interface we don’t get diamond problem, hence we can achieve multiple inheritance
using interface.
Reason
CTS
Solution to Diamond Problem: