Java (1)
Java (1)
Gosling in May 1995 and later acquired by Oracle Corporation and is widely used
for developing applications for desktop, web, and mobile devices.
Key Features of Java
1. Platform Independent 2. 2. Object-Oriented Programming
3. Simplicity 4. Robustness 5. Security 6. Distributed
7. Multithreading 8. Portability 9. High Performance
Data types in Java are of different sizes and values that can be stored in the
variable.
Primitive Data Type: such as boolean, char, int, short, byte, long, float, and
double.
Non-Primitive (Reference) Data Types
1. Strings 2. Class 3.Object 4.Interface 5. Array
Literal: Any constant value which can be assigned to the variable is called
literal/constant.
Java Operators
Java operators are special symbols that perform operations on variables or
values. They can be classified into several categories based on their
functionality. These operators play a crucial role in performing arithmetic,
logical, relational, and bitwise operations etc.
Arrays in Java
Arrays are fundamental structures in Java that allow us to store multiple values
of the same type in a single variable. They are useful for storing and managing
collections of data.
For primitive arrays, elements are stored in a contiguous memory location.
For non-primitive arrays, references are stored at contiguous locations, but the
actual objects may be at different locations in memory.
Java Classes
A class in Java is a set of objects which shares common characteristics and
common properties. It is a user-defined blueprint or prototype from which
objects are created. For example, Student is a class while a particular student
named Ravi is an object.
Properties of Java Classes
Class is not a real-world entity. It is just a template or blueprint or
prototype from which objects are created.
Class does not occupy memory.
Class is a group of variables of different data types and a group of
methods.
Java Objects
An object in Java is a basic unit of Object-Oriented Programming and
represents real-life entities. Objects are the instances of a class that are
created to use the attributes and methods of a class. A typical Java
program creates many objects, which as you know, interact by invoking
methods. An object consists of:
class GFG
{
public static void main(String[] args)
{
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name + " and GeekId :" +
geek1.id);
}
}
METHOD OVERLOADING
Method Overloading allows different methods to have the same
name, but different signatures where the signature can differ by the
number of input parameters or type of input parameters, or a mixture
of both.
Method overloading in Java is also known as Compile-time
Polymorphism, Static Polymorphism, or Early binding. In Method
overloading compared to the parent argument, the child argument
will get the highest priority.
E.g.
public class Sum {
public int sum(int x, int y) { return (x + y); }
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public double sum(double x, double y)
{
return (x + y);
}
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
Overriding in Java
Overriding in Java occurs when a subclass implements a method
which is already defined in the superclass or Base Class. The
method in the subclass must have the same signature as in the
superclass. It allows the subclass to modify the inherited methods.
E.g.
class Animal {
void move() {
System.out.println( "Animal is moving.");
}
void eat() {
System.out.println( "Animal is eating.");
}
}
class Dog extends Animal {
@Override void move() {
System.out.println("Dog is running.");
}
void bark() { System.out.println("Dog is barking."); }
}
public class Geeks {
public static void main(String[] args)
{
Dog d = new Dog();
d.move();
d.eat();
d.bark();}
}
Usage of Java Method Overriding
1. Method overriding is used to provide the specific implementation of a method
that is already provided by its superclass.
2. Method overriding is used for runtime polymorphism.
3. Method overriding allows subclasses to reuse and build upon the functionality
provided by their superclass, reducing redundancy and promoting modular code
design.
4. Subclasses can override methods to tailor them to their specific needs or to
implement specialized behavior that is unique to the subclass.
5. Method overriding enables dynamic method dispatch, where the actual
method implementation to be executed is determined at runtime based on the
type of object, supporting flexibility and polymorphic behavior.
1. Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits
the properties and behavior of a single-parent class.
import java.util.*;
class One {
public void print_value()
{
System.out.println("Value 1");
}
}
class Two extends One {
public void print_for() {
System.out.println("for");
}
}
public class Main {
public static void main(String[] args)
{
Two g = new Two();
g.print_value();
g.print_for();
g.print_value();
}
}
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as
well as the derived class also acts as the base class for other classes.
E.g.
class One {
public void printOne() {
System.out.println("printOne() method of One class.");
}
}
class Two extends One {
public void printTwo() {
System.out.println("printTwo() method of Two class.");
}
}
public class Main extends Two {
public static void main(String args[]) {
obj.printOne(); obj.printTwo();
}
}
Class Interface
4. Timed Waiting State: Sometimes the longer duration of waiting for threads
causes starvation, if we take an example like there are two threads T1, T2 waiting
for CPU and T1 is undergoing a Critical Coding operation and if it does not exist
the CPU until its operation gets executed then T2 will be exposed to longer
waiting with undetermined certainty, In order to avoid this starvation situation, we
had Timed Waiting for the state to avoid that kind of scenario as in Timed
Waiting, each thread has a time period for which sleep() method is invoked and
after the time expires the Threads starts executing its task.
Java Packages
Packages in Java are a mechanism that encapsulates a group of classes, sub-
packages, and interfaces.
Packages are used for:
Prevent naming conflicts by allowing classes with the same name to exist
in different packages,
like college.staff.cse.Employee and college.staff.ee.Employee.
They make it easier to organize, locate, and use classes, interfaces, and
other components.
Packages also provide controlled access for Protected members that are
accessible within the same package and by subclasses.
Types of Java Packages
Built-in Packages
User-defined Packages
1. Built-in Packages
These packages consist of a large number of classes which are a part of
Java API.Some of the commonly used built-in packages are:
java.lang: Contains language support classes
java.io: Contains classes for supporting input / output operations.
java.util: Contains utility classes which implement data structures like
Linked List, Dictionary and support ; for Date / Time operations.
java.applet: Contains classes for creating Applets.
java.awt: Contain classes for implementing the components for
graphical user interfaces (like button , ;menus etc). 6)
java.net: Contain classes for supporting networking operations.
2. User-defined Packages
These are the packages that are defined by the user.
1. Create the Package:
2. Use the Class in Program: