class CarLoan { float rate = 10.10f; } class HomeLoan extends CarLoan { float rate = 12.12f; void showRates() { System.out.println(“Carloan Rate = “ + super.rate); //10.10 System.out.println(“Home loan Rate = “ + rate); // 12.12 } } class Ldemo { public static void main(String args[]) { HomeLoan HL = new HomeLoan(); HL.showRates(); } }
class B { void fun() { System.out.println(“method of Base class”); } } class D extends B { void fun() { System.out.println(“method of Derived Class”); } }If you create object of class D and call fun() method,fun() of class D will be called.Consider the following code:
class D extends B { void fun() { System.out.println(“method of Derived class”); super.fun(); // Calling fun() of class B } }
class Base { Base() //1 {System.out.println(“Base class Constructor”); } } class Derived extends Base { Derived() //2 { System.out.println(“Derived Class Constructor”); } } class CIDemo { public static void main(String args[]) { Derived d = new Derived(); } }
Output: Base Class Constructor Derived Class Constructor
class Data { float PI; Data() { PI = 3.14f; } } class Circle extends Data { float Ar; Circle() { Ar = PI * 10 * 10; System.out.println(“Area of Circle: “ + Ar); } } class CDemo { public static void main(String args[]) { Circle c = new Circle(); } }
Output: Area of Circle : 314
class Account { String accNo; Account() //default constructor { } Account(String Ac) // parameterized constructor { accNo = Ac; } } class SavingAccount extends Account { float bal; SavingAccount() //default constructor { } SavingAccount(float b, String acc) //parameterized constructor { super(acc); //calling parameterized const. using super bal = b; } void show() { System.out.println(“AccountNumber : ” + accNo); System.out.println(“Balance : “ + bal); } } class PCDemo { public static void main(String args[]) { SavingAccount sa = new SavingAccount(7000, “SA786”); sa.show(); } }
Output: Account Number : SA786 Balance: 7000