BCA III YR - JAVA (Part 2)
BCA III YR - JAVA (Part 2)
JODHPUR
float nextFloat() It is used to scan the next token of the input as a float.
double nextDouble() It is used to scan the next token of the input as a double.
byte nextByte() It is used to scan the next token of the input as a byte.
It can be imported using java.util packages. It can be imported using java.io packages.
The scanner is a bit slower as it needs to parse data as BufferReader is faster than Scanner, as it only reads a
well. character stream.
The scanner has the method nextLine() to read a line. BufferReader has the method readLine() to read a line.
Through Interface
interface A { // members of A }
interface B { // members of B }
class C implements A, B {
// abstract members of A
// abstract members of B
Dr Shweta } MTMC Jodhpur
Purohit, 24
Abstract Class and Method
• Abstraction is an important concept of object-oriented programming
that allows us to hide unnecessary details and only show the needed
information.
• Abstract classes and methods is to achieve abstraction in Java.
• We cannot create objects of abstract classes.
• Abstract keyword is used to declare an abstract class.
• Syntax-
// create an abstract class
abstract class class_name {
// fields and methods
}
• An abstract class can have both the regular methods and abstract
methods.
• A method that doesn't have its body is known as an abstract method.
We use the same abstract keyword to create abstract methods.
• Example
abstract void method1(); Dr Shweta Purohit, MTMC Jodhpur 25
• If a class contains an abstract method, then the class
should be declared abstract. Otherwise, it will generate
an error.
• As abstract classes cannot be instantiated, we can create
subclasses from it.
• We can then access members of the abstract class using
the object of the subclass.
• An abstract class can have constructors like the regular
class.
• we can access the constructor of an abstract class from
the subclass using the super keyword.
• Abstract classes are used when:
– Want to share some common methods and fields among
multiple classes.
– Declaring non-static and non-final fields in order to modify
the state of the object to which they are bound.
Dr Shweta Purohit, MTMC Jodhpur 26
Abstract Class Interface
Inheritance & Only one abstract class can be inherited by a Multiple interfaces can be
Implementation class. implemented by a class.
It can have final, non-final, static, and non- It can have only static and final
Variable Types
static variables. variables.
• Example2-
class calculate{
int multiply(int a,int b){
return a*b;
}
int multiply(int a,int b,int c){
return a*b*c;
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Dr Shweta Purohit, MTMC Jodhpur 31
• The same method declared in the superclass and its subclasses can have different
access specifiers.
• We can only use those access specifiers in subclasses that provide larger access than
the access specifier of the superclass.
Example-
class Animal {
protected void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
public void displayInfo() {
System.out.println("I am a dog.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
In overloading, the methods have the In overriding, the methods have the same
same name but a different number of name and parameters must be the same.
parameters or a different type of
parameters.
Overloading occurs within the class. Overriding occurs within the two classes
that have an inheritance relationship.
method2()
{
// statements
// calling method1() from method2()
method1();
}
method3()
{
// statements
// calling of method2() from method3()
method2();
}
}
Dr. Shweta Purohit,MTMC Jodhpur 35
Final Keyword
• The final keyword in java is used to restrict the user
from further modifying the entity to which it is
applied.
• The java final keyword can be used in many context
like
– Variable: A final variable is a constant; once initialized, its
value cannot be changed.
final int VARVALUE = 100;
– Method: A final method cannot be overridden by
subclasses, ensuring that the method's implementation
remains unchanged.
class Parent {
public final void display()
{ System.out.println("This is a final method."); } }
Dr. Shweta Purohit,MTMC Jodhpur 36
• Class: If a class is declared as final, which means it cannot be extended by
any other class.
public final class FinalClass {
public void show()
{
System.out.println("This is a final class.");
}
}
• The Final keyword is a non-access modifier.
• The Final keyword can be primitive and non-primitive data
types.
• The final keyword can provide hints to the compiler to
perform optimizations. For example, it may inline constant
values or methods, improving performance.
• This is often used to prevent modification or extension of
certain classes, particularly utility classes.
Dr. Shweta Purohit,MTMC Jodhpur 37
Finalize Method
• The finalize() method in Java is a method of the Object class
used to perform cleanup activity before destroying any object.
• Garbage collector calls it before destroying the objects from
memory.
• Finalize method in Java is called by default for every object
before its deletion.
• This method helps the Garbage Collector close all the resources
the object uses and helps JVM in-memory optimization.
• All classes inherit the Object class directly or indirectly in Java.
The finalize() method is protected in the Object class so that all
classes in Java can override and use it.
protected void finalize() throws Throwable{}