saving . . . saved Abstract class and encapsulation has been deleted. Abstract class and encapsulation has been hidden .
Abstract class and encapsulation
Title
Question
Madam/Sir, 
How the program shown there with classes Person, Employee an Student were following the rule of Abstraction because somehow we are able to access the Abstract Class Person with creating an object of it using a constructor calling of another class say Employee in main method. It means Person p1=new Employee("John", Ee125, 26....);.

Also without extending the class in which main method is there from class Person how we're we able to call it using constructor of other class say Employee because neither Person nor Employee is extended, even if it would have been the constructor of class Person then also how could it be possible to do it without extending it to the class where main method is located?

Java Abstract-Classes 08-09 min 30-40 sec 27-06-22, 5:15 p.m. sc256356

Answers:

Part-1:
The demonstrated example has "Person" as abstract class, while "Employee" and "Student"  are the concrete classes, which are implemented by extending "Person" class. 
The statement Person p1=new Employee("John", Ee125, 26....), creates the object of the Employee class and it is referenced with variable of type "Person" class. Thus variable p1 refers to actual object of the employee class (concrete class). It is not possible to create the object of the abstract class, but reference variable of the abstract class type can refer to the object of the child classes of the abstract class
In the demonstrated example, abstract class has declared the behaviour of the class (i.e. showDetails() method), though actual definition of the abstract method is implemented in the concrete class. Thus, Person class is abstracting the actual behaviour.

Part-2: Separate concrete classes are created for Employee and Student by extending the Person class. In the main method we can create objects of the classes as demonstrated. It is not mandatory to create main method in the extended class only, it can be written in separate class also.
01-07-22, 11:27 a.m. pankajap@cse.iitb.ac.in


Log-in to answer to this question.