Unit 2
Unit 2
Demo(int val)
Demo fun()
Demo temp = new ObjRetDemo (a+5); //created a new object return temp; //returning the object from this method
bjRet {
tatic void main(String args[])
javac ObjRetDemo.java
java ObjRet
urned value is = 25
Static, Nested and Inner Classes
That means these are the classes that are defined inside the other classes. The syntax of defining the inner class is
Access modifier class OuterClass There are four types of inner classes -
{
//code
1. Static member classes
Access_modifier class InnerClass
{
//code 2. Member classes
}
} 3. Local classes
4. Anonymous classes
1. Static member classes
This inner class is defined as the static member variable of another class.
3. Local classes
This class is defined within a Java code just like a local variable.
Syntax
Definition: Inheritance is a mechanism in Java by which derived class can borrow the properties of base class and at the same
time the derived class may have some additional properties.
Advantages of Inheritance
One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing
common code amongst several subclasses.
1. Reusability: The base class code can be used by derived class without any need to rewrite the code.
2. Extensibility: The base class logic can be extended in the derived classes.
3. Data hiding: Base class can decide to keep some data private so that it cannot be altered by the derived class.
4. Overriding: With inheritance, we will be able to override the methods of the base class so that meaningful
implementation of the base class method can be designed in the derived class
Inheritance Concept of Base and Derived Class
The inheritance is a mechanism in which the child class is derived from a parent class.
parent class is called base class and child class is called derived class.
Class A
This is Base class
{
………
}
Class B extends A
This is Derived class
{
……….// uses properties of A
}
Types of Inheritance
4. Hybrid inheritance :
//Super class
Implementation of Different Types of Inheritance
class Vehicle {
Vehicle() {
1. Single inheritance : System.out.println("This is a Vehicle");
}
}
// Subclass
class Car extends Vehicle {
Car() {
System.out.println("This Vehicle is Car");
}
}
Note: that Java does not support multiple inheritances with classes. In
Java, we can achieve multiple inheritances only through Interfaces.
INTERFACE AND INHERITANCE DIFFERENCE
super()
1.Accessing Superclass Members
The super keyword allows you to access fields or methods of the parent class when
they are hidden by the subclass.
You can use super to call a method from the parent class that has been
overridden in the child class.
The super() keyword is used to call the constructor of the parent class.
This must be the first statement in the subclass constructor.
1.Accessing Superclass variable
class A
{
void fun()
{
System.out.println("Method: Class A");
}}
class B extends A The super() is used to access the class variable of immediate
{ parent class.
void fun()
{
System.out.println("Method: Class B");
}
void display()
{
super.fun();
}
public static void main(String args[])
{
B obj =new B();
obj.display();
2. Calling Superclass Methods
class A
{
A()
{
System.out.println("Constructor of Class A");
}
class B extends A
{
B()
{
super();
System.out.println("Constructor of Class B");
}
public static void main(String args[])
{
B obj=new B();
}
}
Method Overriding
Method overriding is a mechanism in which a subclass inherits the methods of superclass and sometimes the
subclass modifies the implementation of a method defined in superclass .
The method of superclass which gets modified in subclass has the same name and type signature.
// Example of Overriding in Java 1
class Animal {
// Base class
void move() { System.out.println(
"Animal is moving."); }
void eat() { System.out.println(
"Animal is eating."); }
}
class Dog extends Animal {
@Override void move()
{ // move method from Base class is overriden in this
// method
System.out.println("Dog is running.");
}
void bark() { System.out.println("Dog is barking."); }
}
public class Geeks {
public static void main(String[] args)
{
Dog d = new Dog();
d.move(); // Output: Dog is running.
d.eat(); // Output: Animal is eating.
d.bark(); // Output: Dog is barking. }}
// Example of Overriding in Java 2
class Parent {
void show() { System.out.println("Parent's show()"); }
}
class Child extends Parent {
// This method overrides show() of Parent
@Override void show()
{
System.out.println("Child's show()");
}
}
class Geeks { Output
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
We have defined this method as abstract because, its definition is overridden in the subclasses B and C,
another function of class A that is fun2() is a normal function.
abstract class A
public class AbstractClsDemo
{
{
abstract void fun1()
public static void main(String[] args)
void fun2()
{
{
B b=new B();
System.out.println("A:Infun2");
C c=new C();
}}
b.fun1(); //invoking the overridden method of class B
class B extends A
b.fun2();
{
c.fun1();//invoking the overridden method of class C
void fun1()
c.fun2();
{
}
System.out.println("B:In fun1");
}
}
Output
}
F:\test>javac AbstractClsDemo.java
class C extends A
F:\test>java AbstractClsDemo
{
B:In fun1
void fun1()
A:In fun2
{
C:In fun1
System.out.println("C:In fun1");
A:In fun2
}
}
The Final with Inheritance Final Variables and Methods
The final keyword can be applied at three places - final int a = 10;
void finalize()
{
finalization code
}
Interfaces
……..
…….
}
Step 1: Write following code and save it as my_interface.java Step 2: Write following code in another file and save it using
InterfaceDemo.java
public interface my_interface Java Program[Interface Demo.java]
class A implements my_interface
{ {
public void my_method(int i)
void my_method(int val); {
System.out.println("\n The value in the class A: "+i);}
} public void another_method() //Defining another method
Do not compile this program. Simply save it. not declared in interface
{
Step 3: Compile the program created in step 2 and get the System.out.println("\nThis is another method in class A");
following output }}
class Interface Demo
F:\test>javac InterfaceDemo.java {
public static void main(String args[])
F:\test>java InterfaceDemo {
my_interface obj=new A();
The value in the class A: 100 //or A obj=new A() is also allowed
A obj1=new A();
This is another method in class A obj.my_method(100);
obj1.another_method();
}}
Packages
Package is a mechanism in which variety of classes and interfaces can be grouped together.
package name_of_package
package My Package;
We can create hierarchy of packages. For instance if you save the required class files in the subfolder MyPkg3 and
the path for this subfolder is C:\MyPkg1\MyPkg2\MyPkg3 then the declaration for the package in your java
program will be -
package MyPkg1.MyPkg2.MyPkg3;
Creating and Accessing Package
Step 2: Create one class which contains two methods. We will store this class in a file named A.java. This file will be stored
in a folder My Package. The code for this class will be as follows-