java2
java2
Programming
Fundamentals
Core Concepts
and Best
Practices
Object-Oriented Programming,
Classes, Methods, Packages,
and Imports
Table of Contents
1. Object-Oriented Programming
(OOP)
Classes and Objects
Encapsulation
Inheritance
Polymorphism
Abstraction
2. Class Structure
3. Main Method
4. Packages
5. Imports
6. Practice Exercises
Object-Oriented
Programming
(OOP)
What is OOP?
Object-Oriented Programming is a
programming paradigm based on the
concept of "objects":
Inheritance
Polymorphism
Abstraction
Classes and
Objects
Class Object
Blueprint or Instance of a
template class
// Class definition
public class Car {
// Attributes
private String make;
private String model;
private int year;
// Constructor
public Car(String make, Stri
this.make = make;
this.model = model;
this.year = year;
}
// Method
public void drive() {
System.out.println("Driv
}
}
Creating and
Using Objects
Key Points:
Objects are created using the new
keyword
Encapsulation
Encapsulation is the bundling of data
with the methods that operate on that
data, while restricting direct access.
Benefits:
Data hiding (private attributes)
Inheritance
Inheritance allows a class (subclass) to
inherit attributes and methods from
another class (superclass).
Benefits: Syntax:
Code
public class Su
reusability
// Addition
Establishes "is- }
a" relationship
Hierarchical
classification
// Superclass
public class Animal {
protected String name;
// Subclass
public class Dog extends Animal
private String breed;
Polymorphism
Polymorphism allows objects to be
treated as instances of their parent
class rather than their actual class.
Types of Polymorphism:
Method Overriding: Subclass
provides a specific implementation
of a method already defined in the
superclass
// Base class
public class Shape {
public double calculateArea(
return 0;
}
}
// Derived classes
public class Circle extends Shap
private double radius;
@Override
public double calculateArea(
return Math.PI * radius
}
}
// Using polymorphism
Shape myShape = new Circle(5.0);
double area = myShape.calculateA
Abstraction
Abstraction is the concept of hiding
complex implementation details and
showing only the necessary features.
// Abstract class
public abstract class Vehicle {
protected String brand;
// Concrete subclass
public class Car extends Vehicle
public Car(String brand) {
this.brand = brand;
}
@Override
public void move() {
System.out.println(brand
}
}
Class Structure
A typical Java class structure consists
of:
// 3. Class declaration
public class Customer {
// 4. Fields/Attributes
private String id;
private String name;
// 5. Constructors
public Customer(String id, S
this.id = id;
this.name = name;
}
// 6. Methods
public String getId() {
return id;
}
10
Main Method
The main method is the entry point for
a Java application.
Method Signature
Components:
public : Accessible from
anywhere
Example:
// Processing command-li
for (int i = 0; i < args
System.out.println("
}
}
}
11
Packages
Packages in Java are used to group
related classes, interfaces, and other
types.
Benefits:
Organization of related classes
Access control
Package Declaration:
package com.example.utility;
Package Naming
Conventions:
Use lowercase letters
Example Directory
Structure:
src/
├── com/
│ └── example/
│ ├── model/
│ │ └── Customer.java
│ └── service/
│ └── CustomerService
12
Imports
Import statements allow you to use
classes from other packages without
using the fully qualified name.
Types of Imports:
2. Wildcard
Import
import java.uti
Import Rules:
java.lang.* is automatically
imported
13
Practice Exercise
1: Basic Class and
Object
Create a Person class with attributes
name , age , and email .
// Constructor
public Person(String name, i
this.name = name;
this.age = age;
this.email = email;
}
@Override
public String toString() {
return "Person [name=" +
}
}
14
Practice Exercise
2: Inheritance
Create a Shape hierarchy with a base
class and derived classes.
// Base class
public abstract class Shape {
public abstract double calcu
public abstract double calcu
}
@Override
public double calculateArea(
return Math.PI * radius
}
@Override
public double calculatePerim
return 2 * Math.PI * rad
}
}
15
Practice Exercise
3: Encapsulation
Design a BankAccount class with
private attributes and methods for
deposit, withdrawal, etc.
public BankAccount(String ac
this.accountNumber = acc
this.ownerName = ownerNa
this.balance = initialBa
}
16
Practice Exercise
4: Packages and
Imports
Create a package structure for a simple
library management system:
src/
├── com/
│ └── library/
│ ├── model/
│ │ ├── Book.java
│ │ └── Author.java
│ ├── service/
│ │ └── BookService.jav
│ └── app/
│ └── LibraryApp.java
Example
Implementation:
// File: src/com/library/model/B
package com.library.model;
// File: src/com/library/app/Lib
package com.library.app;
import com.library.model.Book;
import com.library.model.Author;
import com.library.service.BookS
17
Practice Exercise
5: Static Methods
and Variables
Create a MathUtils class with static
methods and a counter to track method
calls.
if (numbers == null || n
throw new IllegalArg
}
18
Practice Exercise
6: Abstract
Classes and
Interfaces
Abstract Interface:
Class:
public interfac
double calc
public abstract
void displa
protected S
}
protected S
protected d
// Construc
// implemen
@Override
public doub
return
}
}
19
Practice Exercise
7: Complete
Application
Create a student management system
demonstrating all concepts covered:
Package Structure:
com.university.model
com.university.service
com.university.app
Key Features to
Implement:
Add/remove students and courses
Calculate GPAs
Summary
Next Steps:
Complete the practice exercises
Exception Handling
Multithreading
File I/O
21
Thank You!
Happy Java
Programming
Questions?