Javatext
Javatext
Encapsulation : Binding the data members and related operation into the class is
known as encapsulation.
Encapsulation is defined as the wrapping up of the data and methods under a single
unit .it also implements data hiding.
The encapsulation specifies that data and its related operation must be in the
class body.
It also specifies that the data members should be protected using access
specifiers.
The characteristics of java bean class :1)the class must be public.
2)Private variables
3)Public constructors
4)public getters and setters
Every class in java must have a super class, either user defined or compiler
defined.
If a class does not have user defined super class the compiler provide a default
super class by name Object.
Benefits: Code reusability
Easier Maintenance
Faster development
Polymorphism
Encapsulation
Readability
If the super class contains non-parameter constructor, the sub class constructor
makes implicit call.
Explicit Constructor call : The subclass constructor can make a call to super class
constructor by using (super ()) super calling statement.
If the super class contains parameterized constructor, the sub class should make
explicit call.(using super())
Constructor calling statement : Super() This makes a call to the super class
constructor it can call with parameter or without parameter.
Rules: 1)Super calling statement be used only inside the constructor body.
2)Super calling statement (super()) must be the first statement in the
constructor body .
3)In one constructor body, only one super calling statement is allowed.
Method Overloading: A class defining multiple method with same name but different
parameter list.
The parameter list should differ in any one of the
following :
1)parameter type
2)parameter length
3)parameter sequence
-In one class we can overload both static and non-
static methods.
-We can overload in the subclass with inherited
methods.
-Whenever we want to perform same operation on
different types of data or set of data we should apply method
overloading.
[Note]: We can overload main method, but the execution begins
only if the main method (String[] args) String array parameter.
The method overloading is used to achieve compile
time polymorphism.
Method Overriding: Inheriting method from super class and changing its
implementation in sub class according to sub class specification.
When a method inherits a superclass method with same
declaration its is called override.
It is used to change or modify the method body.
-age -empid
-gender -sal
-name -designation
@override @override
void details(){// } void
details(){ // }
Rules of abstract :
Rule 1 : When a class inherits an abstract class, we have to
override all the abstract methods and give body
Rule 2:When a class inherits an abstract class, and we don't want
to override the inherited abstract method, then make the subclass as abstract
class.
-String class also implicitly inherits the object class and has
overridden three methods.
1. toString()
2.hashcode()
3.equals()
1.toString- should have return string representation of the object(address) but in
string class it is overridden to return the data
2.hashcode - should have return a unique address or references of two objects but
in string class it is overridden to compare the data or content.
3.equals - should have compared the addresses or references of two objects but in
string class it is overridden to compare the data or content
Collection Framework:
->Data Structure : It is a way of storing /Arranging / Organizing the data.
- In other ways DS is arrangement of group of data
-Eg : Arrays ,Linked List
->Collection : Collection is predefined interface present in java.util package
- It was introduced from JDK 1.2
-It is used to store group of data
->Difference between array and collection
Array : Homogenous in nature.
- Fixed Size
- No predefined methods
- Generics not supported
- No underlined data
Collection : Both homogenous and heterogeneous in nature
- Dynamic size
- Many predefined methods available
- Generics supported
- Underlined data structured