A. Object-Oriented Programming Concepts with explanation with example in java:
Object-oriented programming (OOP) is a programming paradigm that uses objects to model real-world entities and their behavior. It involves the concepts of encapsulation, inheritance, and polymorphism.
Example in Java:
public class Car {
private String make;
private String model;
private int year;
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public void start() {
// code to start the car
}
public void stop() {
// code to stop the car
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
}
In this example, Car
is a class that represents a real-world entity. It has attributes like make
, model
, and year
, and behaviors like start()
and stop()
. Objects of this class can be created to represent actual cars.
B. Classes and Objects with explanation with example in java:
A class is a blueprint or template that defines the characteristics and behaviors of an object. An object is an instance of a class. A class can have attributes (also called fields or properties) and methods (also called functions or behaviors).
Example in Java:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void sayHello() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
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;
}
}
In this example, Person
is a class that has attributes name
and age
, and a method sayHello()
. Objects of this class can be created to represent actual people.
C. Constructors and Methods with explanation with example in java:
Constructors are special methods that are called when an object is created. They are used to initialize the attributes of the object. Methods are functions that define the behavior of the object.
Example in Java:
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getCircumference() {
return 2 * Math.PI * radius;
}
}
In this example, Circle
is a class that has a constructor that initializes the radius
attribute. It also has methods getArea()
and getCircumference()
that calculate the area and circumference of the circle.
D. Access Modifiers with explanation with example in java:
Access modifiers are keywords that determine the accessibility of attributes and methods of a class. There are four access modifiers in Java: public, private, protected, and default.
Example in Java:
public class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
}
public double getBalance(){
return balance; }
}
In this example, the balance
attribute is declared as private
, which means it can only be accessed within the class itself. The deposit()
and withdraw()
methods are declared as public
, which means they can be accessed from outside the class. The getBalance()
method is also declared as public
, so that it can be used to get the value of the balance
attribute from outside the class.
E. Encapsulation with explanation with example in java:
Encapsulation is a concept in OOP that refers to the practice of hiding the internal details of an object from the outside world. This is done by declaring the attributes of a class as private
, and providing public methods (also called getters and setters) to access and modify them.
Example in Java:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = 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;
}
}
In this example, the name
and age
attributes are declared as private
, which means they cannot be accessed from outside the Student
class. Instead, public getter and setter methods (getName()
, setName()
, getAge()
, setAge()
) are provided to access and modify these attributes.
F. Inheritance with explanation with example in java:
Inheritance is a mechanism in OOP that allows a new class to be based on an existing class, inheriting the attributes and methods of the parent class. The new class is called the child class or subclass, and the existing class is called the parent class or superclass.
Example in Java:
public class Animal {
protected String species;
public Animal(String species) {
this.species = species; }
public void eat() {
System.out.println("The " + species + " is eating.");
} }
public class Cat extends Animal {
private String name;
public Cat(String name) {
super("cat");
this.name = name; }
public void meow() {
System.out.println("The " + species + " named " + name + " is meowing.");
} }
In this example, Animal
is a parent class that has an attribute species
and a method eat()
. Cat
is a child class that extends Animal
and adds an attribute name
and a method meow()
. The super("cat")
call in the Cat
constructor initializes the species
attribute of the parent class to "cat".
G. Polymorphism with explanation with example in java:
Polymorphism is a concept in OOP that allows objects of different classes to be treated as if they are objects of the same class. This is achieved through method overriding and method overloading.
Example in Java:
public class Shape {
public void draw() {
System.out.println("Drawing a shape.");
}
}
public class Circle extends Shape {
public void draw() {
System.out.println("Drawing a circle.");
}
}
public class Square extends Shape {
public void draw() {
System.out.println("Drawing a square.");
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Square();
shape1.draw(); // prints "Drawing a circle." shape2.draw(); // prints "Drawing a square." } }
In this example, Shape
is a parent class that has a method draw()
. Circle
and Square
are child classes that override the draw()
method to draw a circle and a square, respectively. In the Main
class, we create two Shape
objects shape1
and shape2
, which are actually instances of Circle
and Square
, respectively. When we call the draw()
method on these objects, the appropriate implementation of the method is called based on the actual class of the object at runtime. This is an example of method overriding, which is a form of polymorphism.