Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
1
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
2
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
3
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Object
~ Dr. Ayushi Verma
4
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
5
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Example: public class Car {
String brand= "Honda";
int speed = 120;
voidshowDetails() {
[Link]("Brand: " + brand);
[Link]("Speed: " + speed + " km/h");
}
public static void main(String[] args) {
CarmyCar = newCar();
[Link]();
}
}
~ Dr. Ayushi Verma
6
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
7
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
8
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Inheritance allows a class (subclass) to inherit properties and methods from another class (superclass).
Example: // Superclass
class Animal {
void eat() {
[Link]("This animal eats food.");
}
void sound() {
[Link]("This animal makes a sound.");
}
}
~ Dr. Ayushi Verma
9
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
// Subclass Dog inherits Animal
class Dog extends Animal {
void bark() {
[Link]("The dog barks: Woof Woof!");
}
}
// Subclass Cat inherits Animal
class Cat extends Animal {
void meow() {
[Link]("The cat meows: Meow Meow!");
}
/ Subclass Cow inherits Animal
}
class Cow extends Animal {
void moo() {
[Link]("The cow moos: Moo Moo!");
}
}
~ Dr. Ayushi Verma
10
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
public class AnimalTest {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
Cow cow = new Cow();
// Using methods from Animal class (superclass)
[Link](); [Link]();
[Link]();
[Link]();
[Link]();
[Link](); [Link]();
[Link]();
[Link](); }
[Link]();
}
[Link]();
~ Dr. Ayushi Verma
11
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
[Link] Level
[Link]-Level
[Link]
~ Dr. Ayushi Verma
12
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
13
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Types of Polymorphism
~ Dr. Ayushi Verma
14
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
[Link]-time Polymorphism (Method Overloading): Achieved by defining multiple
methods with the same name but different parameters within the same class.
public class OverloadingExample {
Example : Method Overloading
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
OverloadingExample obj = new OverloadingExample();
[Link]("Sum of integers: " + [Link](5, 10));
[Link]("Sum of doubles: " + [Link](5.5, 10.5));
}
}
~ Dr. Ayushi Verma
15
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
[Link] Polymorphism (Method Overriding): Achieved when a subclass provides a
specific implementation of a method already defined in its superclass.
Example : Method Overriding
class Animal { public class OverridingExample {
public void sound() { public static void main(String[] args) {
[Link]("Animal makes a sound"); Animal myAnimal = new Dog();
[Link](); // Outputs "Dog barks"
}
}
}
}
class Dog extends Animal {
@Override
public void sound() {
[Link]("Dog barks");
}
}
~ Dr. Ayushi Verma
16
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
17
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
18
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
19
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
20
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Purpose of Encapsulation
Data Hiding: Encapsulation allows the internal representation of an object to be
hidden from the outside. Only the necessary details are exposed through a public
interface.
Increased Flexibility: By controlling access to the fields of a class, you can change
the internal implementation without affecting the external code using the class.
Improved Maintainability: Encapsulation helps in maintaining the code by keeping
the fields private and providing public getter and setter methods to modify and view
the fields.
~ Dr. Ayushi Verma
21
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
// Setter method for name
public void setName(String name) {
[Link] = name;
}
// Getter method for age
Example :Basic Encapsulation public int getAge() {
public class Student { return age;
private String name; }
private int age;
// Setter method for age
public void setAge(int age) {
// Getter method for name
if (age > 0) {
public String getName() {
[Link] = age;
return name;
}
}
}
}
~ Dr. Ayushi Verma
22
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
// Create another student
Student student2 = new Student();
public class Main {
[Link]("Bob");
public static void main(String[] args) {
[Link](22);
// Create a new Student object
Student student1 = new Student();
[Link]("\nSecond Student:");
// Set values using setter methods [Link]("Name: " + [Link]());
[Link]("Alice"); [Link]("Age: " + [Link]());
[Link](20); }
}
// Try to set invalid age
[Link](-5); // This will print an error message
// Get values using getter methods
[Link]("Student Name: " + [Link]());
[Link]("Student Age: " + [Link]());
~ Dr. Ayushi Verma
23
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
24
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
25
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
26
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
27
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
28
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
29
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
// Abstract class // Main class
abstract class Vehicle { public class Main {
abstract void start(); // Abstract method (no body) public static void main(String[] args) {
Car myCar = new Car();
voidfuel() { // Non-abstract method (has body) [Link]();
[Link]("Filling fuel."); [Link]();
} }
} }
// Subclass
class Car extends Vehicle {
voidstart() { // Implementing abstract method
[Link]("Car is starting.");
}
}
~ Dr. Ayushi Verma
30
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Interfaces in Java Features of Interfaces:
An interface in Java is probably the most powerful Interface fields by default are public, static, and final.
tool for achieving abstraction. Interface methods are public and abstract by default.
It acts like a contract that defines a set of methods Interfaces include both methods and variables but lack a
a class must implement. method body.
Unlike abstract classes, the key difference is that, No Constructors: Interfaces do not have constructors
by using interfaces, we can achieve 100% and cannot create objects directly.
abstraction in Java classes. Implicitly Public Methods: All methods in an interface are
Interfaces allow multiple inheritances, which means implicitly public and abstract.
a class can implement several interfaces.
Supports Multiple Inheritance.
~ Dr. Ayushi Verma
31
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
// Declaring an interface
Syntax of Interface: interface Shape {
void draw(); // Automatically public and abstract
}
// Class implementing the interface
class Circle implements Shape {
public void draw() { // Implementation of the interface method
[Link]("Drawing a circle.");
}
}
// Testing the implementation
public class Main {
public static void main(String[] args) {
Shape shape = new Circle(); // Polymorphic reference
[Link]();
}
}
~ Dr. Ayushi Verma
32
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Example of an Interface:
~ Dr. Ayushi Verma
33
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Interface Abstract class
Interface support multiple implementations. Abstract class does not support multiple inheritance.
Interface does not contain Data Member Abstract class contains Data Member
Interface does not contain Constructors
Abstract class contains Constructors
An interface Contains only incomplete member (signature of An abstract class Contains both incomplete (abstract) and
member) complete member
An interface cannot have access modifiers by default An abstract class can contain access modifiers for the subs,
everything is assumed as public functions, properties
Member of interface can not be Static Only Complete Member of abstract class can be Static
~ Dr. Ayushi Verma
34
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Difference between Abstraction and Encapsulation
Feature Abstraction Encapsulation
Usage Abstraction is the outer layout that is used in terms Encapsulation is the inner layout used in terms of
Usage
of design. implementation.
Focus or Purpose It hides implementation details while showing functionality. It hides data, code and controls access.
This can abstract classes and interfaces. This can access modifiers (private, protected,
Achieved By public)
Purpose It simplifies usage and improves readability. It protects data and maintains integrity.
Example Using abstract methods for behaviors. Using getters and setters to control access.
~ Dr. Ayushi Verma
35
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Packages: Defining Package, CLASSPATH Setting for Packages, Making JAR Files
forLibrary Packages, Import and Static Import Naming Convention For Packages
A package in Java is a mechanism to group related classes,
interfaces, and sub-packages. It helps in organizing code and
avoiding naming conflicts.
Packages are divided into two categories:
Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)
~ Dr. Ayushi Verma
36
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
[Link]
~ Dr. Ayushi Verma
37
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
To use a class or a package from the library, you need to use the import keyword:
Syntax
import [Link]; // Import a single class
import [Link].*; // Import the whole package
~ Dr. Ayushi Verma
38
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Example:
import [Link];
~ Dr. Ayushi Verma
39
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Example:
Using the Scanner class to get user input:
~ Dr. Ayushi Verma
40
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Example:
import [Link].*;
~ Dr. Ayushi Verma
41
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Example:
~ Dr. Ayushi Verma
42
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
To create a package, use the package keyword:
[Link]
~ Dr. Ayushi Verma
43
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
CLASSPATH Setting for Packages
The CLASSPATH is an environment variable that tells the JVM where to look for
user-defined classes and packages.
~ Dr. Ayushi Verma
44
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
[Link]
~ Dr. Ayushi Verma
45
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
46
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
47
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
48
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
49
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
50
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
51
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
52
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Making JAR Files for Library Packages
A JAR (Java Archive) file is a compressed format that bundles multiple
Java classes and resources into a single file.
~ Dr. Ayushi Verma
53
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
54
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Here class1, class2, etc are the classes in the package pack. The first
two entries represent that there is a manifest file created and added
to [Link]. The third entry represents the sub-directory with the
name pack and the last two represent the files name in the directory
pack.
~ Dr. Ayushi Verma
55
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
56
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
57
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
~ Dr. Ayushi Verma
58
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Import and Static Import Naming Convention For Packages
a) Import Statement
Used to include classes from other packages.
Syntax:
import package_name.class_name; // Import single class
import package_name.*; // Import all classes in the package
~ Dr. Ayushi Verma
59
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Syntax:
import [Link];
public class Main
{
public static void main(String[] args)
{
Calculator calc = new Calculator();
[Link]([Link](5, 3)); // Output: 8
}
}
~ Dr. Ayushi Verma
60
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
b) Static Statement
Allows importing static members (variables/methods) of a class directly..
Syntax:
import static package_name.class_name.static_member;
~ Dr. Ayushi Verma
61
Dept. of CSE, LIET, Gr. Noida
Object Oriented Programming with Java (BCS403)
Syntax:
import static [Link];
import static [Link];
public class Circle {
public static void main(String[] args) {
double radius = 5.0;
double area = PI * pow(radius, 2); // No need to write [Link] or [Link]
[Link]("Area: " + area);
}
}
~ Dr. Ayushi Verma
62
Dept. of CSE, LIET, Gr. Noida