Polymorphism
Polymorphism
Here binding takes place only once in its life time because of
this re-binding is not possible, so it also consider as static
binding.
Polymorphism
Run-Time Polymorphism
It can be achievable by –
• Inheritance
• Method Overriding
• Up-Casting
Polymorphism
Method Overriding
Subclass method maintaining the same method signature as that of
super class with different implementation is know as Method Over
Riding.
Or
Acquiring the property from super class and change its implementation
according to sub-class is consider as Method Over Riding.
Rules for Method Overriding
1. Inheritance
2. Method Signature should be same
3. Access Specifiers visibility can be increased / retained but cannot
decrease the visibility.
4. Final method cannot be over-ridded.
Non-Primitive Casting
Non-Primitive Casting
Converting one non-primitive information to
another non-primitive is known as Non-Primitive
casting.
Classified in to two types :
1. Up-Casting
2. Down-Casting
Non-Primitive Casting
Up-Casting
Converting sub-class type object to super class type
is known as Up-Casting.
Or
Create a sub-class object by providing super class
reference is called as Up-Casting.
Up-Casting is implicitly done by Compiler.
It is also know as Auto-Casting.
Non-Primitive Casting
WHY DO WE NEED UPCASTING ?
VARIABLE SHADOWING :
If the superclass and subclass have variables with same name
then it is known as variable shadowing.
Which variable is used, depend on what ?
In variable shadowing binding is done at compile time , hence it
is a compile time polymorphism. Variable used depends on the
reference type and does not depend on the type of object created.
NOTE :
● It is applicable for both static and non static variable.
● It is a compile time polymorphism.
● Variable usage depends on type of reference and does not
depend on type of object created.
METHOD SHADOWING
METHOD SHADOWING :
If a subclass and superclass have the static method with same
signature , it is known as method shadowing.
Which method implementation gets execute, depend on what ?
In method shadowing binding is done at compile time , hence it is
compile time polymorphism. The execution of the method depends on
the reference type and does not depend on the type of object created.
NOTE :
● Return type should be same
● Access modifier should be same or higher visibility than super
class method.
● Method shadowing is applicable only for the static method.
● It is compile time polymorphism
● Execution of implemented method depends on the reference
type of an object.
Packages
A package in java is used to group a related classes ,
interfaces and subclasses. In a simple word it is a folder
which consist of several classes and interfaces.
Note:
Packages contains only class files.
WHY PACKAGE ?
Packages are used to avoid name conflict.
It increases maintainability.
It is used to categorize classes and interfaces.
It increases the access protection.
It is used to achieve code reusability.
Import
Import statement is used to import the
classes or interfaces present in packages.
Import statement should be used before
declaring a class.
import statement should be end with ( ; ).
We can use multiple import statement in a
same program.
Packages
Package should be the first statement in a java
program.
A java source file should contain only one package
Statement.
A package can contain multiple classes/Interfaces but
one class/interface should be public.
If a package contains public class/interface then it is
mandatory to use public class/interface name as a
source file name. Otherwise, we will get Compile
Time Error.
Access Modifiers
Access Modifier are used to control the visibility
level of java components.
All access Modifier are the keywords in java.
Access Modifier are of 4 types:-
1. private
2. default
3. protected
4. public
Access Modifiers
Access Modifiers
private : If we declare class member as private then that member can be
accessible only within that particular class.
This members are not accessible outside the class body or in other class of
same package.
default: If we declare class member as default then that member can be
accessible in entire package, i.e., it can be used in different class of same
package also.
We don’t use any keyword for default access specifiers.
protected: If we declare class member as protected then that member can be
accessible in entire package and also in different package based on the
relationship.
public: if we declare class member as public then that member is accessible in
entire project.
Access Modifiers
Rule 1 :
Rule 2 :
Outer class can have either public/default access only, but inner class can have any access.
Rule 3 :
A java file can have multiple classes, but only one class should have public access.
The class which as public access must be the file name.
Access Modifiers
Rule 4:
Every class in java will have a constructor., if
programmer is not defining the constructor,
compiler will add the default constructor.
If it is default constructor it will have access level
as per that class.
If it is programmer defined constructor we can
given any access level.
ENCAPSULATION
The process of binding the data member of the class within the
class body into a single unit is called as Encapsulation.
Advantage of Encapsulation
Factory Class
It is an intermediate class between the
implementation class and the main method.
Factory Method
It is related to object creation we can create an
object without exposing the logic to user and it
use the common interface to create a new
Object.
Interface
Marker Interface
Loose Coupling
When ever one application is not fully dependent
another application for their functionality then it is
consider as loose coupling.