









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










template or blueprint from which objects are created.
is also called a derived class, extended class, or child class.
inherits the features. It is also called a base class or a parent class.
facilitates you to reuse the felds and methods of the existing class when you create a new class. You can use the same felds and methods already defned in the previous class.
class Subclass-name extends Superclass-name { //methods and felds
Java Inheritance Example
class Employee{ float salary= 40000 ; } class Programmer extends Employee{ int bonus= 10000 ;
Single Inheritance Example
File: TestInheritance.java class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class TestInheritance{ public static void main(String args[]){ Dog d= new Dog(); d.bark(); d.eat(); }}
barking... eating... Multilevel Inheritance Example
File: TestInheritance2.java class Animal{ void eat(){System.out.println("eating...");} }
void meow(){System.out.println("meowing...");} } class TestInheritance3{ public static void main(String args[]){ Cat c= new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error }}
meowing... eating... Q) Why multiple inheritance is not supported in java?
class A{ void msg(){System.out.println("Hello");} } class B{ void msg(){System.out.println("Welcome");} }
class C extends A,B{//suppose if it were public static void main(String args[]){ C obj= new C(); obj.msg();//Now which msg() method would be invoked? } } Compile Time Error
class Animal{ String color="white"; } class Dog extends Animal{ String color="black"; void printColor(){
class TestSuper2{ public static void main(String args[]){ Dog d= new Dog(); d.work(); }}
eating... barking...
3) super is used to invoke parent class constructor.
class Animal{ Animal(){System.out.println("animal is created");} } class Dog extends Animal{ Dog(){ super (); System.out.println("dog is created"); } } class TestSuper3{ public static void main(String args[]){ Dog d= new Dog();
animal is created dog is created
Output:Compile Time Error 3) Java final class
Example of final class final class Bike{} class Honda1 extends Bike{ void run(){System.out.println("running safely with 100kmph");} public static void main(String args[]){ Honda1 honda= new Honda1(); honda.run(); } } Output:Compile Time Erro Q) Is final method inherited?
class Bike{ final void run(){System.out.println("running...");} } class Honda2 extends Bike{ public static void main(String args[]){ new Honda2().run(); } } Output:running...
Q) What is blank or uninitialized final variable?
Example of blank final variable class Student{ int id; String name; final String PAN_CARD_NUMBER; ... } Que) Can we initialize blank final variable?
class Bike10{ final int speedlimit;//blank fnal variable Bike10(){ speedlimit= 70 ; System.out.println(speedlimit); } public static void main(String args[]){ new Bike10(); } } Output: 70