0% found this document useful (0 votes)
42 views10 pages

Day 7 and 8

Introduction to basic java

Uploaded by

Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views10 pages

Day 7 and 8

Introduction to basic java

Uploaded by

Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Overview of Object-Oriented Programming (OOP)

Java is an Object-Oriented Programming (OOP) language, which means it follows the


OOP paradigm to design and develop software. OOP allows you to model real-world entities
and interactions using objects and classes.

Key Features of OOP:

 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).

2. Classes and Objects in Java

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:

ClassName obj = new ClassName();

Example: Class and Object in Java

public class Car {

// Fields (attributes)

String brand;

int speed;

// Method (behavior)

void displayInfo() {

System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");

public static void main(String[] args) {

// Creating an object of the Car class

Car myCar = new Car();

// Setting the values of the object's fields

myCar.brand = "Toyota";

myCar.speed = 120;

// Calling the object's method

myCar.displayInfo(); // Output: Brand: Toyota, Speed: 120 km/h

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

1. Default Constructor: Provided by Java when no constructor is explicitly defined.


2. Parameterized Constructor: Allows passing parameters to initialize object fields
when the object is created.

Example: Default Constructor:

public class Book {

String title;

int pages;

// Default constructor

Book() {

title = "Unknown";

pages = 0;

void display() {

System.out.println("Title: " + title + ", Pages: " + pages);

public static void main(String[] args) {

Book myBook = new Book(); // Calls default constructor

myBook.display(); // Output: Title: Unknown, Pages: 0

}
Example: Parameterized Constructor:

public class Book {

String title;

int pages;

// Parameterized constructor

Book(String t, int p) {

title = t;

pages = p;

void display() {

System.out.println("Title: " + title + ", Pages: " + pages);

public static void main(String[] args) {

Book myBook = new Book("Java Programming", 500); // Calls parameterized constructor

myBook.display(); // Output: Title: Java Programming, Pages: 500

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:

public class Person {

// Private fields

private String name;

private int age;


// Public getter method

public String getName() {

return name;

// Public setter method

public void setName(String name) {

this.name = name;

// Public getter method

public int getAge() {

return age;

// Public setter method

public void setAge(int age) {

this.age = age;

public static void main(String[] args) {

Person person = new Person();

person.setName("John");

person.setAge(30);

System.out.println("Name: " + person.getName());

System.out.println("Age: " + person.getAge());

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

 Superclass (Parent): The class being inherited from.


 Subclass (Child): The class that inherits from the superclass.

Syntax:

class ParentClass {

// Fields and methods

class ChildClass extends ParentClass {

// Additional fields and methods

Example of Inheritance:

// Superclass

class Animal {

String name;

void makeSound() {

System.out.println(name + " makes a sound.");

// Subclass

class Dog extends Animal {

void bark() {

System.out.println(name + " barks.");


}

public class Main {

public static void main(String[] args) {

Dog dog = new Dog();

dog.name = "Buddy";

dog.makeSound(); // Inherited from Animal class

dog.bark(); // Defined in Dog class

6. Polymorphism in Java

Polymorphism allows objects to take many forms. The two types of polymorphism in Java
are:

1. Compile-time Polymorphism (Method Overloading): Multiple methods have the


same name but different parameter types or numbers.
2. Run-time Polymorphism (Method Overriding): A subclass provides a specific
implementation of a method that is already defined in its superclass.

Example of Method Overriding:

// Superclass

class Animal {

void makeSound() {

System.out.println("The animal makes a sound");

// Subclass

class Dog extends Animal {

// Overriding the method of the superclass

@Override
void makeSound() {

System.out.println("The dog barks");

public class Main {

public static void main(String[] args) {

Animal myDog = new Dog(); // Upcasting

myDog.makeSound(); // Output: The dog barks

7. Practice Problems (Day 7–8)

Problem 1: Define a Person Class

Define a class Person with attributes name and age, and a method introduce to print a
greeting message.

public class Person {

String name;

int age;

void introduce() {

System.out.println("Hi, I am " + name + " and I am " + age + " years old.");

public static void main(String[] args) {

Person person1 = new Person();

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.

public class LibraryBook {

String title;

String author;

String ISBN;

// Parameterized constructor

LibraryBook(String title, String author, String ISBN) {

this.title = title;

this.author = author;

this.ISBN = ISBN;

// Method to display book details

void displayDetails() {

System.out.println("Title: " + title + ", Author: " + author + ", ISBN: " + ISBN);

public static void main(String[] args) {

LibraryBook book = new LibraryBook("Java Programming", "John Doe", "1234567890");

book.displayDetails();

Problem 3: Demonstrate Inheritance

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() {

System.out.println("The vehicle starts.");

class Car extends Vehicle {

void drive() {

System.out.println("The car drives.");

public class Main {

public static void main(String[] args) {

Car myCar = new Car();

myCar.start(); // Inherited from Vehicle class

myCar.drive(); // Defined in Car class

Day 7–8 Goals

 Understand the concepts of OOP: classes, objects, constructors, encapsulation,


inheritance, and polymorphism.
 Practice creating classes and objects, working with constructors, and using
inheritance and polymorphism.
 Gain confidence in applying OOP concepts to solve problems using real-world
examples.

You might also like