Skip to main content

Posts

Showing posts from February, 2021

Super and Final keyword

  Super Keyword      The super keyword in java is a reference varible which is use to refer immediate parent class instance variable . Super can use to invoke immediate parent class method and constructors also. 1. To Refer Instance variable of an immediate parent class:      We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields. For example, class Animal{   String color="white";   }   class Dog extends Animal{   String color="black";   void printColor(){   System.out.println(color); System.out.println(super.color); }   }   class TestSuper1{   public static void main(String args[]){   Dog d=new Dog();   d.printColor();   } } Output: black white 2. To Refer method of an immediate parent class:      The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overri

Polymorphism, Method overloading and Method Overriding

 Introduction of Polymorphism     Polymorphism is the concept by which we can perform single action/operation in different ways. In other word ability to tae more than one form called polymorphism .     Polymorphism plays an important role in allowing objects having different internal structure to share same external interface. That means the general class of operation may be access in same manner but the specified action with each operation may be different. Ploymorphism is use in implementing inheritance. There are two types of polymorphism in java those are as follows: Compile-time polymorphism. Runtime polymorphism.     We can perform polymorphism using method overloading and method overrding. 1. Compile-time polymorphism:     We can achive the compile time polymorphism using function/method overloading concept . When there are multiple function with the same name but having different parameter list or parameter then it is called function/method overloading . We can overload a fun

Inheritance in java

 Introduction     If the one object can acquires all the properties and behaviour of parent object then it's called  Inheritance . The idea behind inheritance concept is, we can create a new class which is build upon existing one so we can reuse the methods and field of the parent class and also create new method in child class. Inheritance is use for method overriding and code reusability concepts. To create new child class from parent class we use the extends keyword. Syntax of define inheritace is as follow: class ChildClass extends ParentClass{     variable declaration;     method declaration; } There are different types of inheritance in java as follows: Single Inheritance. Multilevel Inheritance. Hierarchical Inheritance. 1. Single Inheritance:     A class/parent class inherits another class/child class is called Single inheritance . For example, class Animal{   void eat(){System.out.println("eating...");}   }   class Dog extends Animal{   void bark(){System.out.

Static and this keyword

Static Keyword:      Most of the time static keyword in java use for memory management. We can use static keyword with variables, methods and also with block (Static block) . 1. Static with variables:     If we declare static keyword with variables then it's called static variables . Static variables are use to access or refer common properties of all objects. The static varibels gets memory only once at the time of loading class loading. It makes program more efficient i.e., It saves memory. For example,     static int length=100; 2. Static with method:     If we use static keyword with method then it's called static method . The static method is belong to class rather than object of class. Static method can invoke or call without creating instance or object of class. For example,     static void print(){          System.out.println("Welcome in Java");     } Examples of static variable, method and block is as follows: class Welcome{ static String textValue = "

Java Classes, Object and Methods, Constructors

 Defining a class     A class is a user-defined data type or group of object with a template or blueprint that serves to define its properties. It is a logical entity. Once a class type has been define, we can create " Variables " which are similar to basic type declaration. In java, variables are termed as instance of class, which actual object. The basic form class defination is as follows: class classname [extends superclassname]{     [fields declaration]     [method declaration] } The classname and superclassname are valid java identifiers. The keyword extends indicate that superclassname properties extended into classname. It is a pert of inheritance. (We will discuss inheritance in later part.) Everything inside square brackets is optional that means we can use as per the requirements. For example, class Empty{ } In above example, the Empty class is class name. This class does not have any properties and therefor cannot do anything. A java class content:     Field or

Jump Statements in java

    Introductions       Loopes perform a set of operations repeatedly until the control variable fails to satisfy the test condition. But in some times we need to skip some of the part of loop then we use the jump statements . For breaking the loop we use continue and break statement . Continue Statement:     When the continue statement called, it will break or stop the current itteration of loop on sameline and start the new iteration. Format of this statement is below:          continue; Brak Statement: After call this statement in the body of loop, it will sop or break the loop on current loop on same line and control goes to the next line after that loop. Format is below:     break; We mostly use this statement in while, do, and for loop concept.

Decision Making and Looping

 Introduction     A computer program is well suited for perform certain operation. Every computer language or computer programe have featuer that instruct the program for repetitive task. The process of repeatedly execution of blocks of statement is known as Looping . The statement of block will execute any number of times, from zero to infinite number.   Fig.  Loop Control structure      Java supports such a looping feature. In looping, a sequence of program will executed until some condition for the termination of the loop are satisfied. So looping have two segments, Aprogram loop means its body and control statement means its condition.     The test condition should be carefully stated in order to perform the desired number of loop executions. Java language provides three constructs for looping operation.     while      do     for 1. While:         The simplest of all looping structure in java is the While loop. The basic format is as below:     Initialization;    while( test condi

Decision Making and Branching Statements / Control Statements

 Introduction     Java is a set of statements which executed sequentially in the order in which they appear. This will happen when option or repetitions of certains are not necessary. However, We have a number of situation, where we may have to change the order of execution of statement on bases of certain conditions. This involves a kind of dicision making to see the particular condition occured or not and the direction of execution will execute according to it.     When program braks the sequential flow and jump on the another part of code then it's called branching . When branching is on particular condition then it's called conditional branching . If branches executes without any particular conditions then it's call unconditional branching .     Java supports such decision making capabilities and supports following statements known as Control statement or Decision making statement .     if statements     switch statement     conditional operator statement. 1. if stateme

Operators and Expressions

Introduction     Java Supports  a rich set of operators.  An operator is a symbol that tells the computer to perform certain mathamatical or logical manipulataion. Operators are use in programes to manipulate data and variables. Usually it is part of mathmatical or logical expressions. There are some categories of java operators as below:     Arithmatic Opertaors     Relational Operators     Logical Operators     Assignments Operators     Increment and Decrement Operators.     Conditional Operators     Bitwise Operators     Special Operators. 1. Arithmatic Operators:     Arithmatic operators are use to construct mathemaical expressions an in algebra. Java supports all the basic arithmatic operators.     Arithmatic operators are use as below:          a-b          a-b=10          -14%3=-2 Example code is below:      public class ArithmaticOperations { public static void main(String[] args) { // TODO Auto-generated method stub float a=20.5f, b=4.4f; System.out.println(" a

Variables and data types

 Variables and data types     A variable is an identifier that does not store location. It use to store data values. Unlike constant that remain unchange during the execution of program, but a variable may take different value or can change value different times during the execution of program.     A variable name can be chosen by programmer in meaningful way so it can reflect what it represent in the program. Some example are as follows:     average     height     total_height     classStrength     Variable name may consist of alphabets, digits, the underscore ( _ ) and dollar ( $ ) characters.  It has some conditions those are as follows:     They must not begin/start with digit.     Uppercase and lower case are distinct. That means TOTAL is not same as total .     It should not be a keyword.     White space is not allowed.     Variable name can be any length. Data Types     Every variable in java has data type. Data type specifies the size and type of value that can be stored. Ther

Constants in java

 Constants in java     Constants in java refers to fixed value in java that do not change during the execution of a program. Integer Constants:     An interger constant refers to a sequence of digits. There are three types of integers, Decimal integers, Octal integer and Hexadecimal integer. Decimal integer constant is a set of digits, o through 0, preceded by an optional min sign, Examples are:      123          -321         0          654321 Octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0. Examples are:     037            0          0453       05512 A sequence of digits preceded by 0x or 0X is considered as hexadecimal integers, they may also includes alphabets A through F or a through f . Examples are:     0x2           0x9F              0x4f Octal and hexadecimal numbers are rarely use in programming. Real Constants:     Interegr numbers are inadequate to represent the quantity that vary continuously, such as distance,