0% found this document useful (0 votes)
1 views13 pages

Unit 1 Java

The document outlines the foundations of Object-Oriented Programming (OOP) and Java, covering key principles such as encapsulation, abstraction, inheritance, and polymorphism. It details the Java Development Kit (JDK) components, data types, variables, operators, control statements, classes and objects, access specifiers, static members, constructors, method overriding, and inheritance. Additionally, it includes teaching tips and a quick recap table for revision purposes.
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)
1 views13 pages

Unit 1 Java

The document outlines the foundations of Object-Oriented Programming (OOP) and Java, covering key principles such as encapsulation, abstraction, inheritance, and polymorphism. It details the Java Development Kit (JDK) components, data types, variables, operators, control statements, classes and objects, access specifiers, static members, constructors, method overriding, and inheritance. Additionally, it includes teaching tips and a quick recap table for revision purposes.
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

Foundations of OOP and Java —

Teaching Notes

1. Principles of OOP (Object-Oriented


Programming)

OOP is a programming style based on objects (real-world


entities) rather than just functions and logic. Four pillars:

Principle Meaning Simple Example

Wrapping
data
(variables)
and code
A BankAccount class
(methods)
hides balance, only
Encapsulation into a
exposes
single unit
deposit() / withdraw()
(class),
hiding
internal
details
Principle Meaning Simple Example

Showing
only
essential Driving a car without
Abstraction
features, knowing engine internals
hiding
complexity

One class
acquiring Dog and Cat inherit from
Inheritance
properties Animal
of another

Same
action
behaves sound() method — dog
Polymorphism
differently barks, cat meows
based on
object

Teaching tip: Ask students to name 2 real-world objects and


their properties/behaviors before introducing classes — it
makes the class/object mapping click faster.

2. Java Development Kit (JDK)

JDK is the complete toolkit to develop and run Java programs.

Components:

JVM (Java Virtual Machine) — executes bytecode, gives


platform independence
JRE (Java Runtime Environment) — JVM + libraries needed
to run Java programs

Compiler (javac) — converts .java source code to


.class bytecode

Other tools — debugger, javadoc, etc.

Relationship: JDK ⊃ JRE ⊃ JVM

Flow of execution:

.java file → javac (compiler) → .class file


(bytecode) → JVM → machine code

This is why Java is "Write Once, Run Anywhere" — bytecode


runs on any OS that has a JVM.

3. Data Types, Variables and Arrays

Data Types

Primitive (8 types):

Type Size Example

byte 1 byte byte b = 10;

short 2 bytes short s = 100;

int 4 bytes int age = 25;

long 8 bytes long pop = 7000000000L;


Type Size Example

float 4 bytes float f = 5.5f;

double 8 bytes double d = 9.99;

char 2 bytes char grade = 'A';

boolean 1 bit boolean pass = true;

Non-primitive: String, Array, Class, Interface

Variables

Named memory locations to store values.

int marks = 90; // local variable


static int total; // static variable
int score; // instance variable (inside
class, outside method)

Rules for naming: cannot start with a digit, no spaces, case-


sensitive, no reserved keywords.

Arrays

A collection of similar data types stored in contiguous memory.

int[] marks = {90, 85, 78, 92};


[Link](marks[0]); // 90

// 2D array
int[][] matrix = {{1,2},{3,4}};

Key point: Array index starts from 0, size is fixed once declared.
4. Operators

Category Operators Example

Arithmetic + - * / % a + b, a % b

== != > < >=


Relational a == b
<=

Logical && || ! a > 5 && b < 10

Assignment = += -= *= /= a += 5;

Unary ++ -- ! a++;

Bitwise & | ^ ~ << >> a & b

condition ? a int max = (a>b) ?


Ternary
: b a : b;

Teaching tip: ++a (pre-increment) vs a++ (post-increment) is


a common confusion — always demo with a print statement
side by side.

5. Control Statements

Decision Making

if (marks >= 50) {


[Link]("Pass");
} else {
[Link]("Fail");
}

switch (day) {
case 1: [Link]("Monday"); break;
default: [Link]("Invalid");
}

Looping

for (int i = 0; i < 5; i++) { } // fixed


iterations
while (condition) { } // condition-
checked first
do { } while (condition); // executes
at least once

Jump Statements

break — exits loop/switch

continue — skips current iteration

return — exits method

6. Classes & Objects

Class = blueprint/template. Object = real instance created from


the class.

class Student {
String name;
int age;

void display() {
[Link](name + " - " + age);
}
}

public class Main {


public static void main(String[] args) {
Student s1 = new Student(); // object
creation
[Link] = "Bakeyalakshmi";
[Link] = 25;
[Link]();
}
}

Key point: new keyword allocates memory and creates the


object. Multiple objects can be created from one class, each
with its own data.

7. Access Specifiers

Control visibility of class members.

Subclass
Same Same Other
Specifier (diff.
Class Package Package
package)

private ✅ ❌ ❌ ❌
default
(none)
✅ ✅ ❌ ❌
protected ✅ ✅ ✅ ❌
public ✅ ✅ ✅ ✅
public class Employee {
private double salary; // hidden from outside
public String name; // accessible
everywhere
}

Teaching tip: Tie this directly back to Encapsulation — private


variables + public getter/setter methods is the standard
pattern.

8. Static Members

static means the member belongs to the class, not to


individual objects — shared across all instances.

class Counter {
static int count = 0; // shared by all
objects

Counter() {
count++;
}
}

Static variable → one copy shared by all objects

Static method → can be called without creating an object


( [Link]() ), cannot access non-static
(instance) members directly

main() is always static — JVM calls it without creating an


object first
9. Constructors

Special method that initializes an object; same name as class,


no return type, called automatically when new is used.

class Student {
String name;

// Default constructor
Student() {
name = "Unknown";
}

// Parameterized constructor
Student(String n) {
name = n;
}
}

Student s1 = new Student(); // uses


default
Student s2 = new Student("Surya"); // uses
parameterized

Constructor Overloading: multiple constructors with different


parameter lists in the same class.

Key point: If no constructor is written, Java provides a default


one automatically — but the moment you write any constructor,
the automatic default one disappears.

10. Method Overriding

When a subclass provides its own implementation of a method


already defined in its parent class — same name, same
parameters, achieves runtime polymorphism.

class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
void sound() {
[Link]("Dog barks");
}
}

Animal a = new Dog();


[Link](); // Output: Dog barks

Rules:

Method signature must match exactly

Cannot override static , final , or private methods

Access specifier in child cannot be more restrictive than


parent

Overriding vs Overloading (common exam confusion):

Overriding Overloading

Same class hierarchy (parent-


Same class
child)

Same signature Different parameters

Compile-time
Runtime polymorphism
polymorphism
11. Inheritance

Mechanism where one class (child/subclass) acquires


properties and behaviors of another (parent/superclass) using
extends .

class Animal {
void eat() {
[Link]("This animal eats
food");
}
}

class Dog extends Animal {


void bark() {
[Link]("Dog barks");
}
}

Dog d = new Dog();


[Link](); // inherited from Animal
[Link](); // Dog's own method

Types of Inheritance in Java:

Type Supported in Java?

Single ✅
Multilevel ✅
Hierarchical ✅
Type Supported in Java?

Multiple (via classes)


❌ (avoided — "diamond
problem")

Multiple (via
interfaces)

super keyword — used to call parent constructor or parent
method from child class:

class Dog extends Animal {


Dog() {
super(); // calls Animal's constructor
}
}

Quick Recap Table (for revision/viva)

Topic One-line takeaway

Encapsulation, Abstraction, Inheritance,


OOP Principles
Polymorphism

Full toolkit; contains JRE + JVM +


JDK
compiler

8 primitives + non-primitives (String,


Data Types
Array, etc.)

Arithmetic, relational, logical,


Operators
assignment, ternary
Topic One-line takeaway

Control
if-else, switch, loops, break/continue
Statements

Class & Object Class = blueprint, Object = instance

Access private → default → protected → public


Specifiers (increasing visibility)

Static Members Belongs to class, shared across objects

Same name as class, no return type,


Constructors
auto-called on new

Method Child redefines parent method — runtime


Overriding polymorphism

Child class reuses parent's members via


Inheritance
extends

You might also like