In Java, an abstract class is a special class that cannot be used to create objects directly. Instead, it provides a template or example for copying other classes. It is designed to be extended with subclasses that provide specific instructions for how the abstract class works.
An abstract class is a special class that has the word "abstract" in its declaration. For example, a class called MyClass that is not directly accessible. An abstract class is a unique class that can have methods without any real code. Subclasses must create their own versions of all methods required by the abstract class. Subclasses must make their own versions of any methods not fully defined in the abstract class.
Abstract methods are a type of special method declared with a specific keyword called "abstract". They are empty and contain no instructions or computer commands. Abstract classes can have clear and well-defined methods called concrete methods. These methods set behavior that can be shared with lower classes. Concrete methods are ordinary methods that do not contain the word "abstract".
Objects of an abstract class cannot be created using the "new" keyword because abstract classes cannot be instantiated directly. But you can use a generic class to talk about specific objects of that specific type. Abstract classes are made to complement other classes. When a subclass extends an abstract class, it must create its own versions of all methods defined by the abstract class, but those versions need not contain the actual code. If a subclass does not have code for specific abstract methods, it must also be marked as "abstract". Abstract classes are usually used to create a common layout for a group of similar classes. They also provide certain features or functions that are already configured for use. They help reuse code and ensure that subclasses follow a certain format.
Simply put, an abstract class in Java is like a blueprint for other classes. It does not work by itself and needs other classes to modify it with certain methods. This helps you establish a common way of doing things and gives you a starting point for subclasses.
// Abstract class Syntax
abstarct class Space
{
int address;
// Abstarct method
abstract void city( );
}
package Interview;
//create abstract class
public abstract class Rain{
/* create abstract methods
* start and stop
*/
abstract void start();
abstract void stop();
//create nested class city
public static class City extends Rain {
/* Declared methods start and stop
* from abstract class Rain
*/
void start() {
System.out.println("Rain started");
}
void stop() {
System.out.println("Rain Stopped");
}
}
public static void main(String[] args) {
//create object of class city
City obj = new City();
//call methods start and stop
obj.start();
obj.stop();
/* Output->
* Rain started
* Rain Stopped
*/
}
}
package Interview;
// Create abstract class
public abstract class He {
// Create abstract method MethodA
abstract void MethodA();
/* Create nested class You
* which extends the He class
*/
public static class You extends He {
/* Implement the abstract method
* MethodA from the He class
*/
public void MethodA() {
System.out.println("Message");
}
}
public static void main(String[] args) {
/* Create an object of
* the nested class You
* using the enclosing class name
*/
He.You obj1 = new He.You();
obj1.MethodA();
/* Call the overridden MethodA
* from the You class
*/
//Output:- Message
}
}
package Interview;
// Create Abstract class
/* This program is about
* abstract class having
* no abstract method
*/
public abstract class Mother {
/* Create Method working
* (Not an abstract method)
*/
public void Working () {
System.out.println
("Method working called");
}
/* create Nested class daughter
* which extends superclass Mother
*/
public static class
daughter extends Mother {
}
public static void main(String[] args) {
daughter obj = new daughter();
obj.Working();
//Output->
// Method working called
}
}
package Interview;
/* This program is about
* we can call static method
* of abstract class in main
*/
//create abstract class
public abstract class LG {
//create static method
static void method() {
System.out.println("Method calling");
}
public class son extends LG {
}
public static void main(String[] args) {
/* called static method
* defined in abstract class LG
* (Superclass)
*/
LG.method();
//Output-> Method calling
}
}
No comments:
Post a Comment