JAVA Notes _ JBDL 76_Noida 4
JAVA Notes _ JBDL 76_Noida 4
JAVA Notes _ JBDL 76_Noida 4
Standalone Application:
Java Standalone Application” does not require the Internet. It runs on a local machine. It
takes its input from the local machine as well as saves its data to that local machine.
Ex. Calculator app and notepad, word etc
Web Applications:
The main purpose of web application is to generate dynamic response from server
machine.Web application provides services to web clients only(using the browser we
need to send the request).
Java:
1-Platform Independence:
2-Object-Oriented:
3-Memory Management:
4-Strongly Typed:
5-Multi-Threading Support:
6-Security:
The most unique feature of java is platform independent. In any programming language
source code is compiled into executable code . This cannot be run across all platforms.
When javac compiles a java program it generates an executable file called .class
file.This class file contains byte codes. Byte codes are interpreted only by JVM’s . Since
these JVM’s are made available across all platforms by Sun Microsystems, we can
execute this byte code in any platform. Byte code generated in the Windows
environment can also be executed in the Linux environment. This makes the java
platform independent.
JAVA Architecture:
● JDK is for Java development and includes the tools needed for compiling and
developing Java applications.
● JRE is for end-users and includes the JVM and libraries for running Java
applications.
● JVM executes Java bytecode and provides a runtime environment for Java
applications.
● JIT is a part of the JVM and improves the performance of Java applications by
compiling bytecode into native machine code at runtime.
Object:
An Object is an instance of class. A class defines a type of object. Each object
belongs to some class. Every object contains state and behavior. State is determined by
value of attributes and behavior is called method. Objects are also called as an
instance. Example:
● Class: Blueprint for a car (it defines that all cars have wheels, doors, and can
start/stop).
● Object: A specific car (like your red Toyota) made using that blueprint.
Constructors:
A constructor is a special method used to initialize objects. It is called
automatically when an object is created.
● No argument constructor
● Parameterized constructor
Example:
Access Modifiers:
(explain with example)
Below are the modifiers for classes, attributes, methods and constructors:
Modifiers Description
public Y Y Y Y
protected Y Y Y N
default Y Y N N
(no modifier)
private Y N N N
Encapsulation
In simple terms is the concept of wrapping data (variables) and the methods (functions)
that operate on the data into a single unit, which is typically a class in object-oriented
programming.
Wrapping the code into a single unit to provide you the control over data. We can make
read-only and write-only classes with this. Easy to test, can add custom functionalities
while getting and setting the data.
To achieve encapsulation:
To make private variables and public getter setters to access and update the values.
Data Hiding: It restricts direct access to some of the object's components, meaning that
the internal details (data) are hidden from outside the class. Instead, you interact with
the object's data through public methods.
Control Access: You control how the data is modified or accessed by providing
methods (getters and setters) that manage the data, which helps protect the data from
unintended or harmful changes.
Advantages of Encapsulation:
Inheritance:
● One class can inherit the features and behaviors of another class.
● Inheriting is the process of acquiring features of others.
● For example a child acquires the features of their parents.
● In java inheritance is the process of inheriting members of existing classes by
extending their functionality.
● We use extends keyword in java to extend a class in java.
● All java classes extend java.lang.Object since object class is the super class for
all classes in java.
● It always speaks about reusability.
SuperClass: Parent class/Base class from which features and behaviors are getting
inherited.
SubClass: Child class/Extended class/ Derived class which is inheriting the parent
class. It can have other functionalities as well.
○ Using a parent class reference, you can only call parent class methods, even if
the reference holds a child class object.
This Keyword:
OVERLOADING:
● COMPILE TIME / OVERLOADING/ EARLY BINDING POLYMORPHISM
● Two or more methods are said to be Overloaded if both methods have the same
name but change in the argument type.
● Do not depend upon the return type.
Eg:: abs(int)
abs(float)
abs(long)
Example:
class Animal {
void makeSound() {
System.out.println("generic sound of animal");
}
}
it prints the override value of child class because binding happens at run time which child class
object
—---------------------------------------------------
//Down casting --> means when parent class object try to store in child class reference
// 1--> Trying to Down casting Implicitly: Child c = new Parent(); - > compile time error
//2-->Down casting Explicitly: Child c = (Child)p; or Cat cat1= (Cat) new Animal();
// Cat cat1= (Cat) new Animal();// this results the class cast exception
/*
* You must ensure that the object you are casting (animal) is actually an instance of the
* example:
* */
Scenario 1::
Private methods are not visible in the Child classes hence the overriding concept is not
applicable for private methods.Based on our own requirement we can declare the same
Parent class private method in child class also. It is valid but not overriding.
Scenario 2::
If the method is declared as final in parent class then those methods we cannot override
in child class,it would result in a compile time error.
Scenario 3::
we can't override static method as instance method, it would result in compile time error.
Scenario 4::
we can't override instance(NON- STATIC) method as static,it would result in compile
time error
Rules with respect to scope
While Overriding we cannot reduce the scope of access modifier
eg1:: class Parent{public void methodOne(){}}
class Child extends Parent{protected void methodOne(){}}//Compile time error
Note
public => public
protected => protected/public
default => default/protected/public
private => Overriding concept is not applicable
Overriding => In both parent and child class method should be instance
Method resolution is based on a runtime object.
JVM will take the call so it is called as runtime polymorphism/dynamic dispatch.
Hiding => In both parent and child class methods should be static.
Method resolution is based on reference type.
Compiler will take the call so it is called Compile time polymorphism.
Method Overloading and Method Overriding in Java:
Example:
1. By using ATM,GUI screen bank people are highlighting a set of services that they are
offering without highlighting internal implementation.
Advantages of Abstraction
Abstract Class:
Instantiation of abstract class is not possible. Due to incomplete implementation.
Abstract Modifier
It is the modifier applicable only for methods and class but not for variables.
Abstract Methods
Even though we don't have implementation still we can declare a method with an abstract
modifier. That is abstract methods have only declaration but not implementation.
Example-
public abstract void methodOne(); => valid
public abstract void methodOne(){} => Invalid
● If a class contains at least one abstract method, make that class also abstract.
● Even if the class does not contain any abstract method still the class can be
made as abstract.
eg:: HttpServlet is an abstract class,but it does not contain abstract methods.
final class A{
public abstract void methodOne();//Invalid
}
abstract class A{
public final void methodOne(){...}//Valid
}
Interface:
An interface in Java is a blueprint for a class that defines a contract of methods that a
class must implement, but without providing any concrete implementations. It contains
abstract methods (methods without a body) and constants.
Interface Methods
● Every method present inside the interface is public.
● Every method present inside the method is abstract.
Interface variables
● Inside the interface we can define variables.
● Inside the interface variables define requirement level constants.
● Every variable present inside the interface is by default public ,static, and final.
2. A class can extend a class and can implement any no of interfaces simultaneously.
3. An interface can extend any number of the interfaces at a time.
In Java, you can provide an implementation inside an interface using default and
static methods, starting from Java 8. Here's how to do it:
1. Default Methods:
You can define a default method inside an interface, and it will have a default
implementation that can be used by classes implementing the interface. Here's an
example:
default void defaultMethod() {
System.out.println("This is a default method.");
}
The `defaultMethod` will be inherited from the interface, but the implementing class can
also override it if needed.
2. Static Methods:
You can also define static methods inside an interface, which are not inherited by
implementing classes. They can only be called using the interface name.
static void staticMethod() {
System.out.println("This is a static method.");
}
Differences between an Abstract Class and an Interface:
Purpose Used when only the requirements Used when some implementation
(specifications) are known, and no is provided but not complete,
implementation details are leaving room for further
provided. subclassing.
Variables All variables are public, static, Variables can be of any type; no
and final by default. restrictions on modifiers.
Blocks Static and instance blocks are not Can contain static and instance
allowed. blocks.
Multiple A class can implement multiple A class can inherit from only one
Inheritance interfaces, allowing multiple abstract class due to single
inheritance of behavior. inheritance in Java.
Default and Static Can define default and static Methods can be concrete, but
Methods (Java methods with implementations. there are no default methods.
8+)