Class Inheritance in Python

Rajendra Dharmkar
Updated on 20-Feb-2020 12:43:31

174 Views

Inheritance in classesInstead of defining a class afresh, we can create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name.The child class inherits the attributes of its parent class, and we can use those attributes as if they were defined in the child class. A child class can also override data members and methods from the parent.SyntaxDerived classes are declared much like their parent class; however, a list of base classes to inherit from is given after the class name −class SubClassName (ParentClass1[, ParentClass2, ...]):    'Optional class ... Read More

What Does the Method fill(int, int, int) Do in Java

Samual Sam
Updated on 20-Feb-2020 12:42:04

147 Views

The fill(int[] a, int val) method of the java.util.Arrays class assigns the specified int value to each element of the specified array of integers.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int arr[] = new int[] {1, 6, 3, 2, 9};       System.out.println("Actual values: ");       for (int value : arr) {          System.out.println("Value = " + value);       }       Arrays.fill(arr, 18);       System.out.println("New values after using fill() method: ");       for (int value : arr) {          System.out.println("Value = " + value);       }    } }OutputActual values: Value = 1 Value = 6 Value = 3 Value = 2 Value = 9 New values after using fill() method: Value = 18 Value = 18 Value = 18 Value = 18 Value = 18

Sort Array Using sort(Object[], int, int) Method in Java

Monica Mona
Updated on 20-Feb-2020 12:34:40

123 Views

The java.util.Arrays.sort(Object[] a, int fromIndex, int toIndex) method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       Object ob[] = {27, 11, 5, 44};       for (Object number : ob) {          System.out.println("Number = " + number);       }       Arrays.sort(ob, 1, 3);       System.out.println("Object array ... Read More

What Does the Method sort(int[] a, int fromIndex, int toIndex) Do in Java

karthikeya Boyini
Updated on 20-Feb-2020 12:33:49

142 Views

The sort(int[] a, int fromIndex, int toIndex) method of the java.util.Arrays class sorts the specified range of the specified array of integer value into ascending numerical order. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int iArr[] = {3, 1, 2, 18, 10};       for (int number : iArr) {          System.out.println("Number = " + number);       }       Arrays.sort(iArr, 0, 3);       System.out.println("int array with some ... Read More

What does the method hashCode(int a) do in Java

Samual Sam
Updated on 20-Feb-2020 12:31:32

153 Views

The hashCode(int[]) method of the java.util.Arrays class returns a hash code based on the contents of the specified array. For any two non-null int arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int[] ival = new int[] { 3, 5 };       int retval = ival.hashCode();       System.out.println("The hash code of value1 is: " + retval);       ival = new int[] { 19, 75 };       retval = ival.hashCode(); ... Read More

Access Class Attributes Using Dot Operator in Python

Rajendra Dharmkar
Updated on 20-Feb-2020 12:30:35

718 Views

A class attribute is an attribute of the class rather than an attribute of an instance of the class.In the code below class_var is a class attribute, and i_var is an instance attribute: All instances of the class have access to class_var, which can also be accessed as a property of the class itself −Exampleclass MyClass (object):     class_var = 2     def __init__(self, i_var):         self.i_var = i_var foo = MyClass(3) baz = MyClass(4) print (foo.class_var, foo.i_var) print (baz.class_var, baz.i_var)OutputThis gives the output(2, 3) (2, 4)

What Does the Method add(int i, E element) Do in Java

Priya Pallavi
Updated on 20-Feb-2020 12:26:37

337 Views

The add(int index, E element) method of the java.util.ArrayList class inserts the specified element E at the specified position in this list. It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(15);       arrlist.add(22);       arrlist.add(30);       arrlist.add(40);       arrlist.add(2,25);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }OutputNumber = 15 Number = 22 Number = 25 Number = 30 Number = 40

removeRange Method in Java: Explanation and Usage

Srinivas Gorla
Updated on 20-Feb-2020 12:24:27

147 Views

The removeRange() method of the ArrayList class removes all of the elements from this List whose index is between fromIndex and toIndex.Exampleimport java.util.*; public class ArrayListDemo extends ArrayList{    public static void main(String[] args) {       ArrayListDemo arrlist = new ArrayListDemo();       arrlist.add(10);       arrlist.add(12);       arrlist.add(31);       System.out.println("The list:" + arrlist);       arrlist.removeRange(0,2);       System.out.println("The list after using removeRange:" + arrlist);    } }OutputThe list:[10, 12, 31] The list after using removeRange:[31]

What Does the Method setInt(obj, o) Do in Java

Abhinanda Shri
Updated on 20-Feb-2020 12:22:09

130 Views

The set() method of the ArrayList class replaces the element at the specified position in this list with the specified element.Exampleimport java.util.ArrayList; public class Sample {    public static void main(String args[]) {       ArrayList al = new ArrayList();       System.out.println("Initial size of al: " + al.size());       al.add("C");       al.add("A");       al.add("E");       al.add(1, "A2");       System.out.println("Size of al after additions: " + al.size());       System.out.println("Contents of al: " + al);       System.out.println("Size of al after deletions: " + al.size());     ... Read More

What Does the Method trimToSize Do in Java

Abhinaya
Updated on 20-Feb-2020 12:21:01

150 Views

The trimToSize() method of the java.util.ArrayList class trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String args[]) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(35);       arrlist.add(20);       arrlist.add(25);       arrlist.trimToSize();       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }OutputNumber = 35 Number = 20 Number = 25

Advertisements