Java Nested Interface and Method Overloading and Overriding

Last updated on Dec 26 2022
Prabhas Ramanathan

The java command-line argument is an argument i.e. passed at the time of running the java program.
The arguments passed from the console can be received in the java program and it can be used as an input.
So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.

Table of Contents

Simple example of command-line argument in java

In this example, we are receiving only one argument and printing it. To run this java program, you must pass at least one argument from the command prompt.

1. class CommandLineExample{ 
2. public static void main(String args[]){ 
3. System.out.println("Your first argument is: "+args[0]); 
4. } 
5. }

1. compile by > javac CommandLineExample.java
2. run by > java CommandLineExample sonoo
Output: Your first argument is: sonoo

Example of command-line argument that prints all the values

In this example, we are printing all the arguments passed from the command-line. For this purpose, we have traversed the array using for loop.

1. class A{ 
2. public static void main(String args[]){ 
3. 
4. for(int i=0;i<args.length;i++) 
5. System.out.println(args[i]); 
6. 
7. } 
8. }

1. compile by > javac A.java
2. run by > java A sonoo jaiswal 1 3 abc
Output: sonoo
jaiswal
1
3
abc

Difference between object and class

There are many differences between object and class. A list of differences between object and class are given below:

No. Object Class
1) Object is an instance of a class. Class is a blueprint or template from which objects are created.
2) Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc. Class is a group of similar objects.
3) Object is a physical entity. Class is a logical entity.
4) Object is created through new keyword mainly e.g.
Student s1=new Student();
Class is declared using class keyword e.g.
class Student{}
5) Object is created many times as per requirement. Class is declared once.
6) Object allocates memory when it is created. Class doesn’t allocated memory when it is created.
7) There are many ways to create object in java such as new keyword, newInstance() method, clone() method, factory method and deserialization. There is only one way to define class in java using class keyword.

Student s1=new Student(); Class is declared using class keyword e.g.
class Student{}
5) Object is created many times as per requirement. Class is declared once.
6) Object allocates memory when it is created. Class doesn’t allocated memory when it is created.
7) There are many ways to create object in java such as new keyword, newInstance() method, clone() method, factory method and deserialization. There is only one way to define class in java using class keyword.

Let’s see some real life example of class and object in java to understand the difference well:
Class: Human Object: Man, Woman
Class: Fruit Object: Apple, Banana, Mango, Guava wtc.
Class: Mobile phone Object: iPhone, Samsung, Moto
Class: Food Object: Pizza, Burger, Samosa

Difference between method overloading and method overriding in java

There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below:

No. Method Overloading Method Overriding
1) Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2) Method overloading is performed within class. Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3) In case of method overloading, parameter must be different. In case of method overriding, parameter must be same.
4) Method overloading is the example of compile time polymorphism. Method overriding is the example of run time polymorphism.
5) In java, method overloading can’t be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter. Return type must be same or covariant in method overriding.

Java Method Overloading example

1. class OverloadingExample{ 
2. static int add(int a,int b){return a+b;} 
3. static int add(int a,int b,int c){return a+b+c;} 
4. }

Java Method Overriding example

1. class Animal{ 
2. void eat(){System.out.println("eating...");} 
3. } 
4. class Dog extends Animal{ 
5. void eat(){System.out.println("eating bread...");} 
6. }

Java Custom Exception

If you are creating your own Exception that is known as custom exception or user-defined exception. Java custom exceptions are used to customize the exception according to user need.
By the help of custom exception, you can have your own exception and message.
Let’s see a simple example of java custom exception.

1. class InvalidAgeException extends Exception{ 
2. InvalidAgeException(String s){ 
3. super(s); 
4. } 
5. } 
1. class TestCustomException1{ 
2. 
3. static void validate(int age)throws InvalidAgeException{ 
4. if(age<18) 
5. throw new InvalidAgeException("not valid"); 
6. else 
7. System.out.println("welcome to vote"); 
8. } 
9. 
10. public static void main(String args[]){ 
11. try{ 
12. validate(13); 
13. }catch(Exception m){System.out.println("Exception occured: "+m);} 
14. 
15. System.out.println("rest of the code..."); 
16. } 
17. }

 

Output:Exceptionoccured: InvalidAgeException:not valid
rest of the code…

Java Nested Interface

An interface i.e. declared within another interface or class is known as nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred by the outer interface or class. It can’t be accessed directly.

Points to remember for nested interfaces

There are given some points that should be remembered by the java programmer.
• Nested interface must be public if it is declared inside the interface but it can have any access modifier if declared within the class.
• Nested interfaces are declared static implicitely.

Syntax of nested interface which is declared within the interface

1. interface interface_name{ 
2. ... 
3. interface nested_interface_name{ 
4. ... 
5. } 
6. }

Syntax of nested interface which is declared within the class

1. class class_name{ 
2. ... 
3. interface nested_interface_name{ 
4. ... 
5. } 
6. }

Example of nested interface which is declared within the interface

In this example, we are going to learn how to declare the nested interface and how we can access it.

1. interface Showable{ 
2. void show(); 
3. interface Message{ 
4. void msg(); 
5. } 
6. } 
7. class TestNestedInterface1 implements Showable.Message{ 
8. public void msg(){System.out.println("Hello nested interface");} 
9. 
10. public static void main(String args[]){ 
11. Showable.Message message=new TestNestedInterface1();//upcasting here 
12. message.msg(); 
13. } 
14. }

 

Output:hello nested interface
As you can see in the above example, we are acessing the Message interface by its outer interface Showable because it cannot be accessed directly. It is just like almirah inside the room, we cannot access the almirah directly because we must enter the room first. In collection frameword, sun microsystem has provided a nested interface Entry. Entry is the subinterface of Map i.e. accessed by Map.Entry.

Internal code generated by the java compiler for nested interface Message

The java compiler internally creates public and static interface as displayed below:.

1. public static interface Showable$Message 
2. { 
3. public abstract void msg(); 
4. }

Example of nested interface which is declared within the class

Let’s see how can we define an interface inside the class and how can we access it.

1. class A{ 
2. interface Message{ 
3. void msg(); 
4. } 
5. } 
6. 

7. class TestNestedInterface2 implements A.Message{ 
8. public void msg(){System.out.println("Hello nested interface");} 
9. 
10. public static void main(String args[]){ 
11. A.Message message=new TestNestedInterface2();//upcasting here 
12. message.msg(); 
13. } 
14. }

 

Output:hello nested interface

Can we define a class inside the interface?

Yes, If we define a class inside the interface, java compiler creates a static nested class. Let’s see how can we define a class within the interface:

1. interface M{ 
2. class A{} 
3. }

So, this brings us to the end of blog. This Tecklearn ‘Java Nested Interface and Method Overloading and Overiding’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Java Nested Interface and Method Overloading and Overriding"

Leave a Message

Your email address will not be published. Required fields are marked *