[Go to site: main page, start]

0% found this document useful (0 votes)
5 views5 pages

Java Inheritance and Exception Handling Guide

Code of java on topic inheritance

Uploaded by

dighevaishnavi06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Java Inheritance and Exception Handling Guide

Code of java on topic inheritance

Uploaded by

dighevaishnavi06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. What is inheritance in Java?

It allows one class to acquire properties and methods of another.

2. What is a superclass?
The class whose features are inherited.

3. What is a subclass?
The class that inherits from another class.

4. Relationship between superclass and subclass?


Subclass extends superclass and reuses its members.

5. Syntax for inheritance?


class Subclass extends Superclass { }

6. Benefits of inheritance?
Reusability, reduced code, easier maintenance.

7. Protected members?
Members accessible in same package or subclasses.

8. Accessing protected members?


Through inheritance or same package.

9. Types of inheritance in Java?


Single, multilevel, hierarchical.

10. Why no multiple inheritance?


To avoid ambiguity (diamond problem).

11. Hierarchical inheritance?


One superclass with multiple subclasses.

12. Constructors in inheritance?


Superclass constructor runs before subclass.

13. Calling superclass constructor?


Using super().

14. Purpose of super keyword?


Access superclass methods/variables/constructors.

15. Can constructors be inherited?


No.

16. What is Object class?


Root class of all classes.
17. Why Object is superclass?
All classes implicitly extend it.

18. Methods of Object class?


toString(), equals(), hashCode(), clone().

19. What is method overriding?


Subclass provides new implementation of superclass method.

20. Overriding vs overloading?


Overriding = runtime; overloading = compile-time.

21. What is polymorphism?


Same method shows different behaviors.

22. Types of polymorphism?


Compile-time and runtime.

23. Compile-time polymorphism?


Method overloading.

24. Runtime polymorphism?


Method overriding via dynamic binding.

25. What is an abstract class?


A class containing abstract methods.

26. Can we create objects of abstract class?


No.

27. What is an abstract method?


A method without a body.

28. Declaring abstract method?


abstract void methodName();

29. Purpose of final keyword?


To restrict inheritance, overriding, or modification.

30. Final class?


Class that cannot be inherited.

31. Final method?


Method that cannot be overridden.

32. Can final method be overridden?


No.
33. What is dynamic binding?
Method call resolved at runtime.

34. Runtime polymorphism in Java?


Through method overriding + dynamic binding.

35. What is an interface?


Collection of abstract methods and constants.

36. Define interface syntax?


interface MyInterface { }

37. Interface vs abstract class?


Interface has full abstraction; abstract class may not.

38. Can interface have variables?


Yes, but they are public static final.

39. Can class implement multiple interfaces?


Yes.

40. Purpose of interfaces?


Achieve abstraction and multiple inheritance.

41. What is exception handling?


Managing runtime errors.

42. What is an exception?


An event disrupting program flow.

43. Types of exceptions?


Checked, unchecked, errors.

44. Checked vs unchecked?


Checked at compile time; unchecked at runtime.

45. try block?


Wraps risky code.

46. catch block?


Handles exceptions.

47. If no exception in try?


catch is skipped.

48. Multiple catch clause?


Multiple exception handlers after try.
49. Multiple catch blocks allowed?
Yes.

50. Nested try?


try block inside another try.

51. throw keyword?


Manually throw an exception.

52. throws keyword?


Declare exceptions a method may throw.

53. throw vs throws?


throw → actual throwing; throws → declaration.

54. finally block?


Executes always, cleanup code.

55. Does finally always execute?


Yes, except on [Link]().

56. Built■in exceptions?


NullPointerException, ArithmeticException, IOException.

57. Base class of exceptions?


[Link].

58. User■defined exception?


Create class extending Exception.

59. Can exception be re■thrown?


Yes, using throw.

60. If exception not caught?


Program terminates.

61. What is multithreading?


Executing multiple threads simultaneously.

62. What is a thread?


Smallest unit of execution.

63. Java thread model?


Multithreaded, shared memory model.

64. Main thread?


First thread started by JVM.
65. Creating a thread?
Extend Thread or implement Runnable.

66. Two ways to create thread?


Extending Thread, implementing Runnable.

67. Difference Thread vs Runnable?


Runnable better for multiple inheritance.

68. Starting a thread?


Call start().

69. run() method?


Defines thread’s task.

70. Can we call run() directly?


Yes, but it won’t start a new thread.

71. start() method?


Launches new thread and calls run().

72. Creating multiple threads?


Create multiple Thread/Runnable objects.

73. Thread synchronization?


Controlling shared resource access.

74. sleep() method?


Pauses thread temporarily.

75. join() method?


Waits for a thread to finish.

76. isAlive() method?


Checks if thread is still running.

77. Thread priority?


Thread execution preference.

78. Set priority?


[Link]().

79. Multiple threads sharing object?


May cause race conditions.

80. Daemon threads?


Background service threads.

You might also like