Data Abstraction: ================= Questions and Answers: ====================== 1) Why do we need the constructor in abstract class with no possibility to create the object for the abstract class? Ans: ==== for any class, the constructor can be used to initialize the data members. ==> even for the abstract class, to initialize the data members we need the constructor. So, here the child class of that abstract class can initialize the data members of the parent abstract class. 2) can we define a constructor for an abstract class? Ans: yes ==== 3) What is the use of constructor in abstract class? Ans: to initialize the data members in abstract class we can use "constructor". Here, the child class can consider to initialize the data members while invoking the abstract class constructor. 4) can we create an object for an abstract class? Ans: No 5) Can we create an object for subclass of an abstract class? Ans: yes 6) For Example, I have two abstract methods in abstract class, but the child class is override only one abstract method. what happened here? Ans: that child class of the given abstract class should be abstract class. package AIT.Java.IO.Pack; abstract class Parent{ abstract void m1(); abstract void m2(); } abstract class Child extends Parent{ @Override void m1() { System.out.println("Hi"); } } public class Abstract_1 { public static void main(String[] args) { // TODO Auto-generated method stub } } @Override void m1() { } } 7) can we create a class as abstract class without any abstract method? Ans: Yes. package AIT.Java.IO.Pack; abstract class Parent{ public void m1() { System.out.println("Hi"); } public void m2() { System.out.println("Welcome to Ashok IT."); } } public class Abstract_1 { public static void main(String[] args) { // Parent pobj = new Parent(); } } 8) Why we should create the abstract class without abstract method? Ans: 1) To restrict to create the object for any class, we should consider that class as "abstract class". 2) When we want instruct to any developer in a team that class is partially developed (not fully implemented) in this case also we should make that class as "Abstract class". package AIT.Java.IO.Pack; abstract class InsurancePolicy{ double basePremium; // constructor created. public InsurancePolicy(double basePremium) { this.basePremium = basePremium; } // concrete method public double calculatePremium() { return basePremium + calculateRiskFactor(); } // abstract method public abstract double calculateRiskFactor(); } class HealthPolicy extends InsurancePolicy{ int age; boolean isSmoker; public HealthPolicy(double basePremium, int age, boolean isSmoker) { super(basePremium); this.age = age; this.isSmoker = isSmoker; } @Override public double calculateRiskFactor() { double riskFactor = 0; if(age >= 50 && age < 60) { riskFactor = basePremium * 0.25; } else if(age >= 60) { riskFactor = basePremium * 0.40; } else { riskFactor = 0; } if(isSmoker) { riskFactor = basePremium * 0.15; } return riskFactor; } } class VehiclePolicy extends InsurancePolicy{ int mfgYear; public VehiclePolicy(double basePremium, int mfgYear) { super(basePremium); this.mfgYear = mfgYear; } @Override public double calculateRiskFactor() { if(mfgYear >= 2022) { return basePremium * 0.10; } else { return basePremium * 0.35; } } } public class AbstractionDemo { public static void main(String[] args) { // Creating the object for the health policy HealthPolicy hPolicy = new HealthPolicy(12999.0,55,true); double totalPremium = hPolicy.calculatePremium(); System.out.println("The Total Premium for Health Insurance = "+totalPremium); System.out.println("================================"); VehiclePolicy vPolicy = new VehiclePolicy(14987.0,2023); double premiumAmount = vPolicy.calculatePremium(); System.out.println("The Premium Amount on Vehicle Policy = "+premiumAmount); } } ========================================================== Q: can we define abstract final class? ====================================== Ans: No because abstract class must be inherited while recreating/overriding the abstract method in child class. But final classes never be inherited. abstract final class Parent{} ==> Incorrect ====================================================== Interfaces: =========== Why? ==== 1) to implement the multiple inheritance and hybrid inheritance we need to use "interfaces". 2) to implement abstraction also we need interface. What is interface? ================== Government rural development roads buildings @ contract /interface guidelines{ time material human power } tender call for contractors road construction building construction ======================================= I am Running a software company for my employees lunch and dinner to my employees: guidelines to contracts: guidelines{ 5-different types of curries rice tiffin's } contract body1 contract body2 ============== ============== lunch dinner