BCS403 · Object Oriented Programming with Java · Unit 1
Introduction
Unit Scope & Exam Weightage: This unit covers why Java / history, JVM, JRE, JDK and the Java environment, source-file structure and compilation, fundamental programming structures (data types, variables, operators, control flow, arrays, strings), classes, objects, constructors, methods, access specifiers, static and final members, comments, and the four OOP pillars with inheritance, overriding, overloading, interfaces and abstract classes, plus packages, CLASSPATH, JAR files, imports and naming conventions. Expect 2-mark concept questions and 7-mark code-writing questions.
Importance-Wise Roadmap (BCS403 Unit 1)
| Priority | Exact Syllabus Topic | PYQ Evidence | Marks Pattern | Tag |
|---|---|---|---|---|
| 1 | OOP pillars, polymorphism, overriding, abstract classes | 2023-24, 2025-26 | 7m × 4 | Code + theory |
| 2 | JVM / JRE / JDK, bytecode, platform independence | 2023-24, 2024-25, 2025-26 | 7m + 2m × 2 | Predicted + PYQ |
| 3 | Constructors, classes and objects | 2023-24, 2024-25, 2025-26 | 7m + 2m × 2 | Predicted code |
| 4 | Data types, control structures, arrays & strings | 2024-25 | 7m × 2 | Program |
| 5 | Packages, naming conventions, imports | 2025-26 | 7m | Theory |
| 6 | Interfaces, static/final members, access specifiers | 2023-24 | 2m + integrated | Quick marks |
| 7 | Java history and Java environment | Not directly asked | Predicted 2m | Backup |
Java-specific examiner guidance: Every code answer must compile mentally: matching braces, a class name that matches the file name for public classes, and a correct main signature. Always print the expected output below the code — evaluators use it to check understanding of overriding and this/super.
1. Java Architecture and Environment
1.1 Why Java and a Brief History
Java was started by James Gosling at Sun Microsystems in 1991 as Oak for consumer electronics, renamed Java in 1995, and is now maintained by Oracle. Design goals: simple, object-oriented, platform-independent, robust (no pointers), secure, architecture-neutral, portable, interpreted-plus-compiled, multithreaded and dynamic.
PYQ (2024-25, 2 marks): "Why is Java platform-independent?" javac compiles source to bytecode, an intermediate instruction set understood by the JVM, not to native machine code. Each operating system has its own JVM that interprets/JITs the same bytecode, so "write once, run anywhere".
1.2 JDK, JRE and JVM
Predicted (bank
PRED, 2025-26, 2 marks): "State the specific functions of the JVM, JRE, and JDK. Why is the JVM the reason Java is called Platform Independent?"
| Component | Full form | Contains / does |
|---|---|---|
| JDK | Java Development Kit | JRE + compiler javac, jar, javadoc, debugger — needed to develop |
| JRE | Java Runtime Environment | JVM + core class libraries — needed to run |
| JVM | Java Virtual Machine | Loads .class files, verifies bytecode, executes via interpreter/JIT, manages heap and garbage collection |
Hello.java --javac--> Hello.class (bytecode) --JVM--> native machine instructions
(platform-independent) (per-platform JVM)
JVM reactions to a program: class loading → bytecode verification → execution (interpreted or JIT-compiled hot methods) → automatic garbage collection of unreachable objects.
Predicted (bank
PRED, 2024-25/2023-24, 7m/2m): "Explain the concept of JVM and JRE" / "Describe JVM and byte code in Java Architecture." Draw the pipeline above, define each box, and state that bytecode is a stack-based intermediate representation stored in.classfiles; the JVM's per-platform implementation is what makes the same.classfile run on Windows, Linux and macOS.
1.3 Source File Structure and Compilation
// file: Demo.java -- comment (single line)
/* multi-line comment */
/** Javadoc comment used by the javadoc tool */
import java.util.Scanner; // import a package type
public class Demo { // exactly one public class matching file name
public static void main(String[] args) {
System.out.println("Hello AKTU");
}
}
Compile: javac Demo.java → Demo.class. Run: java Demo. main must be public static void main(String[] args) so the JVM can call it before any object exists.
2. Fundamental Programming Structures
2.1 Data Types, Variables and Operators
PYQ (2024-25, 7 marks): "Discuss the different data types and control structures in Java with examples."
| Category | Types | Size | Example |
|---|---|---|---|
| Primitive – integer | byte, short, int, long |
1, 2, 4, 8 bytes | int age = 20; |
| Primitive – floating | float, double |
4, 8 bytes | double cgpa = 8.7; |
| Primitive – char | char |
2 bytes (Unicode) | char grade = 'A'; |
| Primitive – boolean | boolean |
1 bit logically | boolean passed = true; |
| Reference | class, interface, array, enum | reference to object | String name = "Aditi"; |
Operators: arithmetic (+ - * / %), unary (++ -- !), relational (< > <= >= == !=), logical (&& || !), bitwise (& | ^ << >>), assignment (= += -=), ternary (cond ? a : b), instanceof.
Control flow: if-else, switch (int/String/enum), for, while, do-while, break, continue, labelled break. Java is strongly typed — an int cannot be assigned to a byte without a cast.
int marks = 73;
if (marks >= 60) System.out.println("First Division");
else if (marks >= 45) System.out.println("Second Division");
else System.out.println("Fail");
2.2 Arrays and Strings
int[] a = {4, 7, 2, 9}; // array declaration + initialiser
for (int x : a) System.out.print(x + " "); // enhanced for loop
String s = "Hello";
System.out.println(s.length()); // 5
System.out.println(s.charAt(1)); // e
System.out.println(s.substring(1, 4)); // ell
PYQ (2024-25, 7 marks): "Write a Java program to take user input and display the reverse of a string."
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.nextLine();
String rev = "";
for (int i = s.length() - 1; i >= 0; i--) {
rev = rev + s.charAt(i); // append characters from the end
}
System.out.println("Reverse: " + rev);
sc.close();
}
}
Sample run: input Lecture → output Reverse: erutceL.
Key point: String is immutable, so concatenation builds a new String each iteration; for long strings one would use StringBuilder.reverse().
3. Classes, Objects and Constructors
Class: a user-defined blueprint/template describing fields and methods. Object: an instance of a class created with new, holding its own copy of instance fields and occupying heap memory.
PYQ (2023-24, 2 marks): "Define the concept of classes and object in Java with a suitable example."
class Student {...}is a blueprint;Student s1 = new Student();creates an object.s1is a reference variable;new Student()allocates the object on the heap and invokes the constructor.
Predicted (bank
PRED, 2023-24, 7 marks): "Illustrate Constructors and their applications in Java. Describe the types of constructors. Write a class Student with attributes roll_number, name, branch, email; write an all-argument constructor and create two objects."
public class Student {
int roll_number;
String name, branch, email;
// all-argument (parameterised) constructor
Student(int r, String n, String b, String e) {
roll_number = r;
name = n;
branch = b;
email = e;
}
void display() {
System.out.println(roll_number + " | " + name + " | " + branch + " | " + email);
}
public static void main(String[] args) {
Student s1 = new Student(101, "Aditi", "CSE", "aditi@aktu.ac.in");
Student s2 = new Student(102, "Rahul", "ECE", "rahul@aktu.ac.in");
s1.display();
s2.display();
}
}
Output:
101 | Aditi | CSE | aditi@aktu.ac.in
102 | Rahul | ECE | rahul@aktu.ac.in
| Constructor type | Meaning |
|---|---|
| Default (no-arg) | No parameters; supplies default values if the class declares none explicitly |
| Parameterised | Receives values to initialise fields (as above) |
| Copy | Creates an object from another object of the same class |
Rules: constructor name = class name, no return type (not even void), invoked automatically at new, may be overloaded, cannot be static/final/abstract.
Predicted (2 marks ×2): "What is the role of constructors in Java classes and when are they invoked automatically?" They initialise newly created objects; invoked by the
newoperator immediately after memory allocation.
3.1 Access Specifiers, static and final
| Specifier | Same class | Same package | Subclass (any package) | Everywhere |
|---|---|---|---|---|
private |
✔ | ✘ | ✘ | ✘ |
| default (no keyword) | ✔ | ✔ | ✘ | ✘ |
protected |
✔ | ✔ | ✔ | ✘ |
public |
✔ | ✔ | ✔ | ✔ |
staticmembers belong to the class, one copy shared by all objects; accessed asClassName.member.staticmethods cannot usethisand can only directly access static members.finalvariable = constant (cannot be reassigned);finalmethod cannot be overridden;finalclass cannot be extended.- Comments:
//line,/* */block,/** */Javadoc.
4. The Four Pillars of OOP
PYQ (2025-26, 7 marks): "Discuss the four pillars of Object-Oriented Programming — Encapsulation, Abstraction, Inheritance, and Polymorphism. Also explain how they help in building reusable code."
| Pillar | Meaning | Java mechanism | Reuse benefit |
|---|---|---|---|
| Encapsulation | Wrapping data and methods together, hiding internal representation | private fields + public getters/setters | Internal changes do not affect users of the class |
| Abstraction | Showing essential features, hiding implementation | abstract classes and interfaces | Client codes against a concept, not a concrete detail |
| Inheritance | Acquiring properties of a parent class | extends |
Common code written once in the superclass |
| Polymorphism | One name, many forms | method overloading + overriding | Same call works differently for different objects |
4.1 Inheritance, Overloading and Overriding
class Shape {
void displayArea() { System.out.println("Area depends on shape"); }
}
class Rectangle extends Shape {
int l = 4, b = 5;
@Override
void displayArea() { System.out.println("Rectangle area = " + (l * b)); }
}
public class TestPoly {
public static void main(String[] args) {
Shape s = new Rectangle(); // upcasting: superclass reference, subclass object
s.displayArea(); // dynamic dispatch picks Rectangle's version
}
}
Output: Rectangle area = 20.
PYQ (2025-26, 7 marks): "Explain the concept of Method Overriding. How does Java decide which method to call at runtime when a subclass object is assigned to a superclass reference?" Overriding = subclass provides its own implementation of an inherited method with the same signature. The reference type decides which methods are visible at compile time; the actual object type decides which implementation runs at runtime — dynamic method dispatch. In the code,
sis aShapereference so only Shape's declared methods can be called, but the JVM invokesRectangle.displayArea()because the object is a Rectangle. Rules: same signature, return type covariant, access cannot be reduced,private/static/finalmethods cannot be overridden.
PYQ (2023-24, 7 marks): "Illustrate polymorphism and its types in Java. Differentiate between run-time and compile-time polymorphism. Write super class Shape with method displayArea() and subclass Rectangle. Demonstrate method overriding."
| Compile-time (static) polymorphism | Runtime (dynamic) polymorphism |
|---|---|
| Method/constructor overloading | Method overriding |
| Same name, different parameter lists | Same signature, different implementations |
| Resolved by compiler from argument types | Resolved by JVM from object type |
Example: add(int,int) and add(double,double) |
Example: Shape s = new Rectangle(); s.displayArea(); |
| Also called early binding | Also called late binding / dynamic dispatch |
PYQ (2025-26, 2 marks): "Why is Java platform-independent?" (see §1.1) PYQ (2023-24, 2 marks): "Explain Interfaces in Java with suitable example." An interface is a contract of abstract methods (plus
default/staticmethods and constants); a classimplementsit and must provide bodies. Example:interface Drawable { void draw(); } class Circle implements Drawable { public void draw(){...} }— allows one reference type for many unrelated classes.
4.2 Abstraction and Abstract Classes
PYQ (2023-24, 7 marks): "Explain abstraction and abstract classes in Java. Describe abstract method. With a suitable example demonstrate the application of abstract classes."
abstract class Employee {
String name;
Employee(String n) { name = n; }
abstract double salary(); // no body: subclass must implement
void show() { System.out.println(name + " works"); } // concrete method
}
class Manager extends Employee {
Manager(String n) { super(n); }
double salary() { return 75000.0; }
}
public class AbstractDemo {
public static void main(String[] args) {
Employee e = new Manager("Neha"); // abstract reference, concrete object
e.show();
System.out.println("Salary = " + e.salary());
}
}
Output:
Neha works
Salary = 75000.0
Rules: an abstract class cannot be instantiated; it may contain abstract and concrete methods, fields and constructors; a subclass must implement all abstract methods or itself be abstract. Abstraction hides "how salary is calculated" while exposing the salary() contract.
Abstract class vs interface: abstract class supports state (fields) and extends (one only); interface supports multiple implementation and is a pure capability contract (with default methods since Java 8).
4.3 Encapsulation Example
class BankAccount {
private double balance; // hidden representation
public double getBalance() { return balance; }
public void deposit(double amt) {
if (amt > 0) balance += amt; // invariant enforced here
}
}
The field cannot be corrupted directly; validation lives in one place — the reusable class.
5. Packages
PYQ (2025-26, 7 marks): "Explain how packages are defined and used in Java. Discuss the naming conventions for avoiding package name conflicts."
- A package is a namespace grouping related classes/interfaces; declared with
package college.student;as the first statement in the source file. - Use:
import college.student.Student;or fully qualifiedcollege.student.Student s = new ...;.import college.student.*;imports all public types of that package (not subpackages). - Static import:
import static java.lang.Math.PI;allows writingPIinstead ofMath.PI. - CLASSPATH: environment variable /
-classpathoption telling the JVM where to find.classfiles and JARs, e.g.java -classpath D:\classes Test. - JAR:
jar cf library.jar college/student/*.classpackages compiled classes into one archive; add it to CLASSPATH to reuse the library. - Naming convention: reverse-domain lowercase names, e.g.
in.ac.aktu.cse.project.util— the unique domain prefix prevents collisions between organisations. Class names start uppercase (CamelCase), methods/fields lowerCamelCase, constants ALL_CAPS. - Access interaction: package-private members are visible only inside the same package;
protectedalso reaches subclasses.
package in.ac.aktu.cse.util;
public class Helper { public static int square(int x) { return x * x; } }
6. Exam-Ready Question Set and Practice
Question → answer index
| PYQ / predicted question | Section |
|---|---|
| Four pillars (2025-26, 7m) | §4 table + §4.3 |
| Overriding + dynamic dispatch (2025-26, 7m) | §4.1 |
| Polymorphism types + Shape/Rectangle (2023-24, 7m) | §4.1 |
| Packages (2025-26, 7m) | §5 |
| Data types/control structures (2024-25, 7m) | §2.1 |
| Reverse string (2024-25, 7m) | §2.2 |
| Abstraction/abstract class (2023-24, 7m) | §4.2 |
| Platform independence (2024-25/25-26, 2m) | §1.1 |
| Class and object (2023-24, 2m) | §3 |
| Interface (2023-24, 2m) | §4.1 |
| JVM/JRE/JDK and constructors (predicted) | §1.2, §3 |
Practice questions (label: practice, not real PYQs)
- Practice: Write a
Bookclass with a default and a parameterised constructor; create two objects and print them. - Practice: Overload
area(int side),area(int l, int b)and show which method is chosen. - Practice: Declare a
Drawableinterface and implement it in two unrelated classes; demonstrate an array of interface references. - Practice: Create a package
in.ac.aktu.demo, compile it, and run a class from a different directory using-classpath. - Practice: Explain why
staticmethods cannot be overridden but can be hidden.
7. Last-Night Survival Checklist (Unit 1 only)
- Pipeline:
.java → javac → .class bytecode → JVM per OS; JDK ⊃ JRE ⊃ JVM. - Four pillars with one Java keyword each: private/getters, abstract/interface, extends, overload/override.
- Two code templates: Student constructor with two objects; Shape/Rectangle overriding with output.
- Specifier table (private/default/protected/public) and the
static/finalrules. - Package rule:
packagefirst line, reverse-domain naming, CLASSPATH finds classes, JAR bundles them.
Final Java reminder: A 7-mark program question is graded on compilability, correct constructor/super/@Override usage, and the printed output. Write the output explicitly and keep class names identical to file names.