JAI SHREE RAM
Java
The static keyword in Java is used for memory management.
The static variable can be used to refer to the common property of all objects.
The static variable gets memory only once in the class area at the time of class loading.
Java main() method is always static, so that compiler can call it without the
creation of an object or before the creation of an object of the class.
In any Java program, the main() method is the starting point from where compiler
starts program execution. So, the compiler needs to call the main() method.
if the main() is allowed to be non-static, then while calling the main() method JVM has
to instantiate its class.
Static method of a class can be called by using the class name only without creating an
object of a class.
The main() method in Java must be declared public, static and void. If any of these
are missing, the Java program will compile but a runtime error will be thrown.
The main method is public in Java because it has to be invoked by the JVM. So, if main() is
not public in Java, the JVM won't call it. That's all about why the main method is declared
public and static in Java.
parameters (formal arguments) and instance variables are same. So, we are using this keyword to
distinguish local variable and instance variable.
If local variables(formal arguments) and instance variables are different, there is no need to use this
keyword like in the following program.
It is better approach to use meaningful names for variables. So we use same name for
instance variables and parameters in real time, and always use this keyword.
Usage of Java this keyword
this is a reference variable that refers to the current object.
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.
Cohesion is an indication of how related and focused the responsibilities of an software element.
Coupling refers to the knowledge or information or dependency of another class.
If a class has the details information of another class, there is strong coupling. In Java, we use private,
protected, and public modifiers to display the visibility level of a class, method, and field. You can use
interfaces for the weaker coupling because there is no concrete implementation.
Association
Association represents the relationship between the objects. Here, one object can be associated with
one object or many objects. There can be four types of association between the objects:
One to One
One to Many
Many to One, and
Many to Many
Aggregation
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A
relationship.
Aggregation is a way to achieve Association. Aggregation represents the relationship where one object
contains other objects as a part of its state. It represents the weak relationship between objects. It is also
termed as a has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another
way to reuse objects.
When an object A contains a reference to another object B or we can say Object A has a HAS-A
relationship with Object B, then it is termed as Aggregation. Aggregation helps in reusing the code
The composition is also a way to achieve Association.
The composition is a design technique in java to implement a has-a relationship. Java Inheritance is
used for code reuse purposes and the same we can do by using composition. The composition is
achieved by using an instance variable that refers to other objects.
Inheritance and composition are two programming techniques developers use to establish relationships
between classes and objects. Whereas inheritance derives one class from another, composition defines a
class as the sum of its parts.08-Jan-2020.
Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).
The idea behind inheritance in Java is that you can create new classes that are
built upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of the parent class. Moreover, you can add new methods and
fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
Overloading.
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
overriding
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
Rules for Java Method Overriding
1.The method must have the same name as in the parent class
2.The method must have the same parameter as in the parent class.
3.There must be an IS-A relationship (inheritance).
method overriding is mostly used in Runtime Polymorphism
A static method cannot be overridden.
No. Method Overloading Method Overriding
Method overriding is used to
provide the specific
Method overloading is used to increase
1) implementation of the method
the readability of the program.
that is already provided by its
super class.
Method overriding occurs in two
Method overloading is performed within
2) classes that have IS-A
class.
(inheritance) relationship.
In case of method
In case of method
3) overriding, parameter must be
overloading, parameter must be different.
same.
Method overriding is the
Method overloading is the example
4) example of run time
of compile time polymorphism.
polymorphism.
5) In java, method overloading can't be Return type must be same or
performed by changing return type of the covariant in method overriding.
method only. Return type can be same or
different in method overloading. But you
must have to change the parameter.
Java Method Overloading example
1. Class OverloadingExample{
2. static int add(int a,int b){return a+b;}
3. static int add(int a,int b,int c){return a+b+c;}
4. }
Java Method Overriding example
Class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
}
Super Keyword
The super keyword in Java is a reference variable which is used to refer
immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is
created implicitly which is referred by super reference variable.
Usage of Java super Keyword
1.super can be used to refer immediate parent class instance variable.
2.super can be used to invoke immediate parent class method.
3.super() can be used to invoke immediate parent class constructor.
Final
The final keyword is a non-access modifier used for a variable, a method, or a class.,
which makes them non-changeable (impossible to inherit or override). The final keyword is
useful when you want a variable to always store the same value, like PI (3.14159...).
java final keyword:-
-stop value change
-stop method overridding
-stop inheritance
If you make any variable as final, you cannot change the value of final variable(It will be
constant).
If you make any method as final, you cannot override it.
If you make any class as final, you cannot extend it.
final method is inherited but you cannot override it.
The new keyword is used to allocate memory at runtime. All objects get memory
in Heap memory area.
Blank final variable.
A final variable that is not initialized at the time of declaration is known as blank final variable.
Example:
class Student{
int id;
String name;
final String PAN_CARD_NUMBER;
...
}
Q.) Can we initialize blank final variable?
Yes, but only in constructor.
Class Bike10{
final int speedlimit;//blank final variable
Bike10(){
speedlimit=70;
System.out.println(speedlimit);
}
public static void main(String args[]){
new Bike10();
}
}
static blank final variable
A static final variable that is not initialized at the time of declaration is known as
static blank final variable. It can be initialized only in static block.
example
Class A{
static final int data;//static blank final variable
static{ data=50;}
public static void main(String args[]){
System.out.println(A.data);
}
}
Q.) Can we declare a constructor final?
No, because constructor is never inherited.
Polymorphism
Polymorphism is a concept by which we can perform a single action in different
ways.
There are two types of polymorphism in Java: compile-time polymorphism and
runtime polymorphism. We can perform polymorphism in java by method
overloading and method overriding.
Binding
Connecting a method call to the method body is known as binding.
There are two types of binding
1.Static Binding:- when type of the object is determined at compiled time, it
is known as static binding.
2.Dynamic Binding:- when type of the object is determined at run-time, it is
known as dynamic binding.
instanceof
The java instanceof operator is used to test whether the object is an instance of
the specified type (class or subclass or interface).
Abstraction
Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
Abstract class
A class which is declared as abstract is known as an abstract class. It can have
abstract and non-abstract methods. It needs to be extended and its method
implemented. It cannot be instantiated.
It can have constructors and static methods also.
A method which is declared as abstract and does not have implementation is
known as an abstract method.
No method body in abstract method.
If there is an abstract method in a class, that class must be abstract.
interface
An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
An interface is an abstract "class" that is used to group related methods with "empty" bodies:
To access the interface methods, the interface must be "implemented" (kinda like inherited) by
another class with the implements keyword (instead of extends ).
you can say that interfaces can have abstract methods and variables. It cannot
have a method body.
Java Interface also represents the IS-A relationship.
Why do we use an Interface?
It is used to achieve total abstraction. Since java does not support multiple inheritances in the
case of class, by using an interface it can achieve multiple inheritances. It is also used to
achieve loose coupling.
interface fields are public, static and final by default, and the methods are public and abstract.
A class extends another class, an interface extends another interface, but a class
implements an interface.
Difference between abstract class and
interface
Abstract class and interface both are used to achieve abstraction where we can
declare the abstract methods.
Abstract class Interface
Interface can have only abstract
1) Abstract class can have abstract
methods. Since Java 8, it can have
and non-abstract methods.
default and static methods also.
2) Abstract class doesn't support Interface supports multiple
multiple inheritance. inheritance.
3) Abstract class can have final, non-
Interface has only static and final
final, static and non-static
variables.
variables.
4) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
5) The abstract keyword is used to The interface keyword is used to
declare abstract class. declare interface.
6) An abstract class can extend
An interface can extend another Java
another Java class and implement
interface only.
multiple Java interfaces.
7) An abstract class can be extended An interface can be implemented
using keyword "extends". using keyword "implements".
8) A Java abstract class can have Members of a Java interface are public
class members like private, protected,
etc. by default.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface
achieves fully abstraction (100%).
package
A java package is a group of similar types of classes, interfaces and sub-
packages.
Package in java can be categorized in two form, built-in package and user-defined
package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io,
util, sql etc.
Advantage of Java Package
1) Java package is used to categorize the classes and interfaces so that they can
be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
There are four types of Java access modifiers:
1.Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.
2.Default: The access level of a default modifier is only within the package.
It cannot be accessed from outside the package. If you do not specify any
access level, it will be the default.
3.Protected: The access level of a protected modifier is within the package
and outside the package through child class. If you do not make the child
class, it cannot be accessed from outside the package.
4.Public: The access level of a public modifier is everywhere. It can be
accessed from within the class, outside the class, within the package and
outside the package.
Encapsulation
Encapsulation in Java is a powerful mechanism for storing the data members and data
methods of a class together.
We can create a fully encapsulated class in Java by making all the data members of the class
private. Now we can use setter and getter methods to set and get the data in it.
Advantage of Encapsulation
By providing only a setter or getter method, you can make the class read-only or write-
only.
it provides you the control over the data.
It is a way to achieve data hiding in Java because other class will not be able to
access the data through the private data members.
Array
Array is a collection of similar type of elements which has contiguous memory location. OR
An array is a container object that holds a fixed number of values of a single type. The length
of an array is established when the array is created. After creation, its length is fixed.
Datatype arrayName[] = new datatype[size];
Advantages
Code Optimization: It makes the code optimized, we can retrieve or sort
the data efficiently.
Random access: We can get any data located at an index position.
Disadvantages
SizeLimit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime.
Object class in Java
The Object class is the parent class of all the classes in java by default. In other
words, it is the topmost class of java.
Object Cloning in Java
The object cloning is a way to create exact copy of an object. The clone()
method of Object class is used to clone an object
The java.lang.Cloneable interface must be implemented by the class whose
object clone we want to create. If we don't implement Cloneable interface, clone()
method generates CloneNotSupportedException.
The clone() method is defined in the Object class.
Syntax of the clone() method is as follows:
protected Object clone() throws CloneNotSupportedException
Wrapper classes in Java
The wrapper class in Java provides the mechanism to convert primitive into
object and object into primitive.
Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects
and objects into primitives automatically. The automatic conversion of primitive
into an object is known as autoboxing and vice-versa unboxing.
constructor
A constructor in Java is a special method that is used to initialize the state of an object. The
constructor is called when an object of a class is created.
There are two rules defined for the constructor.
1.Constructor name must be the same as its class name
2.A Constructor must have no explicit return type
3.A Java constructor cannot be abstract, static, final, and synchronized
Note: We can use access modifiers while declaring a constructor. It controls the object
creation. In other words, we can have private, protected, public or default constructor in
Java.
A constructor is just like a method but without return type. It can also be overloaded like Java
methods.
Encapsulation in Java refers to integrating data (variables) and code (methods) into a single
unit. like a class in Java.
Why Encapsulation?
-In Java, encapsulation helps us to keep related fields and methods
together, which makes our code cleaner and easy to read.
-It helps to control the values of our data fields.
It is a way to achieve data hiding in Java because other class will not be able to
access the data through the private data members.
Arrays
Arrays are objects in Java that store multiple variables of the same type. Arrays can
hold either primitives or object references, but the array itself will always be an
object on the heap,
Advantages
Code Optimization:It makes the code optimized, we can retrieve or sort
the data efficiently.
Random access:We can get any data located at an index position.
Disadvantages
Size Limit:We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection framework
is used in Java which grows automatically.
The wrapper class in Java provides the mechanism to convert primitive into object
and object into primitive.