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
A static method is a method that belongs to a class rather
than an instance of a class. This means you can call a static
method without creating an object of the class. Static
methods are sometimes called class methods.
Static Method does not require object declaration
7. 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();
StaticMethod.dispalay2();
}
Static Method
8. 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
9. Constructor
What is Constructor???
Constructor is a special type of method that is used to initialize the object.
Constructor on JAVA is used to create the instance of the class.
It’s a method , which can be overloaded and used for initializing an object.
10. Constructor
Properties of Constructor
Constructor has the same name as that of the class it belongs.
Constructor is a special type of method.
It has no return type not even void.
It is called automatically.
A constructor method is always public sometime it can be protected.
If a constructor is declared an private you can’t create any object of that class.
11. Types of Constructor
1.Default Constructor: A default constructor is a constructor that
either has no parameters or if it has parameters, all the parameters
have default values. If there is no constructor in a class, compiler
automatically creates a default constructor.
2.Parameterized Constructor: A constructor is called parameterized
constructor when it accepts a specific number of parameters. To initialize data
members of a class with distinct values.
13. 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.
14. 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
16. 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
18. 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
object and method to
Another class.
Why do we need Inheritance?
i.For code Reusability
ii.For method overriding
iii.To implement parent child relationship
19. 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 inheritance
23. Why multiple inheritance in possible in Java?
Java doesn't support multiple inheritances in classes because it can lead to diamond problem and
rather than providing some complex way to solve it, there are better ways through which we can
achieve the same result as multiple inheritances.
24. 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.
25. 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