Академический Документы
Профессиональный Документы
Культура Документы
4 PILLARS OF OOP
1. Inheritance (Extending the features from one class to another class)
2. Abstraction (Hiding the details from the user and providing only
functionalities)
3. Polymorphism (Change in the object behavior in different stages in the
application)
4. Encapsulation (Providing the indirect access for the private variables
through setters and getters )
ABSTRACT CLASS
• Class which is having abstract keyword is called as abstract class.
• Abstract class can have abstract method(method which does not have body)
or concreate method.
• In Abstract class we can not create an object. But we can have the
constructor that’s why abstract class is not 100% abstract class.
• In order to access abstract class –
- Inheritance
- Method overriding
• Use of Abstract class is Standardization.
• If a subclass is not overriding all abstract method, declare the subclass as
abstract class.
• Abstract method can not be static.
• Abstract class can not be final.
• Use extend keyword to access abstract class from subclass.
ABSTRACT CLASS DEMO
Syntax:
Interface IBase class Derived implements Ibase
{ {
void print(); public void print(){
S.O.P(“derived method”);
}
} }
INTERFACES
Understanding relationship between classes and interfaces
INTERFACES
Multiple Inheritance in java by Interface
DIFFERENCE BETWEEN INTERFACES
AND ABSTRACT CLASS
Interface Abstract Class
Contains only Abstract methods Contains both abstract method and
concreate methods
Methods are by default public abstract Methods are by default public or default
inside the Interface or protected abstract inside the abstract
class
Subclass inherits interface through Subclass inherits Abstract class through
implements keyword extends keyword
Interface can not have Constructor Abstract class can have Constructor
Variables inside the Interface are final Variables inside the abstract class are
final non final
Interface are 100% abstract Interface are not 100% abstract
Abstract class is used for Standardization Interface is used for
-Standardization
-Multiple Inheritance
-100% abstraction
CALL BY VALUE AND CALL BY
REFERENCE IN JAVA
• If a method is accepting only primitive type of variables as an argument
it is called as Call By Value
• Example
Class D
{
Void add(int a)
{
System.out.println(“The value of a is”+a);
}
}
Class Sample
{
P S V M(String c[])
{
D d1=new D();
d1.add(10);//The value of a is 10
}
}
CALL BY VALUE AND CALL BY
REFERENCE IN JAVA
• If a method is accepting only reference type of variables as an argument
it is called as Call By Reference
• Example
Class D
{
Void add(D d1)
{
System.out.println(“Welcome to java”);
}
}
Class Sample
{
P S V M(String args[])
{
D d1=new D();
d1.add(d1);//Welcome to Java
}
}
CASTING IN JAVA
1. Primitive casting:
Conversion of one primitive type into another primitive type.
Types of Primitive casting
I. Widenings (Conversion of lower data type to higher data type)
II. Narrowing (Conversion of higher data type to lower data type)
Widening is done by the compiler Autowideing [double int]
Narrowing is not by the compiler Explicit Narrowing [int(int)double]
PRIMITIVE TYPE CASTING IN JAVA
• Converting one class object type into another class object type
• In Object casting there are 2 types:
1. Up casting [Storing subclass object into super class type or
Interface]
2. Down casting [Storing superclass object into subclass type
(Without up casting down casting is not possible)]
UP CASTING
• Storing subclass object into super class type or Interface.
• Whenever we perform Up casting, the subclass features will be hidden.
• We can not access sub class features.
• To achieve up casting, there should be Inheritance .
• In case of method overriding we will get the latest implementation.
• In case of up casting and overriding we can access subclass features
• Up casting is done by the compiler automatically it is also called as
“AutoUpcasting”.
UP CASTING DEMO
class Helper
{
static void Demo(IWebDriver i)
{
i.view();
}
}
class Sample
{
p s v m(String a[])
{
Helper.Demo(new firefox());
Helper.Demo(new chrome());
}
}
ABSTRACTION
• Giving the indirect access to the private data members though setters
and getter is called as Encapsulation
• Get()returns the value
• Set()reinitialize the value
• The use of Encapsulation is for security
• In Encapsulation we will be having complete control on private data
member.
ENCAPSULATION DEMO
class A
{
private int a;
A(int a)
{
public int getA()
{
return a;
}
public void setA()
{
this.a=a;
}
}
}
}
}
DIFFERENCE BETWEEN ENCAPSULATION
AND ABSTRACTION
Encapsulation Abstraction
Giving the indirect access to the private Hiding the complexity from the user and
data members though setters and getter displaying required functionalities is
is called as Encapsulation called Abstraction.
We will be having complete control on We will be not having complete control
data members on data members
Security will be more Security will be less
There is no concept of Interface, method There should be Interface and method
overriding overriding
We should have Constructor No need of Constructor