Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

AutomationTesting_Java_12

The document explains key Object-Oriented Programming concepts in Java, focusing on abstraction and encapsulation. Abstraction is achieved through abstract classes and interfaces, allowing programmers to hide implementation details and emphasize functionality. Encapsulation involves bundling data and methods within a class, controlling access to data through private members and public getter/setter methods to enhance security and maintainability.

Uploaded by

vikas bisen
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

AutomationTesting_Java_12

The document explains key Object-Oriented Programming concepts in Java, focusing on abstraction and encapsulation. Abstraction is achieved through abstract classes and interfaces, allowing programmers to hide implementation details and emphasize functionality. Encapsulation involves bundling data and methods within a class, controlling access to data through private members and public getter/setter methods to enhance security and maintainability.

Uploaded by

vikas bisen
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Abstraction in Java is one of the fundamental Object-Oriented Programming (OOP)

concepts that hides


the implementation details and only shows the functionality to the user.
The main goal of abstraction is to reduce complexity and allow the programmer to
focus on
what an object does, instead of how it achieves it.

In Java, abstraction is achieved using:

Abstract classes
Interfaces
1. Abstract Class
An abstract class is a class that cannot be instantiated directly. It can have
abstract methods (methods without a body) as well as concrete methods (methods with
a body). An abstract class is meant to be extended by other classes to provide
implementations for the abstract methods.

Key Points about Abstract Classes:


An abstract class can have both abstract (unimplemented) and concrete (implemented)
methods.
It can have member variables and constructors.
A class that contains at least one abstract method must be declared as abstract.
Example:

abstract class Animal {


public abstract void sound();

public void sleep() {


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

class Dog extends Animal {

public void sound() {


System.out.println("Barking!");
}
}

public class Test {


public static void main(String[] args) {
// Cannot instantiate an abstract class directly
// Animal animal = new Animal(); // Error

// Can create an instance of a subclass


Dog dog = new Dog();
dog.sound(); // Output: Barking!
dog.sleep(); // Output: This animal is sleeping
}
}
2. Interface
An interface is a reference type, similar to a class, that can contain only
constants, method signatures,
default methods, static methods, and nested types. Interfaces cannot contain
instance fields
or constructors. A class implements an interface, thereby inheriting the abstract
methods of
the interface.
Key Points about Interfaces:
All methods in an interface are implicitly abstract (unless they are default or
static).
A class implements an interface and provides the implementation of the abstract
methods.
A class can implement multiple interfaces (Java supports multiple inheritance
through interfaces).
A class must implement all methods of the interface (unless the class is abstract).
Example:

java
Copy
interface Animal {

void sound();

default void sleep() {


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

class Dog implements Animal {

public void sound() {


System.out.println("Barking!");
}
}

public class Test {


public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Barking!
dog.sleep(); // Output: This animal is sleeping
}
}

-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

Encapsulation is one of the key concepts in Object-Oriented Programming (OOP), and


it refers to the
bundling of data (variables) and the methods (functions) that operate on that data
into a single unit
known as a class. Encapsulation also helps in controlling the access to the data,
making sure that the data
is protected from unauthorized access and modification.

In simpler terms, encapsulation is about restricting access to some of an object's


components and ensuring that only necessary and controlled access is provided.

Key Concepts of Encapsulation:


Private Data Members: Data members (variables) of a class are marked as private so
that they are hidden from other classes.
Public Getter and Setter Methods: These methods provide controlled access to the
private data members.
Getter methods retrieve the value of a private variable.
Setter methods set the value of a private variable.
Access Control: You can control how a class's variables are accessed, modified, or
updated using getters and setters.
Why Encapsulation?
Data Hiding: The internal details of the implementation are hidden from outside
interference and misuse.
Improved Security: By controlling access to the data, you can ensure that the
object’s state remains valid and that only legitimate changes are made.
Flexibility: You can change the internal implementation without affecting other
parts of the program, as long as the getter and setter methods remain consistent.
Code Maintenance: Encapsulation promotes code reusability and easier maintenance.
Example of Encapsulation in Java:

class Employee {
// Private data members
private String name;
private int age;
private double 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


public void setAge(int age) {
if (age > 0) { // Control the age to ensure it's a positive value
this.age = age;
} else {
System.out.println("Age must be positive.");
}
}

// Getter for salary


public double getSalary() {
return salary;
}

// Setter for salary


public void setSalary(double salary) {
if (salary > 0) { // Ensure the salary is positive
this.salary = salary;
} else {
System.out.println("Salary must be positive.");
}
}
// Method to display employee details
public void display() {
System.out.println("Employee Name: " + getName());
System.out.println("Employee Age: " + getAge());
System.out.println("Employee Salary: $" + getSalary());
}
}

public class Test {


public static void main(String[] args) {
// Create an object of the Employee class
Employee emp = new Employee();

// Set values using setter methods


emp.setName("John Doe");
emp.setAge(30);
emp.setSalary(50000);

// Display the employee details using the display method


emp.display();

// Try setting invalid values


emp.setAge(-5); // Will show an error message: Age must be positive.
emp.setSalary(-1000); // Will show an error message: Salary must be
positive.
}
}

You might also like