★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
this keyword in Java.pptx
1. This keyword in Java
1. this can be used to refer current class instance variable.
2. this can be used to invoke current class method (implicitly)
3. this() can be used to invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this can be used to return the current class instance from the method.
2.
3. Java static keyword
1. Variable (also known as a class variable)
2. Method (also known as a class method)
3. Block
4. Nested class
The static keyword belongs to the class than an instance of the class. If it were a non-static
method, JVM creates an object first then call main() method that will lead the problem of
extra memory allocation.
4.
5. Java static block
1. Is used to initialize the static data member.
2. It is executed before the main method at the time of classloading.
class Example{
static{System.out.println("static block is invoked");}
public static void main(String args[]){
System.out.println("Hello main");
}
}
6. Java static block rules
1. It executes during class loading
2. It can only use static variables
3. It can not use this
4. It executes only once during the entire execution of the program when the
class loads into the memory
8. Interfaces in Java
In java, an interface is similar to a class, but it contains abstract methods and static final variables only.
🔔 An interface is a container of abstract methods and static final variables.
🔔 An interface, implemented by a class. (class implements interface).
🔔 An interface may extend another interface. (Interface extends Interface).
🔔 An interface never implements another interface, or class.
🔔 A class may implement any number of interfaces.
🔔 We can not instantiate an interface.
🔔 Specifying the keyword abstract for interface methods is optional, it automatically added.
🔔 All the members of an interface are public by default.
9. interface HumanInterfaceExample {
void learn(String str);
void work();
int duration = 10;
}
Converted code
interface HumanInterfaceExample {
public abstract void learn(String
str);
public abstract void work();
public static final int duration =
10;
}
10. Difference Between Class and Interface
S.
No.
Class Interface
1. In class, you can instantiate variables and create an object.
In an interface, you can’t instantiate variables and
create an object.
2. Class can contain concrete(with implementation) methods
The interface cannot contain concrete(with
implementation) methods
3.
The access specifiers used with classes are private,
protected, and public.
In Interface only one specifier is used- Public.
11. Java Abstract Class
An abstract class is a class that created using abstract keyword. In other words, a class prefixed with abstract
keyword is known as an abstract class.
In java, an abstract class may contain abstract methods (methods without implementation) and also non-abstract
methods (methods with implementation).
Syntax
abstract class <ClassName>{
...
}
12. Java Abstract Class Rules
● An abstract class must be created with abstract keyword.
● An abstract class can be created without any abstract method.
● An abstract class may contain abstract methods and non-abstract methods.
● An abstract class may contain final methods that can not be overridden.
● An abstract class may contain static methods, but the abstract method can not be static.
● An abstract class may have a constructor that gets executed when the child class object
created.
● An abstract method must be overridden by the child class, otherwise, it must be defined as an
abstract class.
● An abstract class can not be instantiated but can be referenced.