DAY : 26 Protected keyword and abstraction

Protected keyword In Java, the protected keyword acts as an access modifier for class members (fields and methods). It allows access to those members from within the same package, and by subclasses, even if they are in different packages. Prerequisites: Encapsulation: 1.private - we can use within the class. 2.Default - we can use within the package 3.Public - we can use within the projects. 4.protected - we can use withing the package + child classes in other package. Abstraction 1.Abstraction in Java is a fundamental concept in object-oriented programming (OOP) that hides implementation details and only shows essential features. 2.if at least one method is abstract in a class, then the entire class should be abstract Abstract Methods and Classes An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(double deltaX, double deltaY); If a class includes abstract methods, then the class itself must be declared abstract, as in: public abstract class GraphicObject { // declare fields // declare nonabstract methods abstract void draw(); } Abstract only use in class and method. Not in a variable When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

Apr 16, 2025 - 04:22
 0
DAY : 26 Protected keyword and abstraction

Protected keyword

In Java, the protected keyword acts as an access modifier for class members (fields and methods). It allows access to those members from within the same package, and by subclasses, even if they are in different packages.

Prerequisites:

Encapsulation:
1.private - we can use within the class.
2.Default - we can use within the package
3.Public - we can use within the projects.
4.protected - we can use withing the package + child classes in other package.

Abstraction

1.Abstraction in Java is a fundamental concept in object-oriented programming (OOP) that hides implementation details and only shows essential features.
2.if at least one method is abstract in a class, then the entire class should be abstract

Image description

Abstract Methods and Classes

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class GraphicObject {
   // declare fields
   // declare nonabstract methods
   abstract void draw();
}

Abstract only use in class and method. Not in a variable

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.