0% found this document useful (0 votes)
4 views28 pages

Join Aspire2Learn's Java Community

2 java

Uploaded by

gunjan Sathawane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

Join Aspire2Learn's Java Community

2 java

Uploaded by

gunjan Sathawane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Join Our Telegram Community - Aspire2Learn

“We are here to provide you with simplified


notes for BCA and to help you throughout
your BCA journey...
Team, Aspire2Learn”

Join Our Telegram Community - Aspire2Learn


PROGRAMMING IN JAVA
SYLLABUS
UNIT - I :
Introduction to Java: -History of Java, features of Java, getting started with Java.
Java programs:-Introduction of Application & Applets. Variables: -Variable
naming,
variable initialization, assign values, Rules of variables, Scope of variable.
Operators:
Arithmetic, Assignment, Unary, Comparison, Shift, Bit- Wise, Logical, Conditional,
New, Special, Relational. Data types:-Integers, Char, String, Float etc. Typecasting:
Tokens: -Java tokens Order of precedence of operators Streams: - Input and
output.

UNIT - II :
Creating a class & subclass: -Declaring a class, Naming class, Rules to assign Class
&
Subclass, Creating a new object, Class of an object. Data members: -Declaring
data
member, Naming variables, using class members. Methods: -Using data members,
Invoke a method, passing arguments to a method, calling method. Access
Specifier &
Modifiers: -Public, Private, Protected, Static & Final. Overloading: -Method
overloading, Constructor overloading. Java class library: - Different types of
classes.
Decision making & loops:-If-then-else, Switch,? : operator, While-loop, do-while
loop,
for. Array: -Creating an array, one-dimensional array, two-dimensional array.
String:
String array, string methods. Inheritance: -Single & multiple inheritances
Interfaces:
Defining interfaces, extending interfaces, implementing interfaces.

UNIT - III :
Join Our Telegram Community - Aspire2Learn
Packages: -Java API packages, creating packages, accessing packages, adding a
class to
packages. Import statement: - Introduction & implementation of import
statement.
Applets:-Introduction to Applets & Application, how applets application are
different
creating An applet. Applets life cycle, designing a web page, creating an
executable
applet, running the applet, applet tags, passing a parameter to applet, HTML tag,
Converting applet to application. Threads:-Overview of threads, single & multiple
threads, lift cycle of threads, stopping & blocking threads, working with threads,
priority
to thread, synchronization. Exceptions & Errors:-Introduction, types of error,
exception,
syntax of exception, handling techniques, exception for Debugging.

UNIT - IV :
Event: -Event driven programming, handling an (AWT) events. Graphic class:
Introduction, the graphic classes, drawing & filling of lines, rectangle, circle &
ellipse,
arcs, polygons, text & fonts, creating a font class, font objects, text, coloring
object.
Streams:-Introduction, Abstract stream classes, file input & output.
AWI Applications: -Creating a GUI using AWT toolkit, using component class,
frames.
Components & Control: -Textfield, textarea class, label, button, choice, list,
checkbox,
class, and combo. Menus: -Creating a popup menus. Image: - Type of image,
Properties
of an image, Displaying an image. Layouts: -Using Window Listener interface,
Different
types of Layout, Layout manager, Flow manager, Grid manager. Container: -
Different
types of container (Frame, Dialog, Panel).

Join Our Telegram Community - Aspire2Learn


UNIT TWO –
------------------------------------------------------------------------------
1. Explain access specifier and modifier in java.
S[2023],W[2019].
OR
2. What are the access specifiers ? Explain with proper
syntax. S[2019].
OR
3. Explain access specifiers and their use by giving proper
example. S[2017].
Ans –
Access Specifiers and Modifiers in Java -
 Java provides access specifiers and modifiers to control
the accessibility and behavior of classes, methods, and
variables.
Access Specifier Scope (Where it Usage
is Accessible)

public Accessible from Used for widely


anywhere (inside accessible
or outside the methods and
package). classes.

private Accessible only Used for data


within the same hiding, ensuring
class. encapsulatio
Join Our Telegram Community - Aspire2Learn
protected Accessible within Used for
the same package inheritance.
and in subclasses
(even in different
packages).

default (no Accessible only Used for package-


keyword) within the same level
package. access.

1. Access Specifiers in Java -


 Access specifiers define the visibility of a class, method,
or variable in Java.

Example of Access Modifiers:

Join Our Telegram Community - Aspire2Learn


Access Specifiers Code Explanation:

How it Works?
 publicVar → Can be accessed from anywhere.

 privateVar → Only accessible inside Example class, so


can't be accessed in main().

 protectedVar → Accessible inside Example and


subclasses (even in different packages).

 defaultVar → Only accessible within the same package.


Join Our Telegram Community - Aspire2Learn
Output (without error line):

2. Access Modifiers in Java -


 Modifiers are keywords that modify the behavior of
variables, methods, and classes.

Modifier Purpose

static Belongs to the class, not to


an object.
final Prevents modification
(cannot be overridden or
inherited).

abstract Used for abstract classes


and methods.
Join Our Telegram Community - Aspire2Learn
synchronized Used in multithreading to
prevent data
inconsistency.
Volatile Ensures the variable is
always read from memory,
not cache.

transient Used to skip serialization


of a variable.

Example Of Access Modifier -

Join Our Telegram Community - Aspire2Learn


Access Modifiers Code Explanation:

How it Works?
 staticVar → Shared across all objects, belongs to class,
not instances.

 finalVar → Once assigned, its value cannot be changed.

Join Our Telegram Community - Aspire2Learn


 staticMethod() → Can be called without creating an
object.

Output:

------------------------------------------------------------------------------
4. What is method overriding ? Explain with an example.
S[2024],W[2018], S[2017],W[2017].
Ans –
Method Overriding in Java -
 Method Overriding is a feature in Java that allows a
subclass to provide a specific implementation of a
method that is already defined in its superclass.

Key Rules for Method Overriding:

 The method in the child class must have the same name,
return type, and parameters as in the parent class.

 The method in the child class should have the same or a


more accessible access modifier (Cannot reduce
visibility).

Join Our Telegram Community - Aspire2Learn


 Static methods and constructors cannot be overridden.

 Final methods cannot be overridden.

 The @Override annotation is used for better readability


and error detection.

Explanation of Code (Method Overriding in Java)

1. Parent Class (Animal) -

 The Animal class has a method sound() that prints


"Animals make a sound".

 This method will be overridden by the subclass.

2. Child Class (Dog) - Overriding the sound() Method -

Join Our Telegram Community - Aspire2Learn


 The Dog class inherits from the Animal class using
extends.

 It overrides the sound() method and provides a new


implementation.

 The @Override annotation ensures the method is


correctly overridden.

3. Main Class (Main) - Calling the Methods -

Join Our Telegram Community - Aspire2Learn


 Step 1: An Animal object myAnimal is created, and it
calls sound(), printing "Animals make a sound".

 Step 2: A Dog object myDog is created using upcasting


(Animal myDog = new Dog();).

 Since sound() is overridden in the Dog class, it prints


"Dog barks" instead of the parent class output.

Expected Output:

 First Output: "Animals make a sound" → Comes from


the Animal class.

Join Our Telegram Community - Aspire2Learn


 Second Output: "Dog barks" → Comes from the
overridden sound() method in the Dog class.

Real-World Use Cases of Method Overriding –

1. ATM Transactions (Banking System)

 A parent class Bank has a method withdraw(), but


different banks may have different withdrawal limits.

 The withdraw() method can be overridden in SBI, HDFC,


ICICI classes.

2. Online Shopping (E-commerce) -

 A class Payment has a method processPayment(), which


is overridden in child classes like CreditCardPayment,
UPIPayment, and PayPalPayment.

3. Vehicle Sounds (Polymorphism) -

 A base class Vehicle has a method hornSound(), which is


overridden by Car, Bike, and Truck classes to provide
different horn sounds.

4. Game Development -
Join Our Telegram Community - Aspire2Learn
 A Character class has an attack() method, which is
overridden in Warrior, Mage, and Archer classes to
provide unique attack styles.

5. Difference between method overloading and method


overriding. S[2023],W[2019].
Ans –
Feature Method Method
Overloading Overriding

Definition Multiple methods A subclass


with the same provides a new
name but different implementation
parameter lists in for a method
the same class. inherited from a
superclass with
the same
signature.
Class Happens within Happens between
Involvement the same class. parent and child
classes
(inheritance
required).

Parameters Must be different Must be same


(different number (same name,
or types). parameters, and
return
Join Our Telegram Community - Aspire2Learn
type).

Return Type Can have different Must have the


return types. same return type
(or covariant
return
type).

Access Specifier Can have any Cannot have


access specifier. weaker access
than the parent
class
method.

Static Methods Can be overloaded. Cannot be


overridden
(Method Hiding
occurs).
Final Methods Can be overloaded. Cannot be
overridden.
Polymorphism Achieves Compile- Achieves Runtime
Type time polymorphism.
polymorphism.

Checked Can throw Cannot throw new


Exceptions different or broader
exceptions. exceptions than
the overridden
method.

Join Our Telegram Community - Aspire2Learn


Usage Used for method Used to modify
flexibility (e.g., existing behavior
different ways to in child
perform an classes
operation).

------------------------------------------------------------------------------
7. What is an interface ? How it is defined and used.
S[2024,2018].
Ans –
Interface in Java
 An interface in Java is a blueprint of a class that contains
only abstract methods and constants (until Java 8, which
introduced default and static methods).

 It is used for achieving 100% abstraction and multiple


inheritance in Java.

Defining an Interface -
 An interface is defined using the interface keyword.

Syntax:

Join Our Telegram Community - Aspire2Learn


Using an Interface in Java -
 A class implements an interface using the implements
keyword and provides implementations for its methods.

Example:

Join Our Telegram Community - Aspire2Learn


Key Points:
 Cannot Instantiate → Interfaces cannot be instantiated
directly.

 Abstract Methods → All methods are abstract (before


Java 8).

 Multiple Inheritance → A class can implement multiple


interfaces.

 Default & Static Methods (Java 8+) → Interfaces can have


default and static methods.
Join Our Telegram Community - Aspire2Learn
 No Constructors → Interfaces cannot have constructors.

 Use Case: Interfaces are widely used in Java for


designing loosely coupled, flexible applications, such as
in Spring Framework, JDBC, and Collections API.
------------------------------------------------------------------------------
PROGRAM SECTION –
------------------------------------------------------------------------------
1. Write a java program to demonstrate single inheritance.
W[2019].
OR
2. Explain with example single inheritance. W[2017].
Ans –
Java Program to Demonstrate Single Inheritance

What is Single Inheritance?


 Single inheritance in Java allows a child (subclass) to
inherit properties and methods from a parent
(superclass). This enables code reusability and enhances
maintainability.

Example Code:

Join Our Telegram Community - Aspire2Learn


Explanation of Code:
 Define a Parent Class (Animal) → Contains a method
eat().

 Create a Child Class (Dog) → Inherits Animal using


extends keyword.

Join Our Telegram Community - Aspire2Learn


 Child Class adds its own method (bark()).

 Inside main(), create an object of Dog and call both


methods.

Expected Output:

3. Write a program in java to accept temperature in celsius


through keyboard and covert it into Fahrenheit.
S[2024,2018].
Ans -
Java Program to Convert Celsius to Fahrenheit -

Formula for Conversion:


F = (C * 9/5) + 32
where:
 F = Temperature in Fahrenheit

 C = Temperature in Celsius

Java Code:
Join Our Telegram Community - Aspire2Learn
Explanation of Code:
 Import Scanner → Used to take user input.

 Create Scanner object → Scanner scanner = new


Scanner([Link]);

 Ask the user to enter temperature in Celsius.

 Read input and store in celsius variable.

Join Our Telegram Community - Aspire2Learn


 Convert Celsius to Fahrenheit using formula → (celsius *
9/5) + 32

 Print the converted Fahrenheit value.

 Close Scanner → To free resources.

Example Input & Output:

Input:

Output:

------------------------------------------------------------------------------
Test Your Knowledge Through
Aspire2Learn Online Quiz –

------------------- Aspire2Learn Online Quiz-------------------

------------------------------------------------------------------------------

Join Our Telegram Community - Aspire2Learn


Aspire2Learn Feedback Form –
- Your Voice, Our Priority

----------------------- Feedback Link -----------------------------


------------------------------------------------------------------------------

Join Our Telegram Community - Aspire2Learn


Join Our Telegram Community - Aspire2Learn
Join Our Telegram Community - Aspire2Learn

You might also like