2. Learning Outcome
● On successful completion of this module,
○ Explain the fundamentals of Object-Oriented Programming concepts
○ Design Object-Oriented based applications
○ Develop Object-Oriented applications
3. Lesson -1 : fundamentals of Object-Oriented
Programming
4. Toady’s Aim
● Explain the fundamentals of Object-Oriented Programming
○ Understand and describe OOP
○ Understand and describe OOP concepts
■ Inheritance
■ Aptraction
■ Encapsulation
■ Polymorphism
5. Good Programming
● more code not necessarily a good thing
● measure good programmers by the amount of functionality
● code can be used many times but only has to be debugged once!
● Code should be
○ often self-contained
○ used to break up code
○ intended to be reusable
○ keep code organized
○ keep code coherent
9. Object:
● A specific real world entity
● Objects have all the attributes specified in the class definition
● An object is an instance of a concept
○ 1234 is an instance of an int
○ "hello" is an instance of a string
10. Class:
● Specifies the characteristics of an entity but is not an instance of that entity
● A class is a blueprint or template that defines the structure of the object.
11. Format:
public class <name of class>
{
attributes
methods
}
Example:
public class Employee {
private int name; // Attribute - stores the name of the employee
public void sayName() // Method - prints the name of the employee
{
System.out.println("My name is " + name);
}
}
12. Activity: What is the value of X?
public class Calculator{
int x = 10;
public static void main(String[] args) {
Calculator myObj = new Calculator();
myObj.x = 25; // x is now 25
System.out.println(myObj.x);
}
}
Attributes
Method
Object
14. Inheritance
● Inheritance is a fundamental concept in object-oriented programming (OOP)
that allows a class to inherit properties and behaviors from another class.
● Inheritance represents the IS-A relationship which is also known as a parent-
child relationship
● It enables code reuse and promotes the organization and hierarchy of classes.
○ Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a
derived class, extended class, or child class.
○ Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It
is also called a base class or a parent class.
16. Example of inheritance
class Employee{
float salary=80000;
}
class Programmer extends Employee{
int bonus=30000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
17. Activity
● Identify the differences between all 5 types of the inheritance ?
● Find the examples for each type of inheritance?
19. Encapsulation
● "sensitive" data is hidden from users
● way of hiding the implementation details of a class from outside access and
only exposing a public interface that can be used to interact with the class.
● encapsulation is achieved by declaring the instance variables of a class as
private,
20. class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) { this.age = age; }
}
public class Display{
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());
}
}