IP CLASS 12 PRACTICAL FILE NETBEANSFull description
A brief Study on Shia Laws of InheritanceFull description
hiFull description
Project Regarding Java MySQL ConnectivityFull description
Understanding IP Routing Concept Article covers the following CCNA/ICND1 Exam Topics: Under Operation of IP Data Networks: 1. Recognize the purpose and functions of various network devices su...
Some important distinguished topics for CBSE Class 12 Computer Science Viva Voce Questions
Class 12 Computer Science notes for all chapters with questions.
Full description
Solved Subjective Questions of CS615Full description
Full description
Delhi UniversityFull description
For Beginner level
CONCEPT OF INHERITANCE Type A: Very Short Short Answer Answer Questions Questions 1
Ans.
Fill Fill in the the bla blank nkss in in eac each h of of the the foll follow owin ing g sen sente tenc nces es:: (i) A met metho hod d tha thatt lac lacks ks a bod body y is is an an ___ _____ ____ ____ ____ ____ ____ ____ ___ _ met metho hod. d. (ii) (ii) An _____ ________ ______ ______ ____ _ is like like a clas classs except except that that it it contai contains ns only only insta instance nce meth method ods, s, no ins instan tance ce varia variable bles. s. (iii) Two ways ways for a class class to to inherit inherit something something in java java are are to to ______ __________ ______ __ a class or _____ ________ ___ are inherited inherited by the subclasses. (iv) (iv) An obj objec ectt can can refe referr to its itsel elff by usi using ng ___ _____ ____ ____ ___ _ keyw keywor ord. d. (v) A _____ ________ _____ __ meth method od is is one one that that does does diff differe erent nt thin things gs depen dependin ding g upon upon the the objec objectt that that invoke invokess it. it. (i) A method that lacks a body is an abstract method. (ii) An Interface is like a class except that it contains only instance methods, no instance variables. (iii (iii))
Two Two way wayss for for a clas classs to to inh inheri eritt som someth ethin ing g in in jav java a are are to to private a class or public are inherited by the subclasses.
2 Ans.
3
Ans.
(iv) (iv)
An obje objecct can can refe referr to to its itsel elff by by us using ing this keyword.
(v)
A overloaded method is one that does different things depending upon the object that invokes it.
Defi Define ne inhe inheri rita tanc nce. e. What What is the the inh inher erita itanc nce e mech mechan anis ism m in in Java Java? ? Inheritance is the capability capability of one class to inherit inherit properties from another class. class. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Inheritance is a compile-time mechanism in Java that allows you to e xtend a class (called the base class or superclass) with another class (called the derived class or subclass). In Java, inheritance inheritance is used for two purposes: 1. class inheritance - create a new class as an extension of another class, primarily for the purpose 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 subtyping.public public class class-name implements interface-name { .... } Inheritance is a way to (i) (i) Make Make gene genera rall cla classses ses in into more more speci pecifi ficc cla classses ses. (ii (ii) Pass Pass argu argume men nts to objec bjects ts of cla classses ses. (iii) (iii) Add Add feat featur ures es to to exis existi ting ng cla class sses es wit witho hout ut rew rewrit ritin ing g them them.. (iv) (iv) Impr Impro ove data data-h -hid idin ing g and and enca encaps psul ulat atio ion. n. Add features to existing classes without rewriting them.
Type B: Short Short Answer Question Questionss 1 Ans.
2 Ans.
Discu Discuss ss vari variou ouss reas reason onss that that sup suppo port rt the the conc concep eptt of inhe inherit ritan ance ce in in Obj Objec ectt Orie Orient nted ed Lang Langua uage ges. s. 1. Inheritance Inheritance is capable capable of expressing expressing the inheritance inheritance relationsh relationship ip of real-world real-world models. models. ‘Men’ inherit from from ‘Person’; ‘Women’ inherit from ‘Person’, etc. 2. Inheritance Inheritance facilitat facilitates es the code reusabil reusability. ity. Additional Additional features features can can be added to a class class by deriving deriving a class from it and then by adding new features t o it. Class ones written and tested need not be rewritten or redefined. 3. Inheritance Inheritance is capable capable of simulating simulating the transitive transitive nature nature of real-world’s real-world’s inheritance, inheritance, which which in turn saves on modification time and efforts, if required. How How does does the the vis visib ibil ilit ity y mode mode cont contro roll the the acce access ss of of mem membe bers rs in the the deri derive ved d clas class? s? Exp Expla lain in with with exa examp mple les. s. Visibility Visibility mode controls controls the access of member in derived derived class by using access specifier. specifier. Below table table shows different visibility modes. Specifier class subclass Package World private Y protected Y Y Y public Y Y Y Y default Y Y class BaseClass { public int x = 10; private int y = 10;
Page 1 of 3
protected int z = 10; int a = 10; //Implicit Default Access Modifier public int getX() { return x; } private int getY() { return y; } private void setY(int y) { this.y = y; } protected int getZ() { return z; } int getA() { return a; } } public class SubclassInSamePackage extends BaseClass { public static void main(String args[]) { BaseClass rr = new BaseClass(); rr.z = 0; SubclassInSamePackage subClassObj = new SubclassInSamePackage(); //Access Modifiers - Public System.out.println("Value of x is : " + subClassObj.x); //Access Modifiers – Private // If comment is removed error will come as trying to access //private member(s). /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ //Access Modifiers - Protected System.out.println("Value of z is : " + subClassObj.z); //Access Modifiers - Default System.out.println("Value of x is : " + subClassObj.a); } }
3
What is the difference between protected and private members? Type C: Long/Practical Answer Questions
1
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.
Ans.
public class Student { private String fname; private String lname; Student(String fname, String lname) { this.fname=fname; this.lname=lname; } void printData()
Write a Graduate class that inherits from Student class defined in question 24. Add Private variables, Stream, Degree It must have a constructor method. Method PrintGrade() to print Stream, Degree along with inherited first and last name.