Vous êtes sur la page 1sur 28

JAVA CONCEPTS

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

abstract class Bike{


Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}

class Honda extends Bike{


void run(){System.out.println("running safely..");}
}
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda(); //bike is created
obj.run(); //running safely..
obj.changeGear(); //gear changed
}
}
INTERFACES
• 100% abstract means we can not create body inside the interface.
• Inside the Interface all methods are abstract.
• Using Interface we can achieve 100%abstraction.

• Rules for Interface:


1. Interface should have subclass.
2. Subclass should override all abstract method.
3. If we are not overriding all abstract method, declare abstract for the
sub class.
4. Inside Interface all methods are public abstract.
5. While overriding abstract method inside the subclass we have to use
public as keyword
6. If a class is inheriting the Interface we should use implements as
keyword.
INTERFACES
7. To represent inheritance between classes and Interface we use
dotted line.
8. Interface will support for multiple Inheritance.
9. Interface supports 100% abstraction and standardization.
10. We can not use final keyword for Interface because subclass method
should override the Interface method.
11. 100% abstract means no constructor , No body for method, no
concreate method in the Interface no object.
12. Inside Interface all variables by default final public static.

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

• Casting is conversion of one form to another form


• There are 2 typed of Casting in java
1. Primitive casting
2. Object casting

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

• There are 6 Primitive data types for casting


1. byte[1 byte]
2. short[2 byte]
3. int[4 byte]
4. long[8 byte]
5. float[4 byte]
6. double [8 byte]
OBJECT 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

Interface IWebDriver Class Sample


{ {
view(); p s v m(string a[])
{
}
IWebDriver w=new FireFox();
w.view();//Firefox view
Class Firefox implements WebDriver
}
{
}
Public void view()
{
System.ou.println(“Firefox view”);
}
}
DOWN CASTING

• Storing super class object into subclass type .


• Whenever we perform up casting, the subclass features will be hidden.
In order to access sub class feature we should do down casting.
• Auto Down casting is not possible in java we have to do explicitly.
• Without up casting, down casting is not possible. Otherwise compiler
gives the error.
DOWN CASTING

Class Mobile Class Sample


{ {
void view() p s v m(string a[])
{ {
System.ou.println(“Mobile view”); Mobile m=new Lg();
} Lg l=(Lg) m;
m.view();// Lg view
} l.view();
Class Lg implements Mobile
{ }
void view() }
{
System.ou.println(“Lgview”);
}
}
POLYMORPHISM
• Change in the object behavior in different stages in the application is
called Polymorphism[same object
• To achieve Polymorphism
-Inheritance
-Method Overriding
-Up casting

In Polymorphism we have 2 types:


1. Compile time Polymorphism [early binding][static Polymorphism ]
2. Runtime Polymorphism [late binding][dynamic Polymorphism ]
POLYMORPHISM

Compile time Polymorphism [early binding][static Polymorphism ]


• Here method binding happens at the compile time itself
• Compile time Polymorphism is an example for method overloading

Runtime Polymorphism [late binding][dynamic Polymorphism ]


• Here method binding happens at the run time
• Compile time Polymorphism is an example for method overriding
POLYMORPHISM DEMO
interface IWebDriver
{
view();
} IWebDriver
class Firefox implements IWebDriver
{
public void view()
{
System.out.println("Firefox view")
}
}
class Chrome implements IWebDriver Firefox Chrome
{
public void view()
{
System.out.println("Chrome view")
}
}

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

• Hiding the complexity from the user and displaying required


functionalities is called Abstraction.
• To achieve abstraction we will always go for Interface.
• Example: Motor bike, Ac etc.
ABSTRACTION DEMO
interface ISwitch
{ class Helper
void on(); {
void off(); static void demo(char c)
} {
class Bulb implements ISwitch if(c=='B')
{ {
public void on() return new Bulb();
{ }
System.out.println("Bulb is on") else
} {
public void off() return new Fan();
{ }
System.out.println("Bulb is off") }
} }
} class Sample
{
class Fan implements ISwitch p s v m(String s[])
{ {
public void on() ISwtich s=Helper.demo('B');
{ s.on();
System.out.println("Fan is on") s.off();
} }
public void off() }
{
System.out.println("Fan is off")
}
}
PACKAGE IN JAVA

• Packages in Java is a mechanism to encapsulate a group of classes,


interfaces and sub packages.
• Package are used to segregate .class file and .java file.
• It is difficult to access package in the notepad.
• In order to access package we go for IDE's eclipse.(by using import
keyword)
ACCESS LEVEL

• Public  Application level access


• Protected Package level access + Inheritance (import)
• Default Package level access
• Private  Class level access
ENCAPSULATION

• 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;
}
}
}

public class Sample


{
p s v m(String a[])
{
A a=new A(10);
System.out.println("The value before a"+a.getA());//10
a.set(100);
System.out.println("The value before a"+a.getA());//100

}
}
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