The document discusses various Java concepts like static keyword, encapsulation, inheritance, and polymorphism. It provides examples and definitions for each concept. For static keyword, it explains how static methods/variables can be accessed without object and used in variables, methods, and blocks. For encapsulation, it discusses restricting access to some object components and provides a real-world example. Inheritance allows a class to acquire properties of a parent class using the "extends" keyword. Polymorphism allows objects to take multiple forms and provides unified interfaces using method overriding and overloading.
3. Static keyword
The static keyword is a non-access modifier used for methods
and attributes. Static methods/attributes can be accessed
without creating an object of a class
Static keyword can be used in
1.Static variable
2.static Method
3.Static Block
4. Why should we use Static Keyword?
1.Static keyword is used For memory management
2.It makes program more efficient by saving memory
6. Static Method does not require object declaration
package staticMethod;
public class StaticMethod {
void display1() {
System.out.println("I am non static method");
}
static void dispalay2() {
System.out.println("I am static method");
}
public static void main(String[] args) {
StaticMethod ob1=new StaticMethod();
ob1.display1();
Static Method
7. Some restriction on Static Method
1.static method can't use non static member
2. “this” and “super” keywords can't be use in static method
3.static can't use non static but can use static
8. Static Block
package staticBlock;
public class block {
static int id;
static String name;
static {
id= 101;
name="Abul Hasnat HRidoy";
}
static void display() {
System.out.println("ID :"+id);
System.out.println("Name :"+
name);
}
public static void main(String[] args) {
block.display();
9. Encapsulation is a way to restrict the direct access to
some components of an object, so users cannot access
state values for all of the variables of a particular object.
Encapsulation can be used to hide both data members
and data functions or methods associated with an
instantiated class or object.
What is encapsulation?
Mozadded Alfeshani Murad
221-15-6047
10. Mozadded Alfeshani Murad
221-15-6047
Encapsulation
Real world example of encapsulation :
The bag contains different stuffs like pen, pencil,
notebook etc within it, in order to get any stuff you
need to open that bag, similarly in java an
encapsulation unit contains its data and behavior
within it and in order to access them you need an
object of that unit.
12. Mozadded Alfeshani Murad
221-15-6047
Benefits of encapsulation programming
Encapsulation in programming has a few key
benefits.
Hiding data: Users will have no idea how classes are
being implemented or stored. All that users will know is
that values are being passed and initialized.
More flexibility: Enables you to set variables as red or
write-only. Examples include: setName(), setAge() or
to set variables as write-only then you only need to
omit the get methods like getName(), getAge() etc.
Easy to reuse: With encapsulation, it's easy to change
and adapt to new requirements
13. What is inheritance?
Inheritance is a mechanism in which one class acquires
all the properties and behaviors of a parent class.
In Java using “extends” keyword we inherit one class to
Another.
Why do we need Inheritance?
i.For code Reusability
ii.For method overriding
iii.To implement parent child relationship
INHERITANCE
14. Types of Inheritance in Java:
1.Single Level Inheritance
2.MultiLevel Inheritance
3. Hierarchical Inheritance
Single Level Inheritance:
When a class inherits another class, it is known as a single level inheritance
18. Polymorphism
Polymorphism is the ability of an object to take on many forms. It allows
objects of different classes to be treated as same type.
● It provides flexibility to the code by taking multiple forms.
● It simplifies the code by providing a unified interface.
19. Polymorphism
Method overriding
Method overriding is the ability of a subclass to
provide its own implementation of a method.
Method overloading
Method overloading is when a class
provides multiple methods with the
same name but different parameters
Polymorphism can be achieved in Java in two ways