OOPS JAVA M1 Solved Ktunotes - in
OOPS JAVA M1 Solved Ktunotes - in
*note: Since there are many rooms, fire detectors, alarms, sprinklers, corresponding data needs
to store in array variables.
Member Variables
Member Functions
+name : String
+rn : int
+rank : int
- fees : long
+ displayDetails(n:String, r: int) : void
+displayRank( ra:int): void
• Inside the box First row represents class name
• Second row represents member variables listed as
access_specifier var_name : datatype
• Third row represents member functions
access_specifier function_name(para1:type, para2:type…): returntype
• Access specifier + (public), -(private), *(protected)
LOGIN
SEARCH
MANAGE
USER
PLAY
MANAGE
SONGS
STOP
CHANGE
}
}
Java Applets
• Applets are programs that can be embedded in an HTML website.
• It runs inside a web browser and requires an extension of JDK to be installed in the browser
for execution.
• Doesn’t have full access to local storage, can’t read and write without permission.
• It doesn’t require a main() method for its execution.
• A java applet program consist of following functions: init(), start() and paint().
import java.applet.Applet;
import java.awt.Graphics;
// importing necessary packages
public class SampleApplet extends Applet //class must extend Applet class in java
{
public void paint(Graphics g) // paint function includes all graphics related operations
{
// printing the message using drawString() method
// other parameters are of the position
g.drawString("Java Applet", 250, 250);
}
}
16. Explain Java Buzzwords. (Properties of Java Programming Language)
Answer
• Object oriented - Java provides the basic object technology of C++ with some enhancements
and some deletions.
• Architecture neutral - Java source code is compiled into architecture-independent object code.
The object code is interpreted by a Java Virtual Machine (JVM) on the target architecture.
• Portable - Java implements additional portability standards. For example, ints are always 32-
bit, 2's-complemented integers. User interfaces are built through an abstract window system
that is readily implemented in Solaris and other operating environments.
• Distributed - Java contains extensive TCP/IP networking facilities. Library routines support
protocols such as HyperText Transfer Protocol (HTTP) and file transfer protocol (FTP).
• Robust - Both the Java compiler and the Java interpreter provide extensive error checking. Java
manages all dynamic memory, checks array bounds, and other exceptions.
• Secure - Features of C and C++ that often result in illegal memory accesses are not in the Java
language. The interpreter also applies several tests to the compiled code to check for illegal
code. After these tests, the compiled code causes no operand stack over- or underflows,
performs no illegal data conversions, performs only legal object field accesses, and all opcode
parameter types are verified as legal.
• High performance - Compilation of programs to an architecture independent machine-like
language, results in a small efficient interpreter of Java programs. The Java environment also
compiles the Java bytecode into native machine code at runtime.
• Multithreaded - Multithreading is built into the Java language. It can improve interactive
performance by allowing operations, such as loading an image, to be performed while
continuing to process user actions.
• Dynamic - Java does not link invoked modules until runtime.
• Simple - Java is similar to C++, but with most of the more complex features of C and C++
removed.
- Member variables associated with Student class are: name, rollno, marks, rank, fees…
- Methods associated with Student class are: displayDetails(), payFees() etc…
2. Objects: Instance of a class. All objects belonging to a class share common variables and methods.
However the values of variables differ across objects.
Eg: In a Student class, objects will be different students say stud1, stud2, stud3…..All these objects
have name, rollno, marks, rank and fees that may contain different values. In addition all these objects
can perform the operations displayDetails(), payFees() defined in class Student.
3. Encapsulation: Wrapping up of data and methods into a single entity. The only way to access data
is through methods.
DATA
METHODS
Teachers Employee
int tid displayDetails()
String name, designation
void displayDetsils()
In the above example Employee class is trying to access the Teachers data through the member method
displayDetails() of class Teachers. It is possible because the data and methods of classes are wrapped
as a single entity by means of encapsulation.
4. Inheritance: It is a process by which object of one class inherit the properties of objects of another
class. It is the capability to define a new class in terms of an existing class. The existing class or Parent
class is known as a base class or super class and the new class or child class is known as derived class
or sub class. Inheritance supports code reusability.
Example : Consider a parent class named Vehicle and its members as given in the figure.
• Vehicle is a class having member variables String reg_no, model, color, int
fuel_capacity and member functions void vehicleDetails(), void fillfuel() and void
calculateSpeed().
5. Abstraction: Abstraction is the process of hiding certain details and showing only essential
information to the user. Abstraction can be achieved with either abstract classes or interfaces. Its main
goal is to handle complexity by hiding unnecessary details from the user. That enables the user to
implement more complex logic on top of the provided abstraction without understanding or even
thinking about all the hidden complexity.
When the object data is not visible to the outer world, it creates
data abstraction. If needed, access to the Objects’ data is
provided through some methods. In the example member
variables of the class are private members limiting the access
of data within the class.
• Dynamic Polymorphism
• Static Polymorphism
In Dynamic polymorphism, the object creation is done at run-time the form of method which should
be executed (the method in the object) can be only decided at run-time. Method Overriding is a type of
Dynamic polymorphism or Run Time Polymorphism. In method overriding, when functions of same
signature is used in both parent and child, the function of superclass will be overridden by the subclass
method.
In Static Polymorphism, method overloading happens. Method overloading is having more than one
method with the same method name but with different arguments (return type may or may not be same).
Here when calling the methods compiler compiler choose which method to call depending on the
parameters passed when calling. This happens at compile-time
A Single-line comment starts and ends in the same line. To write a single-line comment, we can use
the // symbol. For example,
class Main
{
public static void main(String[] args)
{
// prints "Hello, World!"
System.out.println("Hello, World!");
}
}
Multi Line Comments: When we want to write comments in multiple lines, we can use the multi-
line comment. To write multi-line comments, we can use the /*....*/ symbol. For example,
*/
class HelloWorld {
System.out.println("Hello, World!");
This type of comment is also known as Traditional Comment. In this type of comment, the Java
compiler ignores everything from /* to */.
Documentation Comments: This type of comment is used to produce an HTML file that documents
your program. The documentation comment begins with a /** and ends with a */ .
• Comments: Comments can be used to explain Java code, and to make it more readable. It can
also be used to prevent execution when testing alternative code.
*/
class HelloWorld {
System.out.println("Hello, World!");
• Separators: In Java, there are a few characters that are used as separators. The most commonly
used separator in Java is the semicolon.
// program body
…………….
eg: ClassName.java
….
- public : It has to be public so that java runtime can execute this method. If you make any
method non-public then it’s not allowed to be executed by any program, there are some access
restrictions applied.
- static : java runtime starts, there is no object of the class present. That’s why the main method
has to be static so that JVM can load the class into memory and call the main method. If the
main method won’t be static, JVM would not be able to call it because there is no object of the
class is present.
- void : Java programming mandates that every method provide the return type. Java main
method doesn’t return anything, that’s why it’s return type is void. This has been done to keep
things simple because once the main method is finished executing, java program terminates. So
there is no point in returning anything, there is nothing that can be done for the returned object
by JVM.
- main : This is the name of java main method. It’s fixed and when we start a java program, it
looks for the main method.
- String args[] : Java main method accepts a single argument of type String array. This is also
called as java command line arguments.
Example: