0% found this document useful (0 votes)
4 views6 pages

Research Phase

Uploaded by

dennisdionsay2
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)
4 views6 pages

Research Phase

Uploaded by

dennisdionsay2
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/ 6

Explanation:

Encapsulation: Hide the internal state of an object and ensures to


controlled access through getter and setter methods. To not to
preventing direct access from outside of the class.

Example:
// Employee class demonstrating Encapsulation

public class Employee {

// Private fields for encapsulation (data hiding)

private String name;

private int age;

private double salary;

// Constructor to initialize employee details

public Employee(String name, int age, double salary) {

this.name = name;

this.age = age;

this.salary = salary;

// Getter for 'name'

public String getName() {

return name;

// Setter for 'name'

public void setName(String name) {

this.name = name;

// Getter for 'age'

public int getAge() {

return age;
}

// Setter for 'age' with validation

public void setAge(int age) {

if (age > 18) {

this.age = age;

} else {

System.out.println("Age must be > 18");

// Getter for 'salary'

public double getSalary() {

return salary;

// Setter for 'salary' with validation

public void setSalary(double salary) {

if (salary >= 0) {

this.salary = salary;

} else {

System.out.println("Salary can't be negative.");

// Display employee details

public void displayDetails() {

System.out.println("Name: " + name + ", Age: " + age + ", Salary: $" + salary);

public static void main(String[] args) {

Employee emp = new Employee("John Doe", 30, 50000);

emp.displayDetails(); // Display initial details

// Modifying details using setters

emp.setName("Jane Smith");

emp.setAge(25);
emp.setSalary(55000);

emp.displayDetails(); // Display updated details

// Invalid updates

emp.setAge(16); // Age must be > 18

emp.setSalary(-100); // Salary can't be negative

Inheritance: Allow to the class to inherit attributes and methods


from another class. It also allows the subclass to extend or modify all of
the behavior of the parent class while maintaining the core
functionality.
Example: class Animal {

String name;

public Animal(String name) {

this.name = name;

public void makeSound() {

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

class Dog extends Animal {

public Dog(String name) {

super(name);

@Override

public void makeSound() {

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

public void fetch() {

System.out.println(name + " is fetching the ball.");


}

public class Main1 {

public static void main(String[] args) {

Animal animal = new Animal("Generic Animal");

animal.makeSound();

Dog dog = new Dog("Buddy");

dog.makeSound();

dog.fetch();

Polymorphism: Allows objects to be treated as instances of their


superclass and enables method overriding for dynamic behavior at
runtime. It also enables dynamic behavior at runtime, allowing the
program to decide which method to invoke based on the actual object.

Example: class Animal {


public void makeSound() {

System.out.println("Animal makes a sound");

class Dog extends Animal {

@Override

public void makeSound() {

System.out.println("Dog barks");

class Cat extends Animal {

@Override

public void makeSound() {


System.out.println("Cat meows");

public class Main {

public static void main(String[] args) {

Animal animal = new Animal();

Animal dog = new Dog();

Animal cat = new Cat();

animal.makeSound();

dog.makeSound();

cat.makeSound();

Abstraction: Hides implementation details and


exposes only essential features of an object using
abstract classes or interfaces. It allows subclasses
to focus on specific behavior while ensuring
consistency and structure by enforcing a common
interface or base class.

Example:
abstract class Animal {

public abstract void makeSound();

public void sleep() {

System.out.println("This animal is sleeping");

class Dog extends Animal {

@Override

public void makeSound() {


System.out.println("Dog barks");

class Cat extends Animal {

@Override

public void makeSound() {

System.out.println("Cat meows");

public class Main {

public static void main(String[] args) {

Animal myDog = new Dog();

Animal myCat = new Cat();

myDog.makeSound();

myCat.makeSound();

myDog.sleep();

myCat.sleep();

You might also like