InterviewSolution
| 1. |
What Do You Understand By Encapsulation, Inheritance, And Abstraction In Java ? |
|
Answer» The term encapsulation, Inheritance, and abstraction are the features of object-oriented programming. These features provide the facility to deal with the objects. Encapsulation: Encapsulation is one of the PRIMARY features of OOPs concept. The process of binding the data and methods in a single unit is called as encapsulation. With the help of encapsulation, we can hide the data members from other classes, and it is only ACCESSIBLE by its current class. There are two ways to achieve encapsulation:
Inheritance: - Inheritance in Java is a process by which on class can inherit the properties (methods and FIELDS) of another class. It increases the reusability of methods and fields. We can use inherited methods and ALSO can add new methods. Inheritance shows parent-child relationship also known as IS-A relationship. There are the following terms USED in inheritance: Child class/Subclass: Child class or sub-class is one who inherits the other class. Superclass/ Parent class: Superclass or Parent class is on which got inherited by another class. It is also known as base class. For inheriting one class, into another class we use extends keyword in Java. Syntax for inheritance: 1. class A extends B 2. { 3. //methods and fields 4. } Where class A is child class, and class B is Parent class. Abstraction: Abstraction is a one of the important Feature of OOPs concept, which helps a user to show only the functionality not the implementation details of the object. In Java languages we can achieve abstraction in two ways: By using the abstract class (0 to 100%): A class which is declared using 'abstract' keyword is considered as an abstract class. An abstract class can have an abstract method as well as non-abstract methods. Syntax: 1.abstract class A{ } By using interface (100%): An interface in Java is the same as a class in java, it can have abstract methods and variables. But it cannot have a method body. we can declare it by using the interface keyword Syntax: 1.Interface Interface_Name{ 2.//Methods } The term encapsulation, Inheritance, and abstraction are the features of object-oriented programming. These features provide the facility to deal with the objects. Encapsulation: Encapsulation is one of the primary features of OOPs concept. The process of binding the data and methods in a single unit is called as encapsulation. With the help of encapsulation, we can hide the data members from other classes, and it is only accessible by its current class. There are two ways to achieve encapsulation: Inheritance: - Inheritance in Java is a process by which on class can inherit the properties (methods and fields) of another class. It increases the reusability of methods and fields. We can use inherited methods and also can add new methods. Inheritance shows parent-child relationship also known as IS-A relationship. There are the following terms used in inheritance: Child class/Subclass: Child class or sub-class is one who inherits the other class. Superclass/ Parent class: Superclass or Parent class is on which got inherited by another class. It is also known as base class. For inheriting one class, into another class we use extends keyword in Java. Syntax for inheritance: 1. class A extends B 2. { 3. //methods and fields 4. } Where class A is child class, and class B is Parent class. Abstraction: Abstraction is a one of the important Feature of OOPs concept, which helps a user to show only the functionality not the implementation details of the object. In Java languages we can achieve abstraction in two ways: By using the abstract class (0 to 100%): A class which is declared using 'abstract' keyword is considered as an abstract class. An abstract class can have an abstract method as well as non-abstract methods. Syntax: 1.abstract class A{ } By using interface (100%): An interface in Java is the same as a class in java, it can have abstract methods and variables. But it cannot have a method body. we can declare it by using the interface keyword Syntax: 1.Interface Interface_Name{ 2.//Methods } |
|