What is difference between C++ Destructor and Java Finalize method ?
February 19, 2010
February 8, 2010
Detecting A cycle in Singly linked List
http://www.siafoo.net/algorithm/10
http://www.siafoo.net/algorithm/11
How do you determine if your singly-linked list has a cycle? In the late 1960s, Robert W. Floyd invented an algorithm that worked in linear (O(N)) time. It is also called Floyd’s cycle detection algorithm.
The easiest solution to the cycle detection problem is to run through the list, keeping track of which nodes you visit, and on each node check to see if it is the same as any of the previous nodes. It’s pretty obvious that this runs in quadratic (O(N^2)) time… not very efficient, and actually more complicated than this one.
The Tortoise and the Hare is possibly the most famous cycle detection algorithm, and is surprisingly straightforward. The Tortoise and the Hare are both pointers, and both start at the top of the list. For each iteration, the Tortoise takes one step and the Hare takes two. If there is a loop, the hare will go around that loop (possibly more than once) and eventually meet up with the turtle when the turtle gets into the loop. If there is no loop, the hare will get to the end of the list without meeting up with the turtle.
Why can’t you just let the hare go by itself? If there was a loop, it would just go forever; the turtle ensues you will only take n steps at most.
For a somewhat more efficient algorithm, check out Brent’s Cycle Detection Algorithm (The Teleporting Turtle).
How do you determine if your singly-linked list has a cycle? In 1980, Brent invented an algorithm that not only worked in linear time, but required less stepping than Floyd’s Tortoise and the Hare algorithm (however it is slightly more complex). Although stepping through a ‘regular’ linked list is computationally easy, these algorithms are also used for factorization and pseudorandom number generators, linked lists are implicit and finding the next member is computationally difficult.
Brent’s algorithm features a moving rabbit and a stationary, then teleporting, turtle. Both turtle and rabbit start at the top of the list. The rabbit takes one step per iteration. If it is then at the same position as the stationary turtle, there is obviously a loop. If it reaches the end of the list, there is no loop.
Of course, this by itself will take infinite time if there is a loop. So every once in a while, we teleport the turtle to the rabbit’s position, and let the rabbit continue moving. We start out waiting just 2 steps before teleportation, and we double that each time we move the turtle.
Why move the turtle at all? Well, the loop might not include the entire list; if a rabbit gets stuck in a loop further down, without the turtle, it will go forever. Why take twice as long each time? Eventually, the length of time between teleportations will become longer than the size of the loop, and the turtle will be there waiting for the rabbit when it gets back.
Note that like Floyd’s Tortoise and Hare algorithm, this one runs in O(N). However you’re doing less stepping than with Floyd’s (in fact the upper bound for steps is the number you would do with Floyd’s algorithm). According to Brent’s research, his algorithm is 24-36% faster on average for implicit linked list algorithms.
Algorithm for converting a string to an integer value.
Hints:
1. Check that each character is a numeric character. Take into consideration the base in which the conversion is sought.
2. Identify the character in ASCII and convert it to numeral ’1′ -> 1
3. Iterate over string and Multiply by the appropriate base and add it to temp to find the value of the number.
Also take care that number is -ve
January 26, 2010
Amazon-1-Advantages and disadvantages of recursions
1. Advantages and disadvantages of recursion
Show Answer
advantages
1. Small Manageable Code
2. Sometimes Recursion is only option
3. Code Reuse
Disadvantages:
1. Is mostly depth first- You may run out of memory
2. Is memory intensive
3. Stack Space is used prominently
November 17, 2009
| Q1) What is an inner class?
Ans) Inner class is a class defined inside other class and act like a member of the enclosing class.
|
| Q2) What are the different types of inner classes?
Ans) There are two main types of inner classes –
|
| Q3) What is static member class?
Ans) A static member class behaves much like an ordinary top-level class, except that it can access the static members of the class that contains it. The static nested class can be accessed as the other static members of the enclosing class without having an instance of the outer class. The static class can contain non-static and static members and methods.public class InnerClass {
static class StaticInner { static int i = 9; private void method() {} public void method1() {} static void method2() {} final void method3() {} } The static inner class can be accessed from Outer Class in the following manner: No outer class instance is required to instantiate the nested static class because the static class is a static member of the enclosing class. |
| Q4) What are non static inner classes?
Ans)
Non – static inner classes – classes associated with the object of the enclosing class. Member class – Classes declared outside a function (hence a “member”) and not declared “static”. The member class can be declared as public, private, protected, final and abstract. E.g.public class InnerClass {
Method local class – The inner class declared inside the method is called method local inner class. Method local inner class can only be declared as final or abstract. Method local class can only access global variables or method local variables if declared as final public class InnerClass { int i = 9; public void method1() { final int k = 6; Some examples: JButton btn = new JButton(); Anonymous class used with comparator List<Parent> l = new ArrayList<Parent>();
|
| Q5) Does a static nested class have access to the enclosing class’ non-static methods or instance variables?
Ans) No .
|
| Q6) What are the advantages of Inner classes?
Ans) The embedding of inner class into the outer class in the case when the inner class is to be used only by one class i.e. the outer class makes the package more streamlined. Nesting the inner class code where it is used (inside the outer class) makes the code more readable and maintainable. The inner class shares a special relationship with the outer class i.e. the inner class has access to all members of the outer class and still have its own type is the main advantages of Inner class. Advantage of inner class is that they can be hidden from the other classes in the same package and still have the access to all the members (private also) of the enclosing class. So the outer class members which are going to be used by the inner class can be made private and the inner class members can be hidden from the classes in the same package. This increases the level of encapsulation. If a class A is written requires another class B for its own use, there are two ways to do this. One way is to write a separate class B or to write an inner class B inside class A. Advantage of writing the inner class B in the class A is you can avoid having a separate class. Inner classes are best used in the event handling mechanism and to implement the helper classes. The advantage of using inner class for event handling mechanism is that the use of if/else to select the component to be handled can be avoided. If inner classes are used each component gets its own event handler and each event handler implicitly knows the component it is working for. e.g. Button btn1 = new Button(“Submit”);
} ); |
| Q7)What are disadvantages of using inner classes?
Ans)
1. Using inner class increases the total number of classes being used by the application. For all the classes created by JVM and loaded in the memory, jvm has to perform some tasks like creating the object of type class. Jvm may have to perform some routine tasks for these extra classes created which may result slower performance if the application is using more number of inner classes. 2. Inner classes get limited support of ide/tools as compared to the top level classes, so working with the inner classes is sometimes annoying for the developer. |
| Q8) What are different types of anonymous classes?
Ans 1) Plain old anonymous class type one–
e.g. class superClass{ void doSomething() { System.out.println(“Doing something in the Super class”); } } class hasAnonymous{ interface Eatable{ interface Vehicle { |
| Q9) If you compile a file containing inner class how many .class files are created and what are all of them accessible in usual way?
Ans) If a inner class enclosed with an outer class is compiled then one .class file for each inner class an a .class file for the outer class is created. e.g.
class EnclosingOuter { class Inner{ } } If you compile the above code with command % javac EnclosingOuter.java Two files EnclosingOuter.class EnclosingOuter$Inner.class will be created. Though a separate inner class file is generated, the inner class file is not accessible in the usual way like, % java EnclosingOuter$Inner |
| Q10) How to access the inner class from code within the outer class?
Ans) The inner class is instantiated only through the outer class instance.
class EnclosingOuter { private int noInnerClass = 1; public void getNoOfInnerClasses(){ Inner in = new Inner(); System.out.println(“No Of Inner classes is : “+ in.getNoOfClassesFromOuter()); } class Inner{ public int getNoOfClassesFromOuter(){ return noInnerClass; } } |
| Q11) How to create an inner class instance from outside the outer class instance code?
Ans) To create an instance of the inner class you must have the instance of its enclosing class.
e.g. class EnclosingOuter { class Inner{ } } To create the instance of inner class from class other than the enclosing class. 1) class OtherThanOuter{ EnclosingOuter out = new EnclosingOuter(); EnclosingOuter.Inner in = out.new Inner(); } 2) class OtherThanOuter{ |
| Q12) How to refer to the outer this i.e. outer class’s current instance from inside the inner class?
Ans) The outer this reference i.e. the outer class’ current instance’ reference can be refered using ‘OuterClassName.this’. E.g
class EnclosingOuter { class Inner{ System.out.println(“Inner class reference is “ + this); // inner class instance
|
| Q13) Which modifiers can be applied to the inner class?
Ans) Following are modifiers that can be applied to the inner:
public private abstract final protected strictfp static – turns the inner class into static nested class. |
| Q14) Can the method local inner class object access method’s local variables?
Ans) No, a method local inner class object can not access the method local variable.
Reason: The local variables are not guaranteed to live as long as the local inner class object. The method local variable live on stack and exist only till the method lives, their scope is limited only code inside the method they are declared in. But the local inner class object created within the method lives on heap and it may exist even after the method ends if in case the reference of this local inner class is passed into some other code and is stored in an instance variable. So we can not be sure that the local variables will live till the method local inner class object lives, therefore the method local inner class object can not access the method local variable. To access the method local variables, the variable has to be declared as final. |
| Q15) Can a method local inner class access the local final variables?Why?
Ans) Yes. Because the final variables are stored on heap and they live as long as the method local inner class object may live.
|
| Q16) Which modifiers can be applied to the method local inner class?
Ans) Only abstract or final keyword isallowed.
|
| Q17) Can a local class declared inside a static method have access to the instance members of the outer class?
Ans) No. There is no this reference available in the static method .The static method class can not have access to any members of the outer class other than static members.
|
| Q18) Can a method which is not in the definition of the superclass of an anonymous class be invoked on that anonymous class reference?
Ans) No. Compilation will fail.As the reference variable type of the anonymous class will be of superclass which will not know of any method defined inside the anonymous class the compilation will fail.
e.g. class SuperClass{ void doSomething() { System.out.println(“In the Super class”); } } class hasAnonymous{ SuperClass anon = new SuperClass(){ void doSomething() { System.out.println(“In the Anonymous class”); } void doStuff() { System.out.println(“An Anonymous class method not present in superClass”); } }; public void doIt(){ |
| Q19) Can an anonymous class define method of its own?
Ans) Yes. But there will be no way by which the methods defined in the anonymous class which are not present in its superclass be invoked. As only those methods which are defined in the suprclass which the anonymous class extends be invoked defining the methods in the anonymous class will be of no use
|
| Q20) Can an anonymous class implement multiple interfaces directly?
Ans) No. An anonymous class can implement only one interface. If the anonymous class is extending a class then it becomes the implementer of all the interfaces implemented by its superclass automatically.
|
| Q21) Can an anonymous class implement an interface and also extend a class at the same time? |
Exceptions
| Q1) What is an Exception?
Ans) The exception is said to be thrown whenever an exceptional event occurs in java which signals that something is not correct with the code written and may give unexpected result. An exceptional event is a occurrence of condition which alters the normal program flow. Exceptional handler is the code that does something about the exception. |
| Q2) Exceptions are defined in which java package?
Ans)All the exceptions are subclasses of java.lang.Exception |
| Q3) How are the exceptions handled in java?
Ans)When an exception occurs the execution of the program is transferred to an appropriate exception handler.The try-catch-finally block is used to handle the exception. |
| Q4) Explain the exception hierarchy in java.
Ans) The hierarchy is as follows:
Throwable is a parent class off all Exception classes. They are two types of Exceptions: Checked exceptions and UncheckedExceptions. Both type of exceptions extends Exception class. |
| Q5) What is Runtime Exception or unchecked exception?
Ans) Runtime exceptions represent problems that are the result of a programming problem. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small. Runtime exceptions need not be explicitly caught in try catch block as it can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program’s clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can). The solution to rectify is to correct the programming logic where the exception has occurred or provide a check. |
| Q6) What is checked exception?
Ans) Checked exception are the exceptions which forces the programmer to catch them explicitly in try-catch block. It is a subClass of Exception. Example: IOException. |
| Q7) What is difference between Error and Exception?
Ans) An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime.Though error can be caught in catch block but the execution of application will come to a halt and is not recoverable. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.) |
| Q8) What is difference between ClassNotFoundException and NoClassDefFoundError?
Ans) A ClassNotFoundException is thrown when the reported class is not found by the ClassLoader in the CLASSPATH. It could also mean that the class in question is trying to be loaded from another class which was loaded in a parent classloader and hence the class from the child classloader is not visible. Consider if NoClassDefFoundError occurs which is something like java.lang.NoClassDefFoundError src/com/TestClass does not mean that the TestClass class is not in the CLASSPATH. It means that the class TestClass was found by the ClassLoader however when trying to load the class, it ran into an error reading the class definition. This typically happens when the class in question has static blocks or members which use a Class that’s not found by the ClassLoader. So to find the culprit, view the source of the class in question (TestClass in this case) and look for code using static blocks or static members. |
| Q9) What is throw keyword?
Ans) Throw keyword is used to throw the exception manually. It is mainly used when the program fails to satisfy the given condition and it wants to warn the application.The exception thrown should be subclass of Throwable.
public void parent(){
try{ child(); }catch(MyCustomException e){ } } public void child{ |
| Q10) What is use of throws keyword?
Ans) If the function is not capable of handling the exception then it can ask the calling method to handle it by simply putting the throws clause at the function declaration.
public void parent(){
try{ child(); }catch(MyCustomException e){ } } public void child throws MyCustomException{ |
| Q11) What are the possible combination to write try, catch finally block?
Ans)
The catch blocks must always follow the try block. If there are more than one catch blocks they all must follow each other without any block in between. The finally block must follow the catch block if one is present or if the catch block is absent the finally block must follow the try block. |
| Q12) How to create custom Exception?
Ans) To create you own exception extend the Exception class or any of its subclasses. e.g. 1 class New1Exception extends Exception { } // this will create Checked Exception 2 class NewException extends IOExcpetion { } // this will create Checked exception 3 class NewException extends NullPonterExcpetion { } // this will create UnChecked exception |
| Q13) When to make a custom checked Exception or custom unchecked Exception?
Ans) If an application can reasonably be expected to recover from an exception, make it a checked exception. If an application cannot do anything to recover from the exception, make it an unchecked exception. |
| Q14)What is StackOverflowError?
Ans) The StackOverFlowError is an Error Object thorwn by the Runtime System when it Encounters that your application/code has ran out of the memory. It may occur in case of recursive methods or a large amount of data is fetched from the server and stored in some object. This error is generated by JVM. e.g. void swap(){
|
| Q15) Why did the designers decide to force a method to specify all uncaught checked exceptions that can be thrown within its scope?
Ans) Any Exception that can be thrown by a method is part of the method’s public programming interface. Those who call a method must know about the exceptions that a method can throw so that they can decide what to do about them. These exceptions are as much a part of that method’s programming interface as its parameters and return value. |
| Q16) Once the control switches to the catch block does it return back to the try block to execute the balance code?
Ans) No. Once the control jumps to the catch block it never returns to the try block but it goes to finally block(if present). |
| Q17) Where is the clean up code like release of resources is put in try-catch-finally block and why?
Ans) The code is put in a finally block because irrespective of try or catch block execution the control will flow to finally block. Typically finally block contains release of connections, closing of result set etc. |
| Q18) Is it valid to have a try block without catch or finally?
Ans) NO. This will result in a compilation error. The try block must be followed by a catch or a finally block. It is legal to omit the either catch or the finally block but not both. e.g. The following code is illegal.
|
| Q19) Is it valid to place some code in between try the catch/finally block that follows it?
Ans) No. There should not be any line of code present between the try and the catch/finally block. e.g. The following code is wrong.
|
| Q20) What happens if the exception is never caught and throws down the method stack?
Ans) If the exception is not caught by any of the method in the method’s stack till you get to the main() method, the main method throws that exception and the JVM halts its execution. |
| Q21) How do you get the descriptive information about the Exception occurred during the program execution?
Ans) All the exceptions inherit a method printStackTrace() from the Throwable class. This method prints the stack trace from where the exception occurred. It prints the most recently entered method first and continues down, printing the name of each method as it works its way down the call stack from the top. |
| Q22) Can you catch more than one exceptions in a single catch block?
Ans)Yes. If the exception class specified in the catch clause has subclasses, any exception object that is a subclass of the specified Exception class will be caught by that single catch block. E.g..
The catch block above will catch IOException and all its subclasses e.g. FileNotFoundException etc. |
| Q23)Why is not considered as a good practice to write a single catchall handler to catch all the exceptions?
Ans) You can write a single catch block to handle all the exceptions thrown during the program execution as follows :
If you use the Superclass Exception in the catch block then you will not get the valuable information about each of the exception thrown during the execution, though you can find out the class of the exception occurred. Also it will reduce the readability of the code as the programmer will not understand what is the exact reason for putting the try-catch block. |
| Q24) What is exception matching?
Ans) Exception matching is the process by which the the jvm finds out the matching catch block for the exception thrown from the list of catch blocks. When an exception is thrown, Java will try to find by looking at the available catch clauses in the top down manner. If it doesn’t find one, it will search for a handler for a supertype of the exception. If it does not find a catch clause that matches a supertype for the exception, then the exception is propagated down the call stack. This process is called exception matching. |
| Q25) What happens if the handlers for the most specific exceptions is placed above the more general exceptions handler?
Ans) Compilation fails. The catch block for handling the most specific exceptions must always be placed above the catch block written to handle the more general exceptions. e.g. The code below will not compile.
The code below will compile successfully :-
|
| Q26) Does the order of the catch blocks matter if the Exceptions caught by them are not subtype or supertype of each other
Ans) No. If the exceptions are siblings in the Exception class’s hierarchy i.e. If one Exception class is not a subtype or supertype of the other, then the order in which their handlers(catch clauses) are placed does not matter. |
| Q27) What happens if a method does not throw an checked Exception directly but calls a method that does? What does ‘Ducking’ the exception mean?
Ans) If a method does not throw an checked Exception directly but calls a method that throws an exception then the calling method must handle the throw exception or declare the exception in its throws clause. If the calling method does not handle and declares the exception, the exceptions is passed to the next method in the method stack. This is called as ducking the exception down the method stack. e.g. The code below will not compile as the getCar() method has not declared the CarNotFoundException which is thrown by the getColor () method.
Fix for the above code is
|
| Q28) Is an empty catch block legal?
Ans) Yes you can leave the catch block without writing any actual code to handle the exception caught. e.g. The code below is legal but not appropriate, as in this case you will nt get any information about the exception thrown.
|
| Q29)Can a catch block throw the exception caught by itself? |
KeyWords
| Q1) What are different types of access modifiers in Java?
Ans) There are four different types of modifiers:
|
|||||||||||||||
| Q2) What is the use of final keyword?
Ans) The final keyword can be assigned to
If a final is assigned to a variable, the variable behaves as a constant. It means that the value of variable once set cannot be changed.
If a final is assigned to a method then it cannot be overridden in its child class. class Parent {
} class Child extends Parent{
} If a class is made as final, then no other class can extend it and make it as parent class. E.g. String Class. Final objects are instantiated only once. i.e final Map map = new HashMap(); map.put(“key”,”value”); map = new HashMap(); // error |
|||||||||||||||
| Q3) What is use of synchronized keyword?
Ans) This keyword is used to prevent concurrency. Synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object and thus prevents race condition. E.g. public void synchronized method(){} synchronized (this){ // synchronized keyword on block of code } |
|||||||||||||||
| Q4) What is volatile keyword?
Ans) In general each thread has its own copy of variable, such that one thread is not concerned with the value of same variable in the other thread. But sometime this may not be the case. Consider a scenario in which the count variable is holding the number of times a method is called for a given class irrespective of any thread calling, in this case irrespective of thread access the count has to be increased. In this case the count variable is declared as volatile. The copy of volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory. The volatile variable also have performance issues. |
|||||||||||||||
| Q5) What is a transient variable?
Ans) If some of the properties of a class are not required to be serialized then the varaibles are marked as transient. When an object is deserialized the transient variables retains the default value depending on the type of variable declared and hence lost its original value. |
|||||||||||||||
| Q6) What is a strictfp modifier?
Ans) Strictfp is used with variable only . It is used to restrict floating point calculations ( fp ) to ensure portability ( platform Independent ). When this modifier is specified, the JVM adheres to the Java specifications ( IEEE-754 floating-point specification ) and returns the consistent value independent of the platform. That is, if you want the answers from your code (which uses floating point values) to be consistent in all platforms, then you need to specify the strictfp modifier. |
|||||||||||||||
| Q7) What is a static variable?
Ans) Static keyword can be used with the variables and methods but not with the class but there are static class. Anything declared as static is related to class and not objects.
Static variable : Multiples objects of a class shares the same instance of a static variable.Consider the example: public class Counter{ private static int count=0; private int nonStaticcount=0; public void incrementCounter(){ count++; nonStaticcount++; } public int getCount(){ return count; } public int getNonStaticcount(){ return nonStaticcount; }
public static void main(String args[]){ Counter countObj1 = new Counter(); Counter countObj2 = new Counter(); countObj1.incrementCounter(); countObj1.incrementCounter(); System.out.println(“Static count for Obj1: “+countObj1.getCount()); System.out.println(“NonStatic count for Obj1: “+countObj1.getNonStaticcount()); System.out.println(“Static count for Obj2: “+countObj2.getCount()) System.out.println(“NonStatic count for Obj2: “+countObj2.getNonStaticcount()) } Output Static count for Obj1: 2 NonStatic count for Obj1: 2 Static count for Obj2: 2 NonStatic count for Obj2: 0 In the above program obj1 and obj2 share the same instance of static variable count hence if the value is incremented by one object , the incremented value will be reflected across the other objects |
|||||||||||||||
| Q8) What is a static method?
Ans)A method defined as static is called static method. A static method can be accessed without creating the objects. Just by using the Class name the method can be accessed.
Static method can only access static variables and not local or global non-static variables. For eg: public class Test{
public static void printMe(){ System.out.println(“Hello World”); } } public class MainClass{ public static void main(String args[]){ Test.printMe() } Also static method can call only static methods and not non static methods. But non-static methods can call static mehtods. |
|||||||||||||||
| Q9) Why static methods cannot access non static variables or methods?
Ans) A static method cannot access non static variables or methods because static methods doesnt need the object to be accessed. So if a static method has non static variables or non static methods which has instantiated variables they will no be intialized since the object is not created and this could result in an error.
|
|||||||||||||||
| Q10) What is static class?
Ans) A class cannot be declared static. But a class can be said a static class if all the variables and methods of the class are static and the constructor is private. Making the constructor private will prevent the class to be instantiated. So the only possibility to access is using Class name only
|
|||||||||||||||
| Q9) What is throw keyword?
Ans) Throw keyword is used to throw the exception manually. It is mainly used when the program fails to satisfy the given condition and it wants to warn the application.The exception thrown should be subclass of Throwable.
public void parent(){
try{ child(); }catch(MyCustomException e){ } } public void child{ |
|||||||||||||||
| Q10) What is use of throws keyword? |
OOPS
| Q1) What is polymorphism?
Ans) Polymorphism gives us the ultimate flexibility in extensibility. The abiltiy to define more than pne function with the same name is called Polymorphism. In java,c++ there are two type of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding). When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time. Overriding occurs when a class method has the same name and signature as a method in parent class. Overloading occurs when several methods have same names with
int add(int a,int b)
float add(float a,int b) float add(int a ,float b) void add(float a) int add(int a) void add(int a) //error conflict with the method int add(int a)
Class BookDetails{
String title; String publisher; float price;
} Example: Overriding class BookDetails{
class ScienceBook{ setBook(String title){} //overriding
} |
| Q2) What is inheritance?
Ans) Inheritance is the property which allows a Child class to inherit some properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.
public class Parent{ public String parentName; public class Child extends Parent{ public String childName; public void printMyName(){ } In above example the child has inherit its family name from the parent class just by inheriting the class. |
| Q3) What is multiple inheritance and does java support?
Ans) If a child class inherits the property from multiple classes is known as multiple inheritance.
Java does not allow to extend multiple classes but to overcome this problem it allows to implement multiple Interfaces. |
| Q4) What is abstraction?
Ans) Abstraction is way of converting real world objects in terms of class. For example creating a class Vehicle and injecting properties into it. E.gpublic class Vehicle {
public String colour; |
| Q5) What is data hiding?
Ans) The way of providing restriction to the access of property of a class is called data hiding. In java this is achieved by using access modifiers: – public, private, protected and default.
|
| Q6) What is encapsulation? |
