JAVA EXTERNAL NOTES(UNIT-I,II)
[Link] b/w object and class (with definition example program) (explain
in detail)
A.
Demonstration:
class Person {
String name;
int age;
// Constructor
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
// Method
public void sayHello() {
[Link]("Hello, my name is "+ name +"and I am" + age +"years old.");
}
}
class Main {
public static void main(String[] args) {
// Create a Person object
Person obj=new Person("Ali",15);
// Invoke the sayHello() method
[Link]();
}
}
[Link] b/w string, string buffer and string builder (methods)
A.
String class Methods: length() , charAt() , substring() , concat() ,
equals() , compareTo() , replace() , toUpperCase() ,
toLowerCase() , trim() , valueOf() , etc.
CODE:
String str1 = "Hello";
String str2 = "World";
String str3 = [Link](str2);
StringBuffer class Methods: append() , insert() , delete() , reverse() ,
charAt() , substring() , length() , capacity() , setCharAt() ,
trimToSize() , etc.
CODE:
StringBuffer sb = new StringBuffer("Hello");
[Link](" World");
StringBuilder class Methods:same as StringBuffer class Methods
CODE:
StringBuilder sb = new StringBuilder("Hello");
[Link](" World");
[Link] line arguments
[Link] line arguments are nothing but simply arguments that are specified after
the name of the program in the system's command line, and these argument values are
passed on to your program during program execution.
In Java, command line arguments can be accessed through the main() method, which
takes an array of Strings as an argument. The array contains the command line
arguments passed to the program.
CODE:
public class Main {
public static void main(String[] args) {
[Link]("Number of arguments: " + [Link]);
for (int i = 0; i < [Link]; i++) {
[Link]("Argument " + i + ": " + args[i]);
}
}
}
[Link] features of Java in detail (Buzz words)
A.
1. Platform independence: Java is platform-independent, which means that a Java
program can run on any platform without the need for recompilation. This is
achieved through the use of the Java Virtual Machine (JVM), which provides a
layer of abstraction between the Java code and the underlying hardware and
operating system.
2. Object-oriented: Java is an object-oriented programming language, which means
that it supports the concepts of classes, objects, inheritance, encapsulation, and
polymorphism. This makes it easier to write complex software by breaking it down
into smaller, reusable components.
3. Garbage collection:Java has an automatic garbage collector that manages the
memory used by the program. This means that the programmer does not need to
explicitly allocate or deallocate memory, which can help to prevent memory leaks
and other memory-related errors.
4. Multithreading: Java supports multithreading, which allows multiple threads of
execution to run concurrently within a single program. This can improve
performance and responsiveness by allowing the program to perform multiple
tasks at the same time.
5. Exception handling: Java has a robust system for handling errors and exceptions.
This allows the programmer to write code that can gracefully handle unexpected
situations, such as runtime errors or user input errors.
6. Security: Java has built-in security features that help to prevent malicious code
from running on the system. This includes features such as the security manager,
which controls access to system resources, and the class loader, which verifies the
integrity of classes before they are loaded into the JVM.
7. Rich class library: Java has a large and comprehensive class library that provides
a wide range of pre-built functions and classes for performing common tasks. This
can save time and effort for the programmer by providing a set of reusable
components that can be used to build complex applications.
[Link] types of Inheritance
A.
[Link] inheritance
In this inheritance, a derived class is created from a single base [Link] the given
example, Class A is the parent class and Class B is the child class since Class B inherits
the features and behavior of the parent class A.
Syntax for Single Inheritance
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
[Link]-level inheritance
In this inheritance, a derived class is created from another derived class.
In the given example, class c inherits the properties and behavior of class B and class B
inherits the properties and behavior of class B. So, here A is the parent class of B and
class B is the parent class of C. So, here class C implicitly inherits the properties and
behavior of class A along with Class B i.e there is a multilevel of inheritance.
Syntax for Multi-level Inheritance
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : B
{
public void fooC()
{
//TO DO:
}
}
[Link] inheritance
In this inheritance, a derived class is created from more than one base class. This
inheritance is not supported like C#, F#, etc., and Java Language.
In the given example, class c inherits the properties and behavior of class B and class A
at the same level. So, here A and Class B both are the parent classes for Class C.
The syntax for Multiple Inheritance
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Base Class
class B
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : A, B
{
public void fooC()
{
//TO DO:
}
}
[Link] Inheritance
In this inheritance, more than one derived class is created from a single base class and
further child classes act as parent classes for more than one child class.
In the given example, class A has two children class B and class D. Further, class B and
class C both are having two children - class D and E; class F and G respectively.
The syntax for Hierarchical Inheritance
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : A
{
public void fooC()
{
//TO DO:
}
}
//Derived Class
class D : C
{
public void fooD()
{
//TO DO:
}
}
//Derived Class
class E : C
{
public void fooE()
{
//TO DO:
}
}
//Derived Class
class F : B
{
public void fooF()
{
//TO DO:
}
}
//Derived Class
class G :B
{
public void fooG()
{
//TO DO:
}
}
Advantages of Inheritance
1. Reduce code redundancy.
2. Provides better code reusabilities.
3. Reduces source code size and improves code readability.
4. The code is easy to manage and divided into parent and child classes.
Disadvantages of Inheritance
1. In Inheritance base class and child class, both are tightly coupled. Hence If you
change the code of the parent class, it will affect all the child classes.
2. In a class hierarchy, many data members remain unused and the memory
allocated to them is not utilized. Hence it affects the performance of your
program if you have not implemented inheritance correctly.
[Link] b/w Interface and classes and how does interface
Supports multiple inheritance
A.
Interfaces and classes are both types of reference types in Java, but they have some
differences in terms of their characteristics and usage.
Similarities:
Both interfaces and classes can be used to define types in Java.
They can have methods and fields, and methods can be abstract or concrete.
They can be extended or implemented by other classes or interfaces.
They can be used as reference types for object creation or method parameters.
Interfaces support multiple inheritance in Java because a class can implement multiple
interfaces. This means that a class can inherit behavior and functionality from multiple
sources, making it more flexible and reusable.
public class MyClass implements A, B
{
// implement methods for A and B
}
[Link] Garbage collections
[Link] collection is a process in Java that automatically frees up memory by
removing objects that are no longer being used by the application. Java uses a garbage
collector (GC) to perform this task, which runs as a part of the Java Virtual Machine
(JVM). The JVM maintains a heap area where all objects are allocated, and the garbage
collector runs periodically to remove any unused objects from the heap.
In addition to the above, the JVM also provides different options for configuring
garbage collection, such as setting the heap size, adjusting the garbage collector
settings, and monitoring the garbage collection process. Proper tuning of the garbage
collection process is essential for optimizing the performance of a Java application.
[Link] overloading, Overriding, Polymorphism (In Detail)
A.
Method Overloading:
Method overloading is a feature in Java that allows a class to have multiple methods
with the same name but different parameters. The compiler determines which method
to execute based on the number and type of arguments passed. Method overloading is
used to create methods that perform similar tasks but with different input parameters.
It is commonly used to provide flexibility in the design of APIs and to simplify code.
Method Overloading Example:
public class Calculator {
public int add(int x, int y) {
return x + y;
}
public double add(double x, double y) {
return x + y;
}
}
In this example, the Calculator class has two add methods that take different parameter
types (int and double). This allows the user to call the appropriate method based on the
type of data being added.
Method Overriding:
Method overriding is a feature in Java that allows a subclass to provide its own
implementation of a method that is already defined in its superclass. The method in the
subclass must have the same name, return type, and parameters as the method in the
superclass. Method overriding is used to implement runtime polymorphism.
Method Overriding Example:
public class Animal {
public void makeSound() {
[Link]("The animal makes a sound");
}
}
public class Cat extends Animal {
@Override
public void makeSound() {
[Link]("The cat meows");
}
}
Polymorphism:
Polymorphism is a feature in Java that allows objects of different classes to be treated
as if they were objects of the same class. Polymorphism can be achieved through
method overriding and method overloading. Polymorphism allows for flexibility and
reusability of code, and is a fundamental concept in OOP.
Polymorphism Example:
public class Animal {
public void makeSound() {
[Link]("The animal makes a sound");
}
}
public class Cat extends Animal {
@Override
public void makeSound() {
[Link]("The cat meows");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
[Link]("The dog barks");
}
}
public class AnimalTest {
public static void main(String[] args) {
Animal animal1 = new Cat();
Animal animal2 = new Dog();
[Link]();
[Link]();
}
}
[Link] vs Date hiding
A.
[Link] Overloading
[Link] overloading is a feature in Java that allows a class to have more than one
constructor with different parameters. When a class has multiple constructors, they are
differentiated based on the number, type, and order of their parameters. This is also
known as constructor overloading.
public class Person {
int age;
String name;
Person(){
[Link] = "";
[Link] = 0;
}
Person(String name){
[Link] = name;
[Link] = 0;
}
Person(String name,int age){
[Link] = name;
[Link] = age;
}
}
[Link] Modifiers
A.
Access modifiers in Java are keywords used to specify the level of access or visibility
that a class, method, or variable has in relation to other classes or objects. There are
four access modifiers in Java:
[Link]: Public access modifier allows unrestricted access to a class, method, or
variable. It can be accessed from any other class, anywhere, even outside the package.
public class MyClass {
public int x;
public void myMethod() {
// code here
}
}
[Link]: Private access modifier restricts the access of a class, method, or variable to
the same class only. It cannot be accessed from outside the class, even in the same
package.
public class MyClass {
private int x;
private void myMethod() {
// code here
}
}
[Link]: Protected access modifier allows access to a class, method, or variable
within the same package or by a subclass in a different package. It cannot be accessed
from outside the package by non-subclass
public class MyClass {
protected int x;
protected void myMethod() {
// code here
}
}
[Link]: Default access modifier, also known as package-private, restricts the access
of a class, method, or variable to the same package only. It cannot be accessed from
outside the package. If no access modifier is specified, the default access modifier is
applied.
class MyClass {
int x;
void myMethod() {
// code here
}
}
[Link]/protection mechanism visibility(in detail)
A. REFER Q.20
[Link]
[Link] Java, abstraction can be achieved through abstract classes and interfaces. An
abstract class is a class that cannot be instantiated and can contain both abstract and
non-abstract methods. An abstract method is a method without a body, which means it
only has a signature and no implementation. On the other hand, an interface is a
blueprint of a class that defines a set of methods without providing their
implementation.
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
[Link]("Zzz");
}
}
// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
[Link]("The pig says: wee wee");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
[Link]();
[Link]();
}
}
[Link] keyword, this Keyword, static,final,synchronized
A.
Super Keyword:
The super keyword in Java is a reference variable that is used to refer to the immediate
parent class object. It can be used to access any member of the superclass that is not
private or overridden.
Super Keyword Examples:
To call the superclass constructor from the subclass constructor:
public class SubClass extends SuperClass {
public SubClass() {
super(); // calls the SuperClass constructor
}
}
To call a superclass method:
public class SubClass extends SuperClass {
public void someMethod() {
[Link](); // calls the someMethod() of SuperClass
}
}
This is useful when you want to reuse the code of the superclass without having to
rewrite it in the subclass.
This Keyword:
The this keyword is used to refer to the current object instance in a method or
constructor. It can be used to refer to any member of the current object or to invoke
another constructor of the same class.
This Keyword Examples:
To access the instance variable of the current object:
public class MyClass {
private int myVar;
public void setMyVar(int myVar) {
[Link] = myVar; // sets the instance variable of the current object
}
}
To call another constructor of the same class:
public class MyClass {
private int myVar;
public MyClass() {
this(0); // calls the constructor with one int parameter
}
public MyClass(int myVar) {
[Link] = myVar; // sets the instance variable of the current object
}
}
This is useful when you want to avoid duplicating code in multiple constructors.
Static:
The static keyword in Java is used to declare a member of a class that belongs to the
class itself, rather than to any particular instance of the class. This means that it can be
accessed without creating an object of the class.
Static Examples:
To declare a static variable:
public class MyClass {
public static int myVar; // static variable belongs to the class
}
To declare a static method:
public class MyClass {
public static void myMethod() { // static method belongs to the class
// code here
}
}
This is useful when you want to share data or functionality across all instances of a class.
Final:
The final keyword in Java is used to declare a variable, method or class that cannot be
changed or extended.
Final Examples:
To declare a final variable:
public class MyClass {
public final int myVar = 10; // final variable cannot be changed
}
To declare a final method:
public class MyClass {
public final void myMethod() { // final method cannot be overridden
// code here
}
}
To declare a final class:
public final class MyClass { // final class cannot be extended
// code here
}
This is useful when you want to prevent modification or extension of a variable, method
or class.
Synchronized:
The synchronized keyword in Java is used to define a block of code that can be
accessed by only one thread at a time. This is useful when you want to avoid conflicts
between multiple threads that access the same shared data.
Examples:
To synchronize a block of code:
public class Counter {
private int count;
public synchronized void increment() {
count++;
}
public synchronized int getCount() {
return count;
}
}
To synchronize a static method:
public class SynchronizedExample {
private static int count = 0;
public static synchronized void incrementCount() {
count++;
}
public static int getCount() {
return count;
}
}
To synchronize a class:
public class SynchronizedExample {
private static int count = 0;
public static void incrementCount() {
synchronized([Link]) {
count++;
}
}
public static int getCount() {
return count;
}
}
[Link] passing mechanisms.
A.
In Java, there are two types of parameter passing mechanisms:
1. Pass by Value:
When a method is called, a copy of the value of each actual argument is passed to
the method. The changes made to the parameter inside the method have no
effect on the actual argument passed to the method. This is called pass by value.
For example of Pass by Value, consider the following code:
public class PassByValueExample {
public static void main(String[] args) {
int x = 10;
changeValue(x);
[Link]("Value of x: " + x);
}
public static void changeValue(int x) {
x = 20;
}
}
Output:
Value of x: 10
In this example, we pass an integer variable x with the value of 10 to the method
change Value. Inside the method, we change the value of x to 20. But when we print the
value of x outside the method, it remains 10. This is because the value of x passed to
the method is a copy of the original value, and any changes made inside the method
do not affect the original value.
2. Pass by Reference:
When a method is called, a copy of the reference to the object is passed to the
method. The changes made to the object inside the method are reflected in the
original object passed to the method. This is called pass by reference.
For example of Pass by Reference, consider the following code:
public class PassByReferenceExample {
public static void main(String[] args) {
StringBuilder str = new StringBuilder("Hello");
changeValue(str);
[Link]("Value of str: " + str);
}
public static void changeValue(StringBuilder str) {
[Link](" World!");
}
}
In this example, we pass a StringBuilder object to the method change Value. Inside the
method, we append " World!" to the object. When we print the value of str outside the
method, it reflects the changes made inside the method. This is because the reference
to the object is passed to the method, and any changes made to the object inside the
method are reflected in the original object passed to the method. This is called pass by
reference.
[Link] types and their size
A.
[Link] classes
[Link] classes also provide several utility methods for working with primitive data
types. For example, [Link]() can be used to convert a string to an integer.
Another example is [Link](), which can be used to check if a value is not a
number. (CAN REFER WRAPPER CLASS CODE FROM LAB PDF)
[Link] casting, Type Conversion
[Link] Java, there are two types of casting: implicit casting and explicit casting.
Implicit casting occurs automatically when a value of one data type is assigned to a
variable of another data type that can hold a larger range of values.
For example:
int i = 10;
double d = i; // implicit casting from int to double
Explicit casting, on the other hand, involves manually converting a value of one data
type to another data type using the cast operator. This is necessary when a value of a
larger data type needs to be assigned to a variable of a smaller data type that cannot
hold the larger value. For example:
double d = 10.5;
int i = (int) d; // explicit casting from double to int
Type conversion, on the other hand, is the process of converting a value from one
form to another, such as converting a string value to an integer or a boolean value to a
string. In Java, there are several built-in methods that can be used for type conversion,
such as [Link]() for converting a string to an integer, or
[Link]() for converting a boolean value to a string.
[Link] Program structure.
A.
[Link] of package and access (in detail )
A.
[Link] vs Abstraction (Detail)
A.
[Link] of Object Oriented Programming
[Link] four principles of object-oriented programming (abstraction, inheritance,
encapsulation, and polymorphism).These topics are already covered in the pdf.
Made with❤️by Telegram @oxrogerthat