Professional Documents
Culture Documents
unit 1 oops
unit 1 oops
PART-2
JVM, JRE, Java Environmnent.
Classloader
Memory area
allocated by
JVM
PC Native
Class Heap Stack method
area register stack
Native Java
Execution Native
method
engine interface Libraries
Fig. 1.41.
example
L myapp
4. Compile your code
i Compile your Java source files using the javac compiler.
When compiling, make sure your current working directorvis
the directory that contains the root ofyour package structure
PART- 12
Packages : Defining Package, CLASSPATH Setting for
Packages, Making JAR Files for Library Packages.
A. Package:
1 In Java, a package is a way to organize related classes, interfaces,
and sub-packages.
2 It provides a mechanism for grouping related types together, making
it easier to manage and maintain a large codebase.
3. A package is essentially a directory that contains a collection of
Java files.
B. Defining a package : Here's the process of defining a package in a
Java program :
Object Oriented Programming witlh Java 1-37 F (CS/IT-Sem-4)
Answer
A. Polymorphism :
1. Polymorphism refers to the ability of different objects to respond to
the same method call in a way that is appropriate for their specific
types.
In Java, polymorphism allows objects of different classes to be
2.
treated as objects of a common superclass.
B. Types of polymorphism in Java : There are two main types of
polymorphism in Java:
1. Compile-time polymorphism (static binding)
Also known as method overloading.
i. Occurs when multiple methods have the same name but different
parameter lists within the same class or between a subclass and its
superclass.
i. The Java compiler determines which methodto call based on the
number and types of arguménts passed during compile time.
iv. The decision on which nethod to executeis made at compile time,
and it's often referred to, as early binding.
V. Example :
class Calculator {
int add(int a, intb ) )
double add(double a, double b) {... )
PART- 10
Encapsulation, Polymorphism.
Answer
Answer
Following isan xample of method overriding in Java:
class Animal
public void makeSound() {
System.out.println("Somne generic sound");
9. Multilevel inheritance :
Introduction
from another
i. In multilevel inheritance, a subclass derives
creating achain of inheritance. subclass,
turn, extends class "A."
A class "C" extends class "B," which, in
4. Hierarchical inheritance :
inherit from a singla
i. In hierarchical inheritance, multiple subclasses
superclass.
multiple classes shars
This creates a branching structure where
common features.
PART-9
Overriding, Overloading.
Answer
Inheritance in Java :
A. properties and
Inheritance allows a new class (subclass) to inherit
behaviors (fieldsand methods) from an existing class (superclass).
1.
superclass, inheriting
2. In Java, you can create a subclass that extends a existing ones.
its attributes and adding new ones or modifying
3. Inheritance in
Java is achieved using the 'extends' keyword.
Inheritance is important in
B. Importance of inheritance in 00P:
OOP for following reasons :
1. Code reusability : Inheritance promotes code reuse.
hierarchy of
2 Hierarchy creation :Inheritance enables youto create a
classes, representing an "is-a" relationship,
Polymorphism : Inheritance is a key factor in achieving polymorphism
in 0OP.
from
4 Method overriding : Subclasses canoverride methods inherited class.
the superclass. This allows you to customize the behavior of a
5. Abstraction : Inheritance promotes the concept of abstraction.
6. Efficiency : Inheritance can leadto more efficient code, as it eliminates
the need to duplicate codeshared by multiple classes.
Que 1.32. Explain types of inheritance in Java.
Answer
Following are the common types of inheritance in Java:
1. Single inheritance:
i. Single inheritance involves a subclass inheriting from a single
superclass.
ii. In Java, all classes implicitly inherit from the 'Object' class, which
serves as the root of the class hierarchy.
ii. Therefore, Java supports single inheritance.
2 Multiple inheritance :
i. Java does not support multiple inheritance of classes, which means
a class cannot directly inherit from more than one class.
ii. However, multiple inheritance can be achieved through interfaces.
ii. A class can implement multiple interfaces, effectively inheriting
method signatures from multiple sources.
1-16 F (CS/TT-Sem-4)
Object Oriented Programming witlh Java
publie int getMyValue() {
return myValue;
4 Copy constructor :
1.
A copy constructor is used to create a new object by copying the
attributes of an existing object.
ii.
This conmstructor typically takes an object of the samne class as a
parameter and initializes the new object with the values of the
existing object.
Example:
public class Point
private int x;
private int y;
I/Copy constructor
public Point(Point other) (
this.x = other.x;
this.y = other y;
Answyer
1. A methodis a block of code which only runs when it is called.
2, Methods are used to perform certain actions, and they are also known
as functions.
3.
A method must be declared within aclass. It is defined with the name of
the method, followed by parentheses ().
Example : Create a method inside Main:
public class Main{
static void myMethod() {
Il code tobe executed
2 Parameterized constructor :
i Aparameterized constructor is defined with one or more parameters
ii. It allows you to provide initial values when creating objects,
ii. Parameterized constructors are useful whenyou want to set specific
g
values for an object's attributes e
during objectr
creation. ie
Example:
public class Person
private String name;
private int age;
,Parameterized constructor
,»blicPerson(String name, int age)[
this.name = name;
this.age = age;
ii. Met.hods: A class contains methods that define the behavior of objects
created from that class. Methods are functions that can perform actions
and manipulate the data stored in the fields. Methods are defined within
the class and can take parameters and return ýalues. For example:
public class Calculator {
public int add(int a, int b) {
return a+ b;