Day 7 and 8
Day 7 and 8
Encapsulation: Binding data (attributes) and methods (functions) that manipulate the
data within a single unit (class). It helps in data hiding and protecting object integrity.
Abstraction: Hiding complex implementation details and exposing only the essential
features of an object.
Inheritance: Allows one class (subclass/child) to inherit properties and methods from
another class (superclass/parent).
Polymorphism: Allows objects to be treated as instances of their parent class.
Polymorphism supports method overloading (compile-time) and method overriding
(runtime).
Class:
A class is a blueprint or template for creating objects. It defines the properties (fields) and
behaviors (methods) of the object.
Syntax of a Class:
class ClassName {
// fields (attributes)
int field1;
String field2;
// methods (behaviors)
void methodName() {
// method body
Object:
An object is an instance of a class. It represents a real-world entity and is created from the
class.
Creating an Object:
// Fields (attributes)
String brand;
int speed;
// Method (behavior)
void displayInfo() {
myCar.brand = "Toyota";
myCar.speed = 120;
Class: Car defines two fields (brand and speed) and a method (displayInfo).
Object: myCar is an instance of the Car class, and the method displayInfo is called
on it.
3. Constructors in Java
A constructor is a special method used to initialize objects. A constructor has the same name
as the class and no return type.
Types of Constructors:
String title;
int pages;
// Default constructor
Book() {
title = "Unknown";
pages = 0;
void display() {
}
Example: Parameterized Constructor:
String title;
int pages;
// Parameterized constructor
Book(String t, int p) {
title = t;
pages = p;
void display() {
4. Encapsulation
Encapsulation is the process of wrapping data (fields) and methods into a single unit (class).
It restricts direct access to fields and allows access through methods (getters and setters).
Example of Encapsulation:
// Private fields
return name;
this.name = name;
return age;
this.age = age;
person.setName("John");
person.setAge(30);
}
The name and age fields are private and can only be accessed through the getName, setName,
getAge, and setAge methods, ensuring data security.
5. Inheritance in Java
Inheritance allows a class to inherit fields and methods from another class. It promotes code
reusability and establishes a parent-child relationship between classes.
Syntax:
class ParentClass {
Example of Inheritance:
// Superclass
class Animal {
String name;
void makeSound() {
// Subclass
void bark() {
dog.name = "Buddy";
6. Polymorphism in Java
Polymorphism allows objects to take many forms. The two types of polymorphism in Java
are:
// Superclass
class Animal {
void makeSound() {
// Subclass
@Override
void makeSound() {
Define a class Person with attributes name and age, and a method introduce to print a
greeting message.
String name;
int age;
void introduce() {
System.out.println("Hi, I am " + name + " and I am " + age + " years old.");
person1.name = "Alice";
person1.age = 25;
person1.introduce();
}
Problem 2: Create a LibraryBook Class with Constructor
Create a class LibraryBook that includes attributes like title, author, and ISBN. Create a
constructor to initialize these values and a method to display them.
String title;
String author;
String ISBN;
// Parameterized constructor
this.title = title;
this.author = author;
this.ISBN = ISBN;
void displayDetails() {
System.out.println("Title: " + title + ", Author: " + author + ", ISBN: " + ISBN);
book.displayDetails();
Create a base class Vehicle with a method start. Derive a subclass Car that inherits the
start method and adds a drive method.
class Vehicle {
void start() {
void drive() {