Unit 2 Java
Unit 2 Java
Advantages:
Code reusability:
Inheritance allows programmers to reuse code that has already been written and tested. This can save time and effort,
and it can also help to reduce errors.
Reduced code duplication:
Inheritance can help to reduce code duplication by allowing programmers to inherit common properties and methods
from a base class. This can make code more organized and easier to maintain.
Improved maintainability:
Inheritance can make code more maintainable by allowing programmers to make changes to a base class without having
to make changes to all of the derived classes.
Extensibility:
Inheritance allows programmers to extend the functionality of existing classes by creating new derived classes. This can
be useful for adding new features or for adapting existing classes to new requirements.
Example:
// Superclass
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
System.out.println();
Explanation:
The Animal class is the superclass with a method sound().
The Dog class is a subclass of Animal, inheriting the sound() method and adding a new method bark().
The Puppy class is a subclass of Dog, inheriting both the sound() and bark() methods and adding a new method
play().
In the main method, we demonstrate both single and multilevel inheritance by creating objects of Dog and
Puppy classes and invoking methods from their superclass and subclass.
Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
Output:
Dog is eating
Animal is eating
In this example, the Dog class overrides the Animal class's eat() method. However, the Dog class still wants to call the
Animal class's eat() method, so it uses the super keyword.
Example:
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
Example:
// Superclass
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclass 1
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
// Subclass 2
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
In this example:
The Animal class defines a method sound().
The Dog and Cat classes are subclasses of Animal, and they override the sound() method with their own
implementations.
In the main method, we create objects of type Animal but assign them references to Dog and Cat objects.
When we call the sound() method on these objects, the method implementation invoked is determined by the
actual type of the object (Dog or Cat) rather than the reference type (Animal). This is dynamic method dispatch
in action.
8. Define package? How to create package with example program.
A Package is nothing but a directory that contain classes and interfaces.
Packages act as “Container” for classes.
PACKAGE CREATION :
To create a package simply include a package command as the first statement in a Java source file.
The package name must be the same as the directory name. For example, if you create a package
named com.example.myapp, you would create a directory named com/example/myapp and then create Java source
files in that directory.
Example:
package com.example.myapp;
Java bytecode is an intermediate representation of Java programs that is executed by the Java Virtual Machine (JVM). It
is platform-independent, meaning that the same Java program can run on different devices and operating systems.
When you compile a Java program, the Java compiler converts your source code into bytecode. This bytecode is then
read and processed by a JVM running on a target system. The JVM interprets the bytecode and executes the instructions
it contains.
Java bytecode is a set of instructions that the JVM can understand and execute. These instructions are typically
represented by one or more bytes, hence the name "bytecode".
Bytecode is a compact form of instruction, which makes it efficient to store and transmit. It is also platform-independent,
which means that the same bytecode can be executed on any platform that has a JVM.
The Java Virtual Machine (JVM) is a software program that executes Java bytecode. The JVM is responsible for loading
bytecode into memory, interpreting it, and executing the instructions it contains.
The JVM is also responsible for managing memory and garbage collection. Garbage collection is the process of
automatically reclaiming memory that is no longer being used by Java objects.
The JVM is a key part of the Java platform. It allows Java programs to be executed on a variety of different platforms,
without having to be recompiled for each platform.
public:
Public members are accessible from any other class or package.
There is no restriction on access.
Example:
public class MyClass {
public int publicVariable;
public void publicMethod() {
// Method implementation
}
}
protected:
Protected members are accessible within the same package and by subclasses (even if they are in different packages).
Example:
class MyClass {
protected int protectedVariable;
protected void protectedMethod() {
// Method implementation
}
}
private:
Private members are accessible only within the same class.
They cannot be accessed from outside the class, not even from subclasses.
Example:
class MyClass {
private int privateVariable;
private void privateMethod() {
// Method implementation
}
}
Access specifiers are essential for encapsulation, allowing you to control the visibility of your classes, methods, and
variables. By choosing the appropriate access specifier, you can enforce encapsulation and ensure proper data hiding
and access control in your Java programs.