Types of Array in java There are two

  • Slides: 15
Download presentation

Types of Array in java There are two types of array. • Single Dimensional

Types of Array in java There are two types of array. • Single Dimensional Array • Multidimensional Array • Anonymous array Single Dimensional Array: Initialization of arrays 1) int a[]={1, 2, 3, 4, 5}; 2) int marks[]; Marks= new int[5]; 3)float sal[]={20. 1, 40. 4, 80. 0}; 4)char ch[]={‘q’, ’w’, ’r’, ’l’}; 5)float[] sal={55. 5, 77. 8, 88. 9}; To create 1 D array: • int a[]= new int[n];

One dimensional array Class Oned { Public staic void main(String args[]) {int n; Scnnsr

One dimensional array Class Oned { Public staic void main(String args[]) {int n; Scnnsr sc=new Scnner(System. in); System. out. println(“enter n value”); int n=sc. next. Int(); int a[]=new int[n]; System. out. println(“enter an element into an array”); for(int i=0; i<n; i++) { a[i]=sc. next. Int(); } System. out. println(“the elements of an array”); for(int i=0; i<n; i++) { System. out. println(a[i]); } } }

Multidimensional Array • • Creating a 2 D array: int a[][]= new int[n][m]; Initializing

Multidimensional Array • • Creating a 2 D array: int a[][]= new int[n][m]; Initializing 2 Darray: Int a[3][3]={{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Class Twod { Public staic void main(String args[]) { int n, m; Scnnsr sc=new Scnner(System. in); System. out. println(“enter n and m value”); int n=sc. next. Int(); int m=sc. next. Int(); int a[][]=new int[n][m]; System. out. println(“enter an element into 2 d array”); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { A[i][j]=sc. next. Int(); } } System. out. println(“the elements of 2 D an array”); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { System. out. print(a[i][j] +” “); } System. out. prinln(); } }

Anonymous array Using this concept no need to declare an array while passing to

Anonymous array Using this concept no need to declare an array while passing to method instead we can pass directly into method. Class Anonymous { Static void display(int a[]) { for (int i=0; i<a. length; i++) { int s=0; s=s+a[i]; } System. out. println(“the sum is “+s); } Public static void main(string args[]) { display(new int[]{10, 20, 30, 40, 50}); }} •

Commandline arguments

Commandline arguments

Nested Classes in Java • • In java, it is possible to define a

Nested Classes in Java • • In java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation, and create more readable and maintainable code. The scope of a nested class is bounded by the scope of its enclosing class. Thus in above example, class Nested. Class does not exist independently of class Outer. Class. A nested class has access to the members, including private members, of the class in which it is nested. However, reverse is not true i. e. the enclosing class does not have access to the members of the nested class. A nested class is also a member of its enclosing class. As a member of its enclosing class, a nested class can be declared private, public, protected, or package private(default). Nested classes are divided into two categories: – static nested class : Nested classes that are declared static are called static nested classes. – Inner class : An inner class is a non-static nested class.

Syntax

Syntax

Example for static nested class Outer { static int outer_x = 10; int outer_y

Example for static nested class Outer { static int outer_x = 10; int outer_y = 20; private int outer_private = 30; static class Inner { void display() { System. out. println("outer_x = " + outer_x); System. out. println("outer_y = " + outer_y); System. out. println("outer_private = " + outer_private); } }} public class Inner. Class. Demo { public static void main(String[] args) { Outer o = new Outer (); o. Inner inn=o. new Inner (); inn. display(); } Output: 10 30 }

Example for inner class Outer { static int outer_x = 10; int outer_y =

Example for inner class Outer { static int outer_x = 10; int outer_y = 20; private int outer_private = 30; class Inner { void display() { System. out. println("outer_x = " + outer_x); System. out. println("outer_y = " + outer_y); System. out. println("outer_private = " + outer_private); } }} public class Inner. Class. Demo { public static void main(String[] args) { Outer o = new Outer (); o. Inner inn=o. new Inner (); inn. display(); }} output: 10 20 30

 • Forms of Inheritance • Specialization. The child class is a special case

• Forms of Inheritance • Specialization. The child class is a special case of the parent class; in other words, the child class is a subtype of the parent class. • Specification. The parent class defines behavior that is implemented in the child class but not in the parent class. • Construction. The child class makes use of the behavior provided by the parent class, but is not a subtype of the parent class. • Generalization. The child class modifies or overrides some of the methods of the parent class. • Extension. The child class adds new functionality to the parent class, but does not change any inherited behavior. • Limitation. The child class restricts the use of some of the behavior inherited from the parent class. • Variance. The child class and parent class are variants of each other, and the class-subclass relationship is arbitrary. • Combination. The child class inherits features from more than one parent class. This is multiple inheritance and will be the subject of a later chapter. •

 • • • Difference between static nested and inner class Static nested classes

• • • Difference between static nested and inner class Static nested classes do not directly have access to other members(non-static variables and methods) of the enclosing class because as it is static, it must access the non-static members of its enclosing class through an object. That is, it cannot refer to non-static members of its enclosing class directly. Because of this restriction, static nested classes are seldom used. Non-static nested classes (inner classes) has access to all members(static and nonstatic variables and methods, including private) of its outer class and may refer to them directly in the same way that other non-static members of the outer class do.

Benefits of Inheritance Software Reuse Code Sharing Improved Reliability Consistency of Interface Rapid Prototyping

Benefits of Inheritance Software Reuse Code Sharing Improved Reliability Consistency of Interface Rapid Prototyping Polymorphism Information Hiding Cost of Inheritance Execution speed Program size Message Passing Overhead Program Complexity

final is keyword which is used to prevent changes on it For Example 1)Final

final is keyword which is used to prevent changes on it For Example 1)Final pi=3. 142; Is a constant value which can't changed during the execution of the program 2)if a class is declared as a final which cant be inherited For Example final class A { ---} Class B extends A / { } note a valid statement 3) not override the methods if the methods are declared as final class A { -final void display() { }} Class B extends A { void display() } } • Not is not a valid stament