Unit 6 OOP's Concepts
Unit 6 OOP's Concepts
Unit 6 OOP's Concepts
Data Hiding :- Data hiding it is the process of restricting the direct access to the data member
by providing indirect secured access via methods. With the help of data hiding we can provide
security for our data members by providing verification and validation.
for data hiding we can make a use of getter and setter method.
Getter method:- Getter method is used to get the data and fetch the data.
Setter Method:- Setter method is used to set the data and modify the data.
Ex. Without Data hiding
public class A1
{
public static void main(String[] args)
{
B b1=new B();
System.out.println(b1.a);
}
-------------------------------------------
public class B1
{
public static void main(String[] args)
{
int a=10;
}
}
Ex. With Data hiding
public class A
{
public static void main(String[] args)
{
B b1 = new B();
System.out.println(b1.get());
}
------------------------------------------
public class B
{
private int a=10;
2. We can prefix private access modifier only for member of the class.
3. If we prefix any data member with private access modifier then it’s written in the same
class we can access the private member directly.
4. But outside of the class if you want access private member the we should make use of
Getter and Setter method.
5. We can achieve the data hiding with the help of private keyword.
• Rules to be follow uses of private keyword in encapsulation:
1. If we want make our private member only readable then we make create only Getter
method.
2. If we want to make our data member both readable and writable then create Getter and
Setter Method.
3. We want to make our data member only writable then create only Setter method.
Ex. UML Diagram:
2. Inheritance:-
• Inheritance is the process acquiring properties from Parent class to Child class. It’s
comes under Is-a Relationship.
• Is-a relationship means parent and child relationship.
• All properties from Parent class will get inherited to child class expect private member,
constructor, multiline initializer.
• We can achieved inheritance between class to class with the help of extends keyword.
• We can achieved inheritance between class to interface with the help of implements
keyword.
Que: Why we used Inheritance in Java?
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
Phrase used in Inheritance
2. Sub Class or Child Class: the class which is acquiring(receiving, obtaining) the
properties of parent class is called as derived class, extended class, or child class.
3. Super Class/Parent Class: The class which giving the properties is called as base
class or a parent class.
1. Single level Inheritance:- Inheritance is possible only one level then such type of
inheritance is known as single level inheritance.
2. Multi level Inheritance:- Inheritance is possible more than one level then such type
of inheritance is known as multi level inheritance.
3. Hierarchical Inheritance:- If the parent class is having more than one child at same level
is known as hierarchical inheritance.
4. Multiple inheritance:- If the child class is having more one parent at same level it is
known as multiple inheritance. Multiple inheritance is not possible in java.
5. Hybrid Inheritance:- Hybrid inheritance is possible with the combination of any two
type of inheritance is known as hybrid inheritance.
• Super Call Statement:
Super is a keyword and it is used to access the member of super class (Parent Class). Super call statement is used to call
the constructor of parent class from child class.
When the object is created for child class, then super call statement is used to call the constructor of parent class to load
all the non-static members of that class into the object created.
Step to be followed
2. Call the constructor, if it is present then it will call the Parent class constructor. After that Parent class
constructor will call their Parent class constructor.
3. Check weather the non – static members are present in that parent class and load into object created inside
the heap area. And assign with default value.
4. After that check weather the non – static initializer is executing from top to bottom.
2. Super call statement should be the first statement inside the constructor.
3. We can not used multiple Super call statement inside same constructor because multiple
inheritance is not possible.
4. We can not used ‘super call statements’ as well as ‘this call statement’.
5. If the programmer fails to declared Super call statement then compiler will add no –
argument Super call statement.
• Difference Between Super( ) and Super:
Super( ) Super
3. It must be the first statement of the 3. It can be used anywhere inside the
constructor body. constructor body.
• Difference Between Super( ) and this( ):
Super( ) This( )
2. Can be added in all the constructor of a 2. Can be added only in n-1 constructor of
class. a class.
3. Compiler will implicitly add no –
argument super( ) to the constructor 3. Compiler doesn’t add this( ) implicitly.
which doesn’t have this( ).
Ex.
int a;
{
System.out.println(" From MLNSI 1 of Parent class ");
System.out.println(a);
a = 12;
}
Parent2()
{
super();
System.out.println(" From Parent() constructor");
}
{
System.out.println(a);
}
}
public class Child2 extends Parent2
{
int b;
{
System.out.println(" From MLNSI of Child Class");
a = 7;
b = 9;
}
Child2()
{
super();
System.out.println(" From child() constructor");
}
public static void main(String[] args)
{
Child2 ch1 = new Child2();
System.out.println(ch1.a);
System.out.println(ch1.b);
}
}
Ex.
public class Ola
{
String starting;
String destination;
Ola(String start, String end)
{
this.starting = start;
this.destination = end;
}
}
1. We can access the member of student object using the reference variable s1, s2, s3.
2. We can copy the reference of one variable to another variable only when int is of some
type.
• It is the process of converting one type of reference variable to another type of reference variable is known
as non – primitive type casting.
• There must exists Is-a relationship (Is-a relationship means Parent – Child class relationship).
1. Upcasting: It is the process of converting child class reference variable to Parent class
reference variable is known as upcasting.
It is the process creating object for child class and store it in the reference in parent class
reference variable is known as upcasting.
Ex.
Child ch1 = new child( ); // child type object.
Parent p1 = ch1; // child type reference variable/ converting child type reference variable to parent type
reference variable.
Ex.
Parent p1 = new Child( ); // Parent type reference variable.
Ex.
public class Parent
{
int a = 10;
}
----------------------------------------------------------------------------------------
public class Child extends Parent
{
int b = 20;
}
----------------------------------------------------------------------------------------
public class PCDriver
{
public static void main(String[] args)
{
Child ch1 = new Child();
System.out.println(ch1.a);
System.out.println(ch1.b);
System.out.println(" After upcasting ");
Parent p1 = ch1;
System.out.println(p1.a);// 10
//System.out.println(p1.b);// compile time error (using up-casting reference
variable we can not access child class member
}
}
Notes:
4. Ones if the reference variable got upcasting using upcasting reference variable we can
access only parent class member but not child class members.
Ola o = new Mini( ); Any type of its child class object can be stored
Ola o = new Micro( );
Ola o = new Prime( );
• Disadvantage of up – casting
1. Once the reference variable upcasted using up – casting reference variable we can not
access child class member. This is one and only the biggest disadvantage of upcasting.
2. To overcome this disadvantage of upcasting we are making use of down – casting
• Down – casting:
This process of converting parent class reference variable to child class reference variable is
known as down casting.
Ex.
public class Parent
{
int a = 10;
}
----------------------------------------------------------------------------------------
public class Child extends Parent
{
int b = 20;
}
----------------------------------------------------------------------------------------
public class PCDriver
{
public static void main(String[] args)
{
Child ch1 = new Child();
Parent p1 = ch1;// Upcasting
Parent p11 = (Parent)ch1;// Downcasting
System.out.println(ch1.a );
System.out.println(ch1.b );
}
}
Note:
1. Down – casting is not done implicitly by the compiler.
2. As a programmer we need to perform Down – casting using type cast operator.
Ex.
1. It is the unexpected event which occurs during execution of program (at runtime).
Ans: When we try to convert the ‘p1’ object reference to a specific child type and if the object
does not contain the actual runtime instance of that particular type of object then we get
ClassCastException.
• Instance of Operator:
4. If the specific object is of given type then it returns ‘true’ which means down – casting is
possible.
5. If the specific object is not of given type then it returns false which means Down – casting
is not possible.
6. If down – casting is not possible and still if we try to perform down – casting forcefully
then we get class cast exception.
Case 1:
------------------------------------------------------------------------------------
public class B extends A
{
int l = 20;
}
------------------------------------------------------------------------------------
public class C extends B
{
int m = 30;
}
------------------------------------------------------------------------------------
public class D extends C
{
int n = 40;
}
public class ABCDDriver
{
public static void main(String[] args)
{
A a1 = new B();//Up casting
System.out.println(a1.k);
B b1 = (B)a1;//Down casting
System.out.println(b1.k);
System.out.println(b1.l);
C c1 = (C)b1;//Down casting
System.out.println(c1.k);
System.out.println(c1.l);
System.out.println(c1.m);
D d1 = (D)a1;//Down casting
System.out.println(d1.k);
System.out.println(d1.l);
System.out.println(d1.m);
System.out.println(d1.n);
}
}
3. Polymorphism:
The object has the ability to undergoes in multiple forms or different forms is known
as Polymorphism.
If the binding is achieved during compile time and same behaviour is execute is known as
compile time polymorphism.
Binding means the association between method called statement and the method declaration
statement during compile time and same behaviour is executed is known as compile time
polymorphism.
1. If a class have more than one method with same name but different formal argument is
known as Method Overloading.
• Note: Method overloading is always based on method name and formal argument but not on
}
b. By changing type of Argument:
}
• Method Overloading with Type promotion:
Ex 1:
public class PolymorphismWithTypePromotion
{
public static void main(String[] args)
{
add(10, 20);
}
public static void add(int a, float b)
{
System.out.println("From add(int a, float b)");
}
public static void add(int a, char b)
{
System.out.println("From add(int a, char b)");
}
Note: In above program we perform auto widening. Convert int type of data to float using
type promotion.
Note:
1. In case of method overloading if no suitable method is found then our compiler will try to
perform type promotion.
2. If type promotion is possible then respective method will called. If not we get compile
time error.
3. Type promotion is possible only in case of widening but not on case of narrowing.
5. Type promotion will be done by the compiler to the nearest data type.
Que: Why main method always static?
Ans: Your java program execution is start from main method. When you run a Java program,
the Java Virtual Machine (JVM) looks for a public static void main(String[] args) method to
start executing the program. Because the main method is the starting point, it needs to be
accessible without creating an object of the class.
Since the main method is the starting point of the program and is called by the JVM before
any objects of the class are created, it cannot rely on the existence of any instances of the
class. Therefore, it must be declared as static so that it can be called on the class itself rather
than on an instance.
Making the main method as a static then it ensure the consistency in how it is invoked across
different java programs.
Declaring the main method as a static simplifies the program initialization process. You don't
need to create an object of the class just to execute the program.
Ex 2:
public class PolymorphismWithTypePromotion1
{
public static void main(String[] args)
{
add(5, 5.25);
}
public static void add(int a, double b)
{
System.out.println("From add(int a, double b)");
}
public static void add(double a, double b)
{
System.out.println("From add(double a, double b)");
}
}
Ex 3 : No Suitable method found
Note: Type promotion will done by the compiler to the nearest datatype
Ex 4: Ambiguity problem
1. While performing type promotion if there exist more than one method which can perform
type promotion in this case we get ambiguity error.
2. Because if there exist more than one method which can perform type promotion then our
compiler will get confuse to call which particular method this lead to ambiguity problem.
Ex 5: Don’t care about return type
}
Note: Method overloading is based on method signature but not on return type.
Que: Can we do method Overloading?
Ans: Yes, we can overload the main method but our JVM will call the main method which
accepts (String [] args).
Ex.
Binding means the association between method called statement and the method
declaration statement happens during runtime(During execution of the Program) and
different behaviour execute is known as Runtime Polymorphism, and it is also known
as Dynamic polymorphism.
1. If the parent class and child class is having same static method with same method
signature then it is called as method shadowing.
4. The method shadowing based on the reference variable type the respective class method
will get executed.
public class Parent
{
public static void test()
{
System.out.println(" From parent class test() ");
}
}
------------------------------------------------------------------------------------
public class Child extends Parent
{
public static void test()
{
System.out.println(" From Child Class test() ");
}
}
------------------------------------------------------------------------------------
public class PCDriver
{
public static void main(String[] args)
{
Parent p1 = new Parent();
p1.test();
Child ch1 = new Child();
ch1.test();
Parent p2 = ch1;//Upcasting
p2.test();
Child ch2 = (Child)p2;//Downcasting
ch2.test();
Parent p3 = new Child();
p3.test();
}
}
• Variable Shadowing
1. Variable Shadowing comes under compile time polymorphism, because the binding
achieve during compile time.
2. The variable used based on reference variable type but not based on runtime object.
3. Variable shadowing can be achieved by static variable as well as non – static variable.
Ex.
public class A
{
int a = 10;
}
------------------------------------------------------------------------------------
public class B extends A
{
int b = 20;
}
------------------------------------------------------------------------------------
public class C extends B
{
int c = 30;
}
------------------------------------------------------------------------------------
public class D extends C
{
int d = 40;
}
public class ABCDDriver
{
public static void main(String[] args)
{
C c1 = new C();
System.out.println(c1.c);
A a1 = c1;// Upcasting
System.out.println(a1.a);
B b1 = (B)a1;// Downcasting
System.out.println(b1.b);
D d1 = new D();
System.out.println(d1.d);
}
• Abstraction:
1. Showing essential feature by hiding the background details is known as abstraction.
3. It is the design process of hiding the implementation and by showing only the functionality
(Method Declaration).
Ans:
2. We can provide implementation for abstract component with the help of inheritance and method
overriding.
Abstract keyword:
Abstract Method:
1. Any method which is prefixed with abstract modifier is known as abstract method.
2. Abstract method are incomplete method, Because abstract methods doesn't contain any method
definition and method implementation.
3. Abstract method contain only Method Declaration (Non – static method) but not method
implementation, the child class should provide implementation for this abstract method.
Syntax:
Concrete method
1. The method which is not prefixed with abstract keyword is know as concrete method.
2. Concrete method always contain implementation.
Step 1: Create a class by using inheritance make the class as child to abstract class(Parent).
Step 2: Using method overriding provide implementation for all the abstract method of parent
class.
Note:
In case if the child class doesn’t want to provide implementation for the abstract
method of parent class then make the class as abstract the next upcoming child will have the
responsibility to provide implementation for all the abstract methods.
Ex.
public abstract class Mobile
{
abstract public void Mp3();
abstract public void Camera();
abstract public void Browser();
}
------------------------------------------------------------------------------------
public abstract class Samsung extends Mobile
{
@Override
public void Mp3()
{
System.out.println(" Samsung Mobile has Mp3 ");
}
}
public class S22 extends Samsung
{
@Override
public void Camera()
{
System.out.println(" S22 has 108MP Camera ");
}
@Override
public void Browser()
{
System.out.println(" S22 Works on 5G Network ");
}
}
------------------------------------------------------------------------------------
public class MobileDriver
{
public static void main(String[] args)
{
Mobile m1 = new S22();
m1.Mp3();
m1.Camera();
m1.Browser();
}
}
• Interface
1. Interface is a component which is used to achieve 100% abstraction.
2. Inside the interface non – static concreate method are not allow.
Syntax:
[Access Modifier] + interface + InterfaceName
{
// Statement;
}
3. When the program is executes the source code is converted to .class extension file. Means
Before the compilation.
4. After compilation .class extension interface prefixed with abstract keyword to run the .java
extension file.
5. When we compile the interface byte code will generated which in form of .class extension
file.
Ex.
1. Before the compilation source code
interface Demo
{
public void m1();
}
2. After compilation .class extension
abstract interface I1
{
public void m1();
}
Constructor’s Yes No
2. If the final keyword is prefix with class name then it can not be inherited and it is final
class.
3. If the final keyword gives to the variable then we can not be modify by default consider as
final.
4. If any method is prefix with final keyword then the method can not be overridden.
Ex.
interface Demo
{
final static int a = 10;
public static void main(String[] args)
{
System.out.println(a);
a = 20;// / CTE(Final variable cannot be modified)
}
}
Note: Inside interface static methods are allowed hence we can create main method an
interface therefor we can execute interface as well.
Ex. Final Variable must should initialize at the time of declaration.
interface Demo
{
int a;// The blank final field a may not have been initialized
}
Ex. We cannot create object because by default class is abstract ( implicitly compiler ).
interface Demo
{
int a = 10;
public static void main(String []args)
{
Demo d1 = new Demo();//Cannot instantiate the type Demo
// We cannot create object for interface.
}
}
Ex. Concreate Non – Static method is not possible in interface only concreate static method is accept.
interface Demo
{
public void test() //Abstract methods do not specify a body
{
//Statement;
}
}
Note: Whenever if we create non – static method inside an interface implicitly our compiler
will make it as “abstract”.(no implementation )
Ans: By using interface we can achieve 100% abstraction using interface we can provide
solution for diamond problem therefore we can achieve multiple inheritance with the help of
inheritance.
Que: What are all the member which will not get inherited from an interface?
Ans: From an interface static methods will never get inherited to the class to interface.
Que: Inheritance with respect to Interface?
Ans: An interface can inherited any number of interfaces at the same time with the help of
extends keyword.
Ex:
public interface I1
{
abstract public void m1();
}
------------------------------------------------------------------------------------
public interface I2 extends I1
{
public void m1();
}
Note: When an interface is inheriting another interface in this case the child interface need not
to provide implementation for abstract method of parent interface.
Ex.
public interface I1
{
public void m1();
public void m2();
public static void test();
}
Above program there are two non – static method abstract [m1(), m2()]. And one static
concrete test() method, total: 3 Methods
public interface I2 extends I1
{
public void m3();
}
Above program is only non – static method are inherited
i. public static void m1();
ii. public static void m2();
iii. public static void m3();
Ex. Interface can inherit any number of interface at same time (Multiple inheritance).
public interface I1
{
// Statement;
}
------------------------------------------------------------------------------------
public interface I2
{
// Statement;
}
------------------------------------------------------------------------------------
public interface I3 extends I1, I2
{
// Statement
}
Note: Using interface we don’t get any Dimond problem, hence we can achieve multiple
inheritance using interface.
• Solution on Diamond Problem:
2. Non – static concrete method are not allow in an interface therefor non – static method are
abstract.
3. Static concrete method will never get inherited in an another interface or form an interface.
}
• Step to be follow when performing solution of Diamond Problem
1. new DiamondSolution(); means constructor, first this statement call the super call
statement, means accepts parent class. but in class not have constructor then no create
ambiguity problem.
2. In two interface two method declaration with the same method name and same method
signature but in class doesn’t accepts same method name and same method signature then
it is provide one method in class for implementation.
3. A class can inherit multiple interfaces at the same time but not multiple classes at same
time because of Diamond Problem.
Note:
1. When a class is inheriting an interface in this case all the abstract methods of an interface
will get inherited to the child class hence this child class as the responsibility to provide
implementation to the abstract method of parent interface.
2. In case if the child class doesn’t want to provide implementation for abstract method of
parent interface then make the class as abstract.
3. Then next upcoming child will provide implementation to the abstract methods of an
abstract class.
Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can
have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static Interface has only static and final variables.
variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement An interface can extend another Java interface only.
multiple Java interfaces.
7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".
8) A Java abstract class can have class members like private, Members of a Java interface are public by default.
protected, etc.