Prepare a input form which will take 2 input for range of values. After
getting input from the user, find sum of all primes from the given range
using a button named Generate-Sum. After getting the sum check whether the sum is even or not. All the process will be done by a
class followed by Inheritance in Java. You have to display the output in
a label at Form.
Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts
Wednesday, October 28, 2015
Inheritance in Java Programming
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.
In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability.
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.
Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
Why use inheritance in java
- For Method Overriding (so runtime polymorphism can be achieved).
- For Code Reusability.
Syntax of Java Inheritance
- class Subclass-name extends Superclass-name
- {
- //methods and fields
- }
In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.
Understanding the simple example of inheritance
As displayed in the above figure, Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee.It means that Programmer is a type of Employee.
- class Employee{
- float salary=40000;
- }
- class Programmer extends Employee{
- int bonus=10000;
- public static void main(String args[]){
- Programmer p=new Programmer();
- System.out.println("Programmer salary is:"+p.salary);
- System.out.println("Bonus of Programmer is:"+p.bonus);
- }
- }
Programmer salary is:40000.0 Bonus of programmer is:10000
In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability.
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.
Note: Multiple inheritance is not supported in java through class.
When a class extends multiple classes i.e. known as multiple inheritance. For Example:Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.
- class A{
- void msg(){System.out.println("Hello");}
- }
- class B{
- void msg(){System.out.println("Welcome");}
- }
- class C extends A,B{//suppose if it were
- Public Static void main(String args[]){
- C obj=new C();
- obj.msg();//Now which msg() method would be invoked?
- }
- }
Compile Time Error
Friday, April 5, 2013
OOP in JAVA Quiz Questions
OBJECT ORIENTED PROGRAMMING WITH JAVA
Question: Can a
private method of a super-class be declared within a subclass?
Question: Why Java does not support
multiple inheritance ?
Question:What is the difference between
final, finally and finalize?
Question: Where and how can you use a
private constructor?
Question: In System.out.println(),what is System,out and println,pls explain?
Question: What is meant by "Abstract Interface"?
Question: Can you make an instance of an
abstract class? For example - java.util.Calender is an abstract class with a
method getInstance() which returns an instance of the Calender class.
Question: What is the output of x<y?
a:b = p*q when x=1,y=2,p=3,q=4?
Question:
What is the difference between Swing and AWT components?
Question: Why Java does not support
pointers?
Question: What is a platform?
Question:
What is the main difference between Java platform and other platforms?
Question: What is the Java Virtual
Machine?
Question: What is the Java API?
Question: What is the package?
Question: What is native code?
Question:
Is Java code slower than native code?
Question: What is the serialization?
Question: How to make a class or a bean
serializable?
Question:
Which containers use a border layout as their default layout?
Question:
What is synchronization and why is it important?
Question: What are synchronized methods
and synchronized statements?
Question:
What is synchronization and why is it important?
Question: What are synchronized methods
and synchronized statements?
Question: What are three ways in which a
thread can enter the waiting state?
Question: Can a lock be acquired on a
class?
Question:
What's new with the stop(), suspend() and resume() methods in JDK 1.2?
Question: What is the preferred size of a
component?
Question:
What method is used to specify a container's layout?
Question: Which containers use a Flow-layout as their default layout?
Question: What is thread?
Question: What is multi threading?
Question: How does multi threading take
place on a computer with a single CPU?
Question: How to create multithread in a
program?
Question:
Can Java object be locked down for exclusive use by a given thread?
Question: Can each Java object keep track
of all the threads that want to exclusively access to it?
Question: What state does a thread enter
when it terminates its processing?
Question: What invokes a thread's run()
method?
Question: What is the purpose of the
wait(), notify(), and notifyAll() methods?
Question: What are the high-level thread
states?
Question: What is the Collections API?
Question: What is the List interface?
Question:
How does Java handle integer overflows and under-flows?
Question:
What is the Vector class?
Question: If a method is declared as
protected, where may the method be accessed?
Question:
What is an Iterator interface?
Question: How many bits are used to
represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Question:
What is the difference between yielding and sleeping?
Question:
Is sizeof a keyword?
Question: What are wrapped classes?
Question: Does garbage collection
guarantee that a program will not run out of memory?
Question: What is the difference between
preemptive scheduling and time slicing?
Question: Name Component subclasses that
support painting.
Question: What is a native method?
Question:
How can you write a loop indefinitely?
Question: Can an anonymous class be declared as
implementing an interface and extending a class?
Question: What is the purpose of
finalization?
Question: Which class is the super-class
for every class.
Question: What is the difference between
the Boolean & operator and the && operator?
Question: What is the GregorianCalendar
class?
Question: What is the SimpleTimeZone
class?
Question: Which Container method is used
to cause a container to be laid out and redisplayed?
Question: What is the Properties class?
Question: What is the purpose of the
Runtime class?
Question:
What is the purpose of the System class?
Question: What is the purpose of the
finally clause of a try- catch-finally statement?
Question: What is the Locale class?
Question: What must a class do to
implement an interface?
Question: What is an abstract method?
Question: What is a static method?
Question: What is a protected method?
Question: What is the difference between a
static and a non -static inner class?
Question:
What is an object's lock and which object's have locks?
Question: When can an object reference be
cast to an interface reference?
Question:
What is the difference between a Window and a Frame?
Question: What do heavy weight components
mean?
Question: Which package has light weight
components?
Question: What are peerless components?
Question: What is the difference between
the Font and FontMetrics classes?
Question: What happens when a thread
cannot acquire a lock on an object?
Question: What is the difference between
the Reader/Writer class hierarchy and the InputStream/OutputStream class
hierarchy?
Question: What classes of exceptions may
be caught by a catch clause?
Question:What is the difference between
throw and throws keywords?
Question: If a
class is declared without any access modifiers, where may the class be
accessed?
Question: What is the Map interface?
Question: Does a class inherit the
constructors of its superclass?
Question: Name primitive Java types.
Question: Which class should you use to
obtain design information about an object?
Question: How can a GUI component handle
its own events?
Question: How are the elements of a
GridBagLayout organized?
Question: What advantage do Java's layout
managers provide over traditional windowing systems?
Question: What are the problems faced by Java programmers who don't use layout managers?
Question: What is the difference between
static and non -static variables?
Question: What is the difference between
the paint() and repaint() methods?
Question:
What is the purpose of the File class?
Question:
What restrictions are placed on method overloading?
Question:
What restrictions are placed on method overriding?
Question: What is casting?
Question: Name Container classes.
Question: What class allows you to read
objects directly from a stream?
Question:
How are this() and super() used with constructors?
Question: How is it possible for two
String objects with identical values not to be equal under the == operator?
Question: What an I/O filter?
Question: What is the Set interface?
Question:
What is the List interface?
Question: What is the purpose of the
enableEvents() method?
Question: What is the difference between
the File and RandomAccessFile classes?
Question: What interface must an object
implement before it can be written to a stream as an object?
Question: What is the ResourceBundle
class?
Question: What is the difference
between a Scrollbar and a ScrollPane? Answer: A Scrollbar is a Component, but
not a Container. A ScrollPane is a Container. A ScrollPane handles its own
events and performs its own scrolling.
Question: What is a Java package and how
is it used?
Question: What are the Object and Class
classes used for?
Question: What is Serialization and
deserialization?
Question: what is tunnelling?
Question: Does the code in finally block
get executed if there is an exception and a return statement in a catch block?
Question:
How you restrict a user to cut and paste from the html page?
Question:
Is Java a super set of JavaScript?
Question: What is a Container in a GUI?
Question: How the object oriented approach
helps us keep complexity of software development under control?
Question: What is polymorphism?
Question: What is design by contract?
Question: What are use cases?
Question: What is the difference between
interface and abstract class?
Question: What is an Iterator interface?
Question: What is the difference between
the >> and >>> operators?
Question: Which method of the Component class
is used to set the position and size of a component?
Question: How many bits are used to
represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Question: What is the difference between
yielding and sleeping?
Question: Which java.util classes and
interfaces support event handling?
Question: Is sizeof a keyword?
Question: What are wrapped classes?
Question: Does garbage collection
guarantee that a program will not run out of memory?
Question: What restrictions are placed on
the location of a package statement within a source code file?
Question: Can an object's finalize()
method be invoked while it is reachable?
Question: What is the immediate superclass
of the Applet class?
Question: What is the difference between
preemptive scheduling and time slicing?
Question: Name three Component subclasses
that support painting.
Question: What value does readLine()
return when it has reached the end of a file?
Question: What is the immediate superclass
of the Dialog class?
Question: What is clipping?
Question: What is a native method?
Question: Can a for statement loop
indefinitely?
Question: What are order of precedence and
associativity, and how are they used?
Question:
When a thread blocks on I/O, what state does it enter?
Question: To
what value is a variable of the String type automatically initialized?
Question: What is the catch or declare
rule for method declarations?
Question: What is the difference between a
MenuItem and a CheckboxMenuItem?
Question: What is a task's priority and
how is it used in scheduling?
Question: What class is the top of the AWT
event hierarchy?
Question:
When a thread is created and started, what is its initial state?
Question: Can an anonymous class be
declared as implementing an interface and extending a class?
Question: What is the range of the short
type?
Question:
What is the range of the char type?
Question: In which package are most of the
AWT events that support the event-delegation model defined?
Question: What is the immediate superclass
of Menu?
Question: What is the purpose of
finalization?
Question: Which class is the immediate
superclass of the MenuComponent class.
Question: What invokes a thread's run()
method?
Question: What is the difference between
the Boolean & operator and the && operator?
Question: Name three subclasses of the
Component class.
Question: What is the GregorianCalendar
class?
Question: Which Container method is used
to cause a container to be laid out and redisplayed?
Question: What is the purpose of the
Runtime class?
Question: How many times may an object's
finalize() method be invoked by the garbage collector?
Question: What is the purpose of the
finally clause of a try-catch-finally statement?
Question: What is the argument type of a
program's main() method?
Question:
Which Java operator is right associative?
Question: What is the Locale class?
Question: Can a double value be cast to a
byte?
Question: What is the difference between a
break statement and a continue statement?
Question: What must a class do to
implement an interface?
Question: What method is invoked to cause
an object to begin executing as a separate thread?
Question: Name two subclasses of the
TextComponent class.
Question: What is the advantage of the
event-delegation model over the earlier event-inheritance model?
Question: Which containers may have a
MenuBar?
Question: How are commas used in the
intialization and iterationparts of a for statement?
Question: What is the purpose of the
wait(), notify(), and notifyAll() methods?
Question: What is an abstract method?
Question: How are Java source code files
named?
Question: What is the relationship between
the Canvas class and the Graphics class?
Question: What are the high-level thread
states?
Question: What value does read() return
when it has reached the end of a file?
Question:
Can a Byte object be cast to a double value?
Question: What is the difference between a
static and a non-static inner class?
Question: What is the difference between
the String and StringBuffer classes
Question: If a variable is declared as
private, where may the variable be accessed?
Question: What is an object's lock and
which object's have locks?
Question: What is the Dictionary class?
Question: How are the elements of a
BorderLayout organized?
Question: What is the % operator?
Question: When can an object reference be
cast to an interface reference?
Question: What is the difference between a
Window and a Frame?
Question: Which class is extended by all
other classes?
Question: Can an object be garbage
collected while it is still reachable?
Question: Is the ternary operator written
x : y ? z or x ? y : z ?
Question: What is the difference between
the Font and FontMetrics classes?
Question: How is rounding performed under
integer division?
Question: What happens when a thread
cannot acquire a lock on an object?
Question: What is the difference between
the Reader/Writer class hierarchy and the InputStream/OutputStream class
hierarchy?
Question: What classes of exceptions may
be caught by a catch clause?
Question: If a class is declared without
any access modifiers, where may the class be accessed?
Question: What is the SimpleTimeZone
class?
Question: What is the Map interface?
Question: Does a class inherit the
constructors of its superclass?
Question: For which statements does it
make sense to use a label?
Question: What is the purpose of the
System class?
Question: Which
TextComponent method is used to set a TextComponent to the read-only state?
Question: How are the elements of a
CardLayout organized?
Question: Is &&= a valid Java
operator
Question: Name the eight primitive Java
types.
Question: Which class should you use to
obtain design information about an object?
Question: What is the relationship between
clipping and repainting?
Question: Is "abc" a primitive
value?
Question: What is the relationship between
an event-listener interface and an event-adapter class?
Question: What restrictions are placed on
the values of each case of a switch statement?
Question: What modifiers may be used with
an interface declaration?
Question:
Is a class a subclass of itself?
Question: What is the highest-level event
class of the event-delegation model?
Question: What event results from the
clicking of a button?
Question: How can a GUI component handle
its own events?
Question: What is the difference between a
while statement and a dostatement?
Question: How are the elements of a
GridBagLayout organized?
Question: What advantage do Java's layout
managers provide over traditional windowing systems?
Question: What is the Collection
interface?
Question: What modifiers can be used with
a local inner class?
Question: What is the difference between
static and non-static variables?
Question: What is the difference between
the paint() and repaint() methods?
Question: What is the purpose of the File
class?
Question: Can an exception be rethrown?
Question: Which Math method is used to
calculate the absolute value of a number?
Question: How does multithreading take
place on a computer with a single CPU?
Question: When does the compiler supply a
default constructor for a class?
Question: When is the finally clause of a
try-catch-finally statement executed?
Question: Which class is the immediate
superclass of the Container class?
Question: If a method is declared as
protected, where may the method be accessed?
Question: How can the Checkbox class be
used to create a radio button?.
Question: Which non-Unicode letter
characters may be used as the first character of an identifier?
Question: What restrictions are placed on
method overloading?
Question: What happens when you invoke a
thread's interrupt method while it is sleeping or waiting?
Question: What is casting?
Question: What is the return type of a
program's main() method?
Question: Name four Container classes.
Question: What is the difference between a
Choice and a List?
Question: What class of exceptions are
generated by the Java run-time system?
Question: What class allows you to read
objects directly from a stream?
Question: What is the difference between a
field variable and a local variable?
Question: Under what conditions is an
object's finalize() method invoked by the garbage collector?
Question: How are this() and super() used
with constructors?
Question: What is the relationship between
a method's throws clause and the exceptions that can be thrown during the
method's execution?
Question: What is the difference between
the JDK 1.02 event model and the event-delegation model introduced with JDK
1.1?
Question: How is it possible for two
String objects with identical values not to be equal under the == operator?
Question: Why are the methods of the Math
class static?
Question: What Checkbox method allows you
to tell if a Checkbox is checked?
Question:
What state is a thread in when it is executing?
Question: What are the legal operands of
the instance of operator?
Question: How are the elements of a
GridLayout organized?
Question: What an I/O filter?
Question: If an object is garbage
collected, can it become reachable again?
Question: What is the Set interface?
Question: What classes of exceptions may
be thrown by a throw statement?
Question: What are E and PI?
Question: Are true and false keywords?
Question: What is a void return type?
Question: What is the purpose of the
enableEvents() method?
Question: What is the difference between
the File and RandomAccessFile classes?
Question: What happens when you add a
double value to a String?
Question: What is your platform's default
character encoding?
Question:
Which package is always imported by default?
Question: What interface must an object
implement before it can be written to a stream as an object?
Question: How are this and super used?
Question: What is the purpose of garbage
collection?
Question: What is a compilation unit?
Question: What interface is extended by
AWT event listeners?
Question: What restrictions are placed on
method overriding?
Question: How can a dead thread be
restarted?
Question: What happens if an exception is
not caught?
Question: What is a layout manager?
Question: Which arithmetic operations can result in the throwing of an ArithmeticException?
Question: What are three ways in which a
thread can enter the waiting state?
Question: Can an abstract class be final?
Question: What is the ResourceBundle
class?
Question: What happens if a
try-catch-finally statement does not have a catch clause to handle an exception
that is thrown within the body of the try statement?
Question: What is numeric promotion?
Question: What is the difference between a
Scrollbar and a ScrollPane?
Question: What is the difference between a
public and a non-public class?
Question: To what value is a variable of
the boolean type automatically initialized?
Question: Can try statements be nested?
Question: What is the difference between
the prefix and postfix forms of the ++ operator?
Question: What is the purpose of a
statement block?
Question: What is a Java package and how
is it used?
Question: What modifiers may be used with
a top-level class?
Question: What are the Object and Class
classes used for?
Question: How does a try statement
determine which catch clause should be used to handle an exception?
Question: Can an unreachable object become
reachable again?
Question: When is an object subject to
garbage collection?
Question: What method must be implemented
by all threads?
Question: What methods are used to get and
set the text label displayed by a Button object?
Question: Which Component subclass is used
for drawing and painting?
Question: What are synchronized methods
and synchronized statements?
Question: What are the two basic ways in
which classes that can be run as threads may be defined?
Question: What are the problems faced by
Java programmers who don't use layout managers?
Question: What is the difference between
an if statement and a switch statement?
Question: What happens when you add a
double value to a String?
Question: What is the List interface?
Question: Why do we need public static
void main(String args[]) method in
Question: What is the difference between
an Interface and an Abstract class
Question: Explain serialization
Question: What are the rules of
serialization
Question: What is difference between error
and exception
Question: What do you mean by object
oreiented programming
Question:
What are 4 pillars of object oreinted programming
Question: Difference between procedural
and object oreinted language
Question: What is the difference between
constructor and method
Question: What is the difference between
parameters and arguments
Question: What is reflection in java
Subscribe to:
Posts (Atom)