0% found this document useful (0 votes)
2 views1 page

java2

The document provides an overview of Java programming fundamentals, focusing on Object-Oriented Programming (OOP) principles such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It outlines the structure of Java classes, the main method, packages, and imports, along with practice exercises to reinforce these concepts. Additionally, it encourages further exploration of advanced Java topics and practical application development.
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)
2 views1 page

java2

The document provides an overview of Java programming fundamentals, focusing on Object-Oriented Programming (OOP) principles such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It outlines the structure of Java classes, the main method, packages, and imports, along with practice exercises to reinforce these concepts. Additionally, it encourages further exploration of advanced Java topics and practical application development.
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
You are on page 1/ 1

Java

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":

Objects contain data (attributes) and


code (methods)

Programs are designed by creating


objects that interact with each other

Promotes code reusability and


modularity

Key OOP Principles:


Encapsulation

Inheritance

Polymorphism

Abstraction

Classes and
Objects

Class Object
Blueprint or Instance of a
template class

Defines Has state and


attributes and behavior
behaviors
Occupies
Does not memory space
occupy
memory space

// 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

public class CarDemo {


public static void main(Stri
// Creating objects (ins
Car myCar = new Car("Toy
Car friendsCar = new Car

// Using object methods


myCar.drive(); // Outpu
friendsCar.drive(); //
}
}

Key Points:
Objects are created using the new
keyword

Each object has its own copy of


instance variables

Multiple objects can be created from


a single class

Each object maintains its own state

Encapsulation
Encapsulation is the bundling of data
with the methods that operate on that
data, while restricting direct access.

Benefits:
Data hiding (private attributes)

Controlled access through methods

Prevents unintended interference

Allows for data validation

public class BankAccount {


// Private attributes - hidd
private String accountNumber
private double balance;

// Public methods - controll


public double getBalance() {
return balance;
}

public void deposit(double a


if (amount > 0) {
balance += amount;
} else {
System.out.println("
}
}
}

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;

public void eat() {


System.out.println(name
}
}

// Subclass
public class Dog extends Animal
private String breed;

public Dog(String name, Stri


this.name = name; // In
this.breed = breed;
}

public void bark() {


System.out.println("Woof
}
}

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

Method Overloading: Multiple


methods with the same name but
different parameters

// Base class
public class Shape {
public double calculateArea(
return 0;
}
}

// Derived classes
public class Circle extends Shap
private double radius;

public Circle(double radius)


this.radius = 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 Classes and


Methods:
Abstract class: Cannot be
instantiated, may contain abstract
methods

Abstract method: Declared without


implementation (subclasses must
implement)

// Abstract class
public abstract class Vehicle {
protected String brand;

// Abstract method - no impl


public abstract void move();

// Regular method with imple


public void displayBrand() {
System.out.println("Bran
}
}

// 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:

// 1. Package declaration (optio


package com.example.myapp;

// 2. Import statements (optiona


import java.util.ArrayList;
import java.util.Date;

// 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;
}

public void setName(String n


this.name = name;
}

// 7. Inner classes (optiona


public class Address {
// Inner class implement
}
}

10

Main Method
The main method is the entry point for
a Java application.

public static void main(String[]


// Code to execute
}

Method Signature
Components:
public : Accessible from
anywhere

static : Can be called without


creating an instance

void : Doesn't return any value

main : Method name that JVM


looks for

String[] args : Command-line


arguments

Example:

public class HelloWorld {


public static void main(Stri
System.out.println("Hell

// 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

Prevention of naming conflicts

Access control

Easier to locate and maintain code

Package Declaration:

package com.example.utility;

Package Naming
Conventions:
Use lowercase letters

Use reverse domain name


convention (e.g.,
com.companyname.projectname )

Directory structure must match


package structure

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:

1. Explicit Import 3. Static Import

import java.uti import static j


import static j

2. Wildcard
Import

import java.uti

Import Rules:
java.lang.* is automatically
imported

Classes in the same package don't


need imports

Import statements must appear after


package declaration

Use fully qualified names for naming


conflicts

import java.util.Date; // From


// For java.sql.Date, use fully
java.sql.Date sqlDate = new java

13

Practice Exercise
1: Basic Class and
Object
Create a Person class with attributes
name , age , and email .

public class Person {


private String name;
private int age;
private String email;

// Constructor
public Person(String name, i
this.name = name;
this.age = age;
this.email = email;
}

// Getters and setters


public String getName() { re
public void setName(String n

public int getAge() { return


public void setAge(int age)

public String getEmail() { r


public void setEmail(String

@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
}

// Derived class example


public class Circle extends Shap
private double radius;

public Circle(double radius)


this.radius = radius;
}

@Override
public double calculateArea(
return Math.PI * radius
}

@Override
public double calculatePerim
return 2 * Math.PI * rad
}
}

Create similar implementations for


Rectangle and Triangle classes.

15

Practice Exercise
3: Encapsulation
Design a BankAccount class with
private attributes and methods for
deposit, withdrawal, etc.

public class BankAccount {


private String accountNumber
private String ownerName;
private double balance;

public BankAccount(String ac
this.accountNumber = acc
this.ownerName = ownerNa
this.balance = initialBa
}

public double getBalance() {


return balance;
}

public void deposit(double a


if (amount > 0) {
balance += amount;
System.out.println("
} else {
System.out.println("
}
}

public void withdraw(double


if (amount > 0 && amount
balance -= amount;
System.out.println("
} else {
System.out.println("
}
}
}

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;

public class Book {


private String isbn;
private String title;
private Author author;
// Methods...
}

// 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

public class LibraryApp {


public static void main(Stri
// Implementation...
}
}

17

Practice Exercise
5: Static Methods
and Variables
Create a MathUtils class with static
methods and a counter to track method
calls.

public class MathUtils {


// Static counter to track m
private static int findMinCa
private static int findMaxCa
private static int calculate

// Static method to find min


public static int findMin(in
findMinCalls++; // Incr

if (numbers == null || n
throw new IllegalArg
}

int min = numbers[0];


for (int i = 1; i < numb
if (numbers[i] < min
min = numbers[i]
}
}
return min;
}

// Other methods (findMax, c

// Method to get count of ca


public static void printStat
System.out.println("find
System.out.println("find
System.out.println("calc
}
}

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

public Empl Implementation


this.id
Class:
this.na
this.ba
public class Fu
}
extends
impleme
// Abstract
public abst
private dou
}

// Construc
// implemen

@Override
public doub
return
}
}

19

Practice Exercise
7: Complete
Application
Create a student management system
demonstrating all concepts covered:

Package structure for organization

Classes for Student, Course,


Enrollment

Proper encapsulation and


inheritance

Main application class

Package Structure:

com.university.model
com.university.service
com.university.app

Key Features to
Implement:
Add/remove students and courses

Enroll students in courses

Generate reports (student list,


course enrollment)

Calculate GPAs

This comprehensive exercise combines all


the Java concepts covered in this
presentation.
20

Summary

Java OOP Fundamentals


Covered:
Object-Oriented Programming:
Classes, objects, and the four key
principles

Class Structure: Organization of


Java classes

Main Method: Entry point for Java


applications

Packages: Organization and


namespace management

Imports: Using classes from other


packages

Next Steps:
Complete the practice exercises

Build small applications to reinforce


concepts

Explore advanced Java topics:


Collections Framework

Exception Handling

Multithreading

File I/O

21

Thank You!
Happy Java
Programming
Questions?

You might also like