Question

In: Computer Science

Give an example of an inner and outer class. (Java)

Give an example of an inner and outer class. (Java)

Solutions

Expert Solution

In Java when the code is too lengthy or complicated you create nested classes or basically group your classes which makes them easy to understand and maintain.

This nesting has mainly inner and outer classes. Inner class is basically the class which is the member of another class. And the class who has other class as member is the outer class. You can understand this with a example -

class Outerclas {

##class methods and functions.

Class Innerclas {

public void nest() {

System.out.println("its the inner class of nested class method"); } } }

class Main {

public static void main(String[] args) {

Outer.Inner inside = new Outer().new Inner();

inside.nest(); } }

The output of this code will be - its the inner class of nested class method.

From the code you can see that the outerclas is the outer class where as the innerclas is the inside class as it's a member of the outerclas.


Related Solutions

true or false give reason language in java 1.A static inner class can access the instance...
true or false give reason language in java 1.A static inner class can access the instance variables and methods of its outer non-static class 2.executeQuery from statement may return more than one resultset object 3.Connection is java.sql interface that establishes a session with a specific database 4.Writable is an interface in Hadoop that acts as a wrapper class to almost all the primitive data type 5.Text is the wrapper class of string in Hadoop
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Java Question: What is exception propagation? Give an example of a class that contains at least...
Java Question: What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.
3. Assume an annulus of inner radius r1 and outer radius r2. The inner surface is...
3. Assume an annulus of inner radius r1 and outer radius r2. The inner surface is at T1, the outer surface at T2, T1 > T2. Assume heat transfer between the surfaces by conduction, with a variable conductivity, k = a + bT, develop an expression for the temperature in the material of the annulus.
1. Describe the difference between instance methods and class methods in Java and give an example...
1. Describe the difference between instance methods and class methods in Java and give an example of each. 2. A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application? 3. Describe the difference between abstract classes and concrete classes, giving an example of each. 4. Explain how data are encapsulated and information is hidden in Java? 5. Explain the difference between a class and an interface...
Implement a version with the outer loop, with a while loop, and the inner loop with...
Implement a version with the outer loop, with a while loop, and the inner loop with a do / while loop. Modify this program as he ask ↑↑↑ C++ // This program averages test scores. It asks the user for the 2 // number of students and the number of test scores per student. 3 #include <iostream> 4 #include <iomanip> 5 using namespace std; 6 7 int main() 8 { 9 int numStudents, // Number of students 10 numTests; //...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
A hollow, conducting sphere with an outer radius of .250 m and an inner radius of...
A hollow, conducting sphere with an outer radius of .250 m and an inner radius of .200 m has a uniform surface charge density of -6.37 muC/me2.When a charge is now introduced at the center of the cavity inside the sphere, the new charge density on the outside of the sphere is -4.46 muC/me2. What is the charge at the center of the cavity?
A potential difference of V0 is maintained between the inner and outer conductor of a coaxial...
A potential difference of V0 is maintained between the inner and outer conductor of a coaxial cable of length L, inner conductor radius a, and outer conductor radius b. (a) Calculate the current through the medium (∈,σ) using the analogy of ?⃗ and ?⃗⃗⃗. (b) Now, assume that V0 = 500 V, L = 50 m, a = 2 cm and b = 6 cm, and the relative permittivity and the conductivity of the medium are ∈?=3 and σ=5 μS/m,...
A short and hollow cylindrical bar, having an outer diameter of 4.00 in and an inner...
A short and hollow cylindrical bar, having an outer diameter of 4.00 in and an inner diameter of 7 in, is subjected to two simultaneous loads - an axial compressive force of 75,100 lb. and a torsional moment of 20,100 lb • in. You are required to create two-dimensional (2D) and three-dimensional (3D) stress elements for two stress states on the outer and inner surfaces of the bar to in-plane and out-of-plane stresses, respectively and then draw the 2D and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT