Sunday 22 October 2017

CBSE Class 12 - Informatics Practices - Java Object Oriented Programming-2 - Inheritance (#cbseNotes)

Java - InheritanceClass 12 - Informatics Practices

CBSE Class 12 - Informatics Practices - Java Object Oriented Programming-2 - Inheritance (#cbseNotes)

Q1: What do you mean by the term 'inheritance'?

Answer: In OOPs, Inheritance is the process of creating new class (Derived Class or sub-classes) from an existing class (Base Class or Super class). A Sub-class inherits all the properties (data members) and behaviour (method members) of the parent class (Super-class).

The level of Inheritance may be extended i.e. A Sub-class may have their own sub-classes.

In Real-life most of the things exhibit Inheritance relationship.

Q2: What is the importance of 'inheriatnce'?

Answer: Inheritance is important for the following reasons:
Modelling of Real-world
Reusability of codes
Transitive nature of Inheritance

Modelling of Real-world: By Inheritance, we can model the real-world inheritance relationships in easy way.

Reusability of codes: Inheritance allows the derivation of a new class from existing classes. We can add new features to derived class to make a new one with minimal efforts.

Transitive nature of Inheritance: If we modify the base class then the changes automatically inherit into derived classes. Since inheritance is transitive in nature. This offers faster and efficient way to maintain the large

Q3: What is the difference between superclass and subclass?

Answer: A superclass is a class that is inherited whereas subclass is a class that does the inheritance. It means a sub-class derives proerties and behavior of a super class.

Q4: Describe the different types of inheritance supported in Java OOPS?

CBSE Class 12 - Informatics Practices - Java Object Oriented Programming-2 - Inheritance (#cbseNotes)

Answer: In OOP approach, the Inheritance is many types:

Single Level Inheritance: When a sub-class inherits only one base-class.

Multi-level Inheritance: When a sub-class is derived from sub-class of another base-class.

Multiple Inheritance: When a sub-class derived from multiple base-classes.

Hierarchical Inheritance: When Multiple sub-classes are derived from a single base class.

Hybrid Inheritance: When a sub-class inherits from multiple base-classes and all its base-classes inherits one or more super-classes.

Q5: What is class inheritance vs interface inheritance?

1. class inheritance - create a new class as an extension of another class, primarily for the purpose of code reuse.

class derived-class-name extends base-class-name { .... }

2. interface inheritance - create a new class to implement the methods defined as part of an interface for the purpose of sub-typing.

public class class-name implements interface-name { .... }

Q6: Write a student class with following specifications:
(i)   Two private variables: first name, last name
(ii)  Constructor with two arguments.
(iii) Void Method print Data() to print first + last name.

public class Student
private String fname;
private String lname;
Student(String fname, String lname)
void printData()
public static void main(String[] args)
Student st=new Student("abc", "def");

Q7: What are access specifiers? Describe the different access sepecifiers supported in Java OOP.

Answer: Java provides some Access Specifiers, which can control the visibility/access of the Parent (base) Class from Child (sub) class.

The members (Data and Methods) of the Parent Class may be defined as Private, Public, Protected, and Default, which may limit its accessibility or visibility in the derived (child) classes.

Private : Members are accessible only inside their own class and no where else.

Protected: Members are accessible in own class, all classes within package and all Sub-classes in different package.

Public: Members are accessible everywhere in all the classes.

Package (default): Members without any specifier assumed package level scope i.e. accessible to all classes inside the package only.

Q8: What is an abstract class? Give an example in Java.

Answer: An Abstract class simply represents a concept for its sub-classes. An Abstract class works as template for its sub-classes which contains only data members and method prototype i.e. methods without code (concept).

Abstract classes are normally used as base class in inheritance for which no object is required e.g. JOptionPane Class in Java is Abstract class because it requires no object. Whereas JTextField, JLabel classes etc. are called Concrete classes because they requires an object like ‘jTextField1’ for using them.

No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.