Day -26:Protection keyword and Abstract
protected Keyword in Java The protected keyword in Java is an access modifier used for member variables and methods. It provides a level of access control that allows access within the same package and by subclasses, even if they are in different packages. Usage The protected access modifier is used to restrict access to members of a class such that they are accessible within their own package and by subclasses. This is particularly useful for creating a controlled inheritance structure. Encapsulation private --> within the class default --> within the package protected --> within the package + Child classes in other packages public ---> Within the project // Base class class Animal { protected String name; // protected variable protected void makeSound() { // protected method System.out.println(name + " makes a sound."); } } // Derived class class Dog extends Animal { void speak() { name = "Buddy"; // accessing protected variable System.out.println(name + " says Woof!"); makeSound(); // accessing protected method } } // Main class to run the program public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.speak(); // Accessing protected member from outside the package or class // System.out.println(dog.name); // ⚠️ Not allowed if from another package } } Abstraction in Java Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where we type the text and send the message. We do not know the internal processing about the message delivery. Ways to achieve Abstraction There are two ways to achieve abstraction in Java: Using Abstract Class (0 to 100%) Using Interface (100%) Abstract Class in Java An abstract class in Java acts as a partially implemented class that itself cannot be instantiated. It exists only for subclassing purposes, and provides a template for its subcategories to follow. Abstract classes can have implementations with abstract methods.[TBD] Abstract methods are declared to have no body, leaving their implementation to subclasses.[TBD] Syntax of Abstract Classes In Java, abstract classes are defined using the abstract keyword. Here's a basic syntax example: public abstract class Shape { public abstract double area(); public void display() { System.out.println("This is a shape."); } } In this example, Shape is an abstract class with one abstract method area() and one concrete method display(). Subclasses of Shape must implement the area() method, but they can inherit the display() method.[TBD] Reference link: https://www.datacamp.com/doc/java/protected

protected Keyword in Java
The protected keyword in Java is an access modifier used for member variables and methods. It provides a level of access control that allows access within the same package and by subclasses, even if they are in different packages.
Usage
The protected access modifier is used to restrict access to members of a class such that they are accessible within their own package and by subclasses. This is particularly useful for creating a controlled inheritance structure.
Encapsulation
private --> within the class
default --> within the package
protected --> within the package + Child classes in other packages
public ---> Within the project
// Base class
class Animal {
protected String name; // protected variable
protected void makeSound() { // protected method
System.out.println(name + " makes a sound.");
}
}
// Derived class
class Dog extends Animal {
void speak() {
name = "Buddy"; // accessing protected variable
System.out.println(name + " says Woof!");
makeSound(); // accessing protected method
}
}
// Main class to run the program
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.speak();
// Accessing protected member from outside the package or class
// System.out.println(dog.name); // ⚠️ Not allowed if from another package
}
}
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where we type the text and send the message. We do not know the internal processing about the message delivery.
Ways to achieve Abstraction
There are two ways to achieve abstraction in Java:
- Using Abstract Class (0 to 100%)
- Using Interface (100%)
Abstract Class in Java
An abstract class in Java acts as a partially implemented class that itself cannot be instantiated. It exists only for subclassing purposes, and provides a template for its subcategories to follow. Abstract classes can have implementations with abstract methods.[TBD] Abstract methods are declared to have no body, leaving their implementation to subclasses.[TBD]
Syntax of Abstract Classes
In Java, abstract classes are defined using the abstract keyword. Here's a basic syntax example:
public abstract class Shape {
public abstract double area();
public void display() {
System.out.println("This is a shape.");
}
}
In this example, Shape is an abstract class with one abstract method area() and one concrete method display(). Subclasses of Shape must implement the area() method, but they can inherit the display() method.[TBD]
Reference link:
https://www.datacamp.com/doc/java/protected