Below you will find pages that utilize the taxonomy term “Java”
Posts
Generics in java
Generics in java are used to specify a type which would be known only when the code is ran or called not when it is written.
Generic also helps you to write code that can work with wide variety of types. So you don’t need to overload a function uneccesarily.
Syntax for classes class Box<T> { } class Pencil { } // Class creation Box<Pencil> = new Box<Pencil>(); In the example above we are creating a generic class and we are telling a type T will be given at call time in the example above we created a class Box and at call time we provided a class Pencil.
Posts
Inheritance in java
In english inherit means to derive qualities from your ancestors genetically.
Just like this in java also, one class can inherit properties and methods of another class using inheritance.
Child extends Parent { } This is the basic syntax for inheritance, We use extends keyword to inherit a class. The class that inherits is also called the subclass and the class which was inherited is called superclass.
To refer to parent class from child class we use super keyword and we need to construct parent in the child class before we intialize any of our properties in our constructor.
Posts
Strings and Arrays
Strings Strings are immutable and they don’t change once defined. String is a class in java which allows you to initialize a string literal. String can be initialize in two different ways
Declaring strings like a normal data type
String name = "some name"; Initialializing a new class method.
String name = new String("some name"); String comes with various different methods like charAt, contains, replace, replaceAll, trim, split. We cannot use indecies to access chars in a string because they are not iterable.
Posts
Packages in Java
Package A package is a grouping of related types providing access protection and name space management.
Advantages Easy to determine how types are related. Makes sure that your names don’t conflict with the names used in the package. Creating a package import mypackage; Naming Convention Packages solve the naming problem but what if two programmers create packages with the same name. That’s why we have naming convention for packages.
Package names are written in all lowercase.
Posts
Method overloading in java
Method overloading is the process of having mulitple method defination with a single name. The complier knows which one to call on the basis of the signature of the method which ever signature matches the call that method is invoked.
By default while overloading if the contract doesn’t match it tries to type convert and see if any siganture match like you can have a method to add two ints and if you try to call it with chars it will get invoked.