Class 12 Informatics Practices Revision Notes Chapter 6 Inheritance

Revision Notes for CBSE Class 12 Informatics Practices Chapter 6 -Free PDF Download

Free PDF download of Best CBSE Class 12 Informatics Practices Chapter 6 Inheritance Quick Revision Notes & Short Key-notes prepared by our expert Informatics Practices teachers from latest edition of CBSE(NCERT) books.


Class 12 Informatics Practices Quick Revision notes Chapter 6 Inheritance

Superclass and Subclass
In Java, the superclass is the base class, whose properties and behaviours are inherited by the subclass(es) or the derived class(es). The subclass has to identify the superclass. To define a subclass, we use keyword extends.
The general syntax for defining the superclass and subclass is as follows:

access_specifier class superclass_name { : . } class subclass_name extends superclass_name { : . }

Forms of Inheritance
We know that, inheritance is an ability of Object Oriented Programming (OOP) to inherit the properties of an existing class to a subclass.
There are five forms of inheritance as follows:
1. Single Inheritance When a subclass inherits properties and behaviours from only one superclass or base class. It is known as single inheritance.
Class 12 Informatics Practices Notes Chapter 6 Inheritance 1
2. Multiple Inheritance When a subclass inherits from more than one superclasses (or base classes), it is known as multiple inheritance. Java does not support multiple inheritance by using the concept of classes but the multiple inheritance can be achieved by using the concept of interface.
Class 12 Informatics Practices Notes Chapter 6 Inheritance 2
3. Hierarchical Inheritance When more than one subclasses inherit from one superclass (or base class) inheritance.
Class 12 Informatics Practices Notes Chapter 6 Inheritance 3
4. Multilevel Inheritance When a subclass inherits from a superclass, which itself is inherited from another superclass, then such inheritance is known as multilevel inheritance.
Class 12 Informatics Practices Notes Chapter 6 Inheritance 4
5. Hybrid Inheritance It combines two or more forms of inheritance, e.g. when a subclass inherits from multiple superclass and all of its superclasses inherit from a single superclass, then such type of inheritance is known as hybrid inheritance.
Class 12 Informatics Practices Notes Chapter 6 Inheritance 5
Note In the combination, if one of the form is multiple inheritance then the inherited combination (hybrid inheritance) is not supported by Java through the classes concept but it can be supported or achieved by using the concept of interface.
Access Control for Inherited Members

  1. private The private members of a class are accessible only inside the class itself, in which it has been declared. It cannot be accessed outside the class.
  2. public The public members are accessible in all classes, subclasses of same package or any other package.
  3. protected The protected members of a class are accessible inside the class and in all its subclasses either in same package or in another package.
  4. default The default members, i.e. the members which has been defined without any access specifier are accessible inside the same class or outside the class but in same package.

Class 12 Informatics Practices Notes Chapter 6 Inheritance 6
A Simple Inheritance Program

class A { int a, b; void get(int p, int q) { a=p; b=q; } } class B extends A { void show() { System.out.println(“a = ”+a+“ and b = ”+b); } } class DemoMain { public static void main(String args[]) { B b=new B(); b.get(10,20);; } } Output a = 10 and b = 20

Subtleties of Inheritance in Java
Important points regarding the inheritance of Java class are as follows:

  1. In Java, every class has one direct superclass but can have multiple subclasses.
  2. Every class declared in Java is direct or indirect subclass of a class “Object” defined in java.lang package. It means even if we are declaring a class, which is not being inherited from other class, the new class has a superclass; the class Object.
  3. The inheritance is transitive relationship. It means every class inherits the behaviour and the state (i.e. the methods and variables) from the direct and indirect superclasses.

Method Overloading
The object oriented programming language permits you to reuse a method name for more than one method where arguments (number of arguments or type of arguments) must be different. Return type may or may not be different.

e.g. float arealfloat length, float breadth); float area(int length, int breadth); float arealfloat side);

Invoking a Overloaded Methods
When we try to invoke a particular method, it will automatically select a particular method after matching the arguments supplied with the invoking statement and the parameters of actual definition. In case an exact match is not found, then program will try with near match by automatic type casting in such a way that there is no loss of information, e.g. if an integer value is supplied for a float type argument, then it will be converted into float type variable (e.g. 5 will become 5.0).
We must remember if two methods are having same numbers of arguments in same order, then they cannot be overloaded, whether the name of variables are same or not.

e.g. void perimeter!int length,float breadth); void perimeter(int x,float y);

is not permissible.
Method Overriding
When the child class has the same method as declared in the parent class then it is known as method overriding. In other words, when a child class redefines a parent class method in order to give specific implementation then it is called as method overriding.

e.g. cl ass Vehicle f public void run() { System.out.print!n(“Vehicle is running”); } } class Car extends Vehicle { public void run() //method overriding. { System.out.println(“Car is running”); } }

Here, run( ) method of Vehicle class is overridden by the child class Car. If you want to refer to the overridden or hidden methods and variables of the superclass, you can use the super keyword. Here we must know that

  1. A subclass cannot override the methods that are declared as final in the superclass.
  2. The methods that are declared as abstract in the superclass must override the methods from the superclass or the subclass itself should be abstract.
e.g. class Vehicle { public void run() { System.out.println(“Vehicle is running”); } } class Car extends Vehicle { public void run() {; System.out.println(“Car is running”); } } public cl ass Demo { public static void main(String args[]) { Car obj = new Car();; } }

Output of the above program will be
Vehicle is running
Car is running.
Abstract Class
Abstract class is also known as incomplete class. We cannot create the object of an abstract class but we can make the reference variable of it. The concept behind the abstract class is to inherit from them and provide implementation to it. An abstract class must provide atleast one abstract method.

e.g. abstract class Vehicle //abstract class { public abstract void run(); //abstract method } class Car extends Vehicle { public void run() { System.out.printlnC"Car is running”); } } class Bus extends Vehicle { public void run() { System.out.printlnC"Bus is running”); } }

Abstract Methods
Abstract methods are those methods, in which there is no method statements. We can also say, abstract method as incomplete method. The method statements are provided by the subclass, when they inherit the abstract class containing the abstract method.

e.g. public abstract void accept();

The abstract methods of superclass need overriding in each subclass. It means we have to write their definition in the subclass.
In Java language, the interface defines a protocol of behaviour. The aim of interface is to specify the common behaviour of objects among derived classes. To achieve this aim, an interface declares a set of abstract methods. In whichever class this interface is implemented, the methods defined in the interface are also implemented to make the class exhibit certain behaviour.
To define an interface, we have to use following syntax:

interface interface_name { variable declaration; : . abstract public method declaration; }

Inside the body of the interface, we declare the following things:

  1. The abstract methods, i.e. the methods with empty body. These methods are to be declared with modifier public.
  2. The variables declared inside the interface bydefault constant, i.e. it can not be modified. Since, the Java language does not support multiple inheritance. So, interface ties the elements of different classes together.

Implementation of Interfaces
The class can implement an interface using the implements keyword. If we want a class to implement more than one interfaces then the names of different interfaces must be given in a comma separated list of interface names.
The general syntax for implementation of interfaces is as follows:

class class_name implements interfaced, interface_2,... { : . }

The class in which the interface(es) are implemented adheres to the protocol defined by the interface and has to implement all the methods declared in the interface. An interface can be extended to other interfaces too. There is no restriction on the number of extensions. It means an interface can extend to any number of interfaces.