0% found this document useful (0 votes)
19 views3 pages

Abstraction Theory

Abstraction in object-oriented programming involves hiding implementation details and exposing only essential information, which simplifies code and enhances security. Abstract classes and methods contribute to abstraction by preventing direct instantiation and requiring subclasses to provide implementations. Interfaces further achieve complete abstraction by containing only abstract methods, allowing for more flexible design and inheritance.
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)
19 views3 pages

Abstraction Theory

Abstraction in object-oriented programming involves hiding implementation details and exposing only essential information, which simplifies code and enhances security. Abstract classes and methods contribute to abstraction by preventing direct instantiation and requiring subclasses to provide implementations. Interfaces further achieve complete abstraction by containing only abstract methods, allowing for more flexible design and inheritance.
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

1.Define abstraction and explain its role in object-oriented programming.

Ans: Abstraction means hiding the implementation from the end user and showing only necessary information.

Role in object oriented programming:-

Abstraction allow programmer to build the clean application. In abstraction internal working is hide
and only the necessary information is shown with the help of the interfaces and abstract classes. It will reduce
the complexity of the large programs.It will also improve the secuirity.It will also support a polymorphism.

2. How do abstract classes and abstract methods contribute to abstraction?

Ans: Abstract class used abstract methods and concreate methods.By making the class abstract,you can not
create the object of that class and abstract method do not have the body so there is no implementation is
abstract method all the important code is in sub class which implements the abstract class. As the there is no
implementation of any actual information shown to the user. So that way abstract methods and abstract class
contribute to abstraction,but with the help of the abstract classes,you can not achieve the 100 percent
abstraction.

4. Why use interfaces for achieving abstraction in Java?

Ans:

100 percentage abstraction is not possible by the help of the abstract class, So the interface are used because
interface has only abstract methods and the implementation of all the methods are hide so interface is used.

5. Can an abstract class be instantiated? Why or why not?

Ans:

No, Abstract methods has both the methods Abstract and non abstract.abstract class can not instantiated
because it has abstract methods also and abstract method don’t have the fuctionality,

If you create the object for the abstract class it will don’t have the full functionality. So in java abstract class can
not be instantiated.

6. Explain the concept of abstraction with an example from Java.

Ans:

Abstraction is mechanism where only the necessary information is shown and all the complex things are hide.
In java Abstraction is used to improve the secuirity and maintanibility of the prgrams.To

Achieve the abstraction the abstract classes and interfaces is used.

Example of the abstractin:

Interface runnable{
Void run();

Class human implements Runnable

Void run()

System.out.println(“mens are running..);

class Animal implements Runnable{

void run(){

System.out.println(“dog is running”);

Class MainClass{

Public static void main(String args[]){

Runnable r=new Human();

r.run();

Runnable r1=new Animal();

r.run();

7. How does abstraction enhance the maintainability of code?

Ans:

Abstraction makes code easier to maintain by hiding complex details and showing only the important parts.
This keeps the code simple and clear. It also helps reuse code, making updates and bug fixes faster and easier
because you only need to change the important parts without breaking everything else.
8. Discuss the advantages of using abstract classes over interfaces.

Ans:

Here are the advantages of using abstract classes over interfaces, explained simply:

Abstract classes can have both complete methods and abstract methods, allowing for code reuse. Interfaces
usually only define method signatures.

Abstract classes can have variables to maintain state, while interfaces cannot have instance variables.

Abstract classes can have constructors for initializing fields, whereas interfaces cannot have constructors.

A class can only extend one abstract class, which helps avoid complexities associated with multiple inheritance.

11. Can a class be both abstract and final in Java? Justify your answer.

Ans: No, final class can not inherited and abstract class must be inherited , class can not be both at the same
time.

12. What is the significance of the default keyword in interface methods?

Ans: In interface default methods have the body, you can define the default method in interface,can used in
child class also you can override it.

16. . Why are abstract methods declared without a method body?

Ans:

Abstract method declare without the body to achieve the abstraction. Abstract methods implementation will
be in subclass that implement the abstract class.

19. Can an interface extend another interface in Java?

Ans:

Yes, an interface in Java can inherit another interface. This is done using the extends keyword. An interface can
extend one or more interfaces, allowing it to inherit abstract methods from those interfaces.

You might also like