Posts

Showing posts with the label PROGRAMS

Rules for Method Overriding and Example Program

Image
Rules for Method Overriding The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass. The access level cannot be more restrictive than the overridden method's access level. For example: If the superclass method is declared public then the overridding method in the sub class cannot be either private or protected. Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected....

Hierarchical Inheritance in java with example

Image
Hierarchical Inheritance : In this  inheritance   multiple classes inherits from a  single class i.e there is one super class and  multiple   sub classes. As we can see from the below diagram when a same class is having more than one sub class (or) more than one sub class has the same parent is called as  Hierarchical Inheritance . public class ClassA { public void dispA () { System . out . println ( "disp() method of ClassA" ); } } public class ClassB extends ClassA { public void dispB () { System . out . println ( "disp() method of ClassB" ); } } public class ClassC extends ClassA { public void dispC () { System . out . println ( "disp() method of ClassC" ); } } public class ClassD extends ClassA { public void dispD () { System . out . println ( "disp() method of ClassD" ); } } public class Hierar...

Multilevel Inheritances in Java Program

Image
Multilevel inheritances in Java In Multilevel inheritances there exists single base class, single derived class and multiple intermediate base classes. Single base class + single derived class + multiple intermediate base classes. Intermediate base classes An intermediate base class is one in one context with access derived class and in another context same class access base class. class Car { public void Car() { System.out.println("Class Car"); } } class Maruti extends Car { public void Maruti() { System.out.println("Class Maruti"); } public void brand() { System.out.println("Brand: Maruti"); } } public class Inherit extends Maruti { public void speed() { System.out.println("Max: 80Kmph"); } public static void main(String args[]) { Inherit obj=new Inherit(); obj.brand(); obj.speed(); obj.Car(); obj.Maruti(); } }

SINGLE INHERITANCE :

Image
SINGLE INHERITANCE : The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class. class Shape { int length ; int breadth ; } public class Rectangle extends Shape { int area ; public void calcualteArea () { area = length * breadth ; } public static void main ( String args []) { Rectangle r = new Rectangle (); //Assigning values to Shape class attributes r . length = 10 ; r . breadth = 20 ; //Calculate the area r . calcualteArea (); System . out . println ( "The Area of rectangle of length \"" + r . length + "\" and breadth \"" + r . breadth + "\" is \"" + r . area + "\"" ); } }

Parameterized constructor Example

class Rectangle  {   int length;  int breadth; Rectangle(int len,int bre)   {   length  = len;  breadth = bre;   } public static void main(String args[])  {          Rectangle r1 = new Rectangle(20,10);   System.out.println("Length of Rectangle : " + r1.length);   System.out.println("Breadth of Rectangle : " + r1.breadth);   } }

Default constructor Example Program

class NoteBook {     /*This is default constructor. A constructor does      * not have a return type and it's name      * should exactly match with class name     */     NoteBook() {             System.out.println("Default constructor");    }  public void mymethod()     {                  System.out.println("Hello");     }     public static void main(String args[]) { /* new keyword creates the object of the class           * and invokes constructor to initialize object          */ NoteBook obj = new NoteBook(); obj.mymethod();     } }

Scanner Example Programs

Java Scanner Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them. The Java Scanner class breaks the input into tokens using a delimiter which is white space by default. It provides many methods to read and parse various primitive values. The Java Scanner class is widely used to parse text for strings and primitive types using a regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc. The Java Scanner class extends Object class and implements Iterator and Closeable interfaces. The Java Scanner class provides nextXXX() methods to return the type of value such as nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(), nextBoolean(), etc. To get a single character from the scanner, you can call next().charAt...

BufferedReader Example Program

import java.io.*; class BufferedReaderDemo { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter 1st number"); int x=Integer.parseInt(br.readLine()); System.out.println("enter 2nd number"); int y=Integer.parseInt(br.readLine()); System.out.println("sum is :"+(x+y)); } }

DoWhileExample Program

public class DoWhileExample  {  public static void main(String[] args)  {      int i=1;      do       {          System.out.println(i);                 i++;      }         while(i<=10);  }  }

WhileExample Program

public class WhileExample  {  public static void main(String[] args)  {      int i=1;      while(i<=10) {          System.out.println(i);                  i++;      }  }  }

For Loop Example Program

public class Test  {    public static void main(String args[])  {       for(int x = 10; x < 20; x = x + 1)  {          System.out.print("value of x : " + x );          System.out.print("\n");       }    } }

Nested IF Example Program

   public class Test  {    public static void main(String args[])  {       int x = 30;       int y = 10;       if( x == 30 )  {          if( y == 10 )  {             System.out.print("X = 30 and Y = 10");          }       }    } }

Switch Case Example Program

class   WeekDays {      public   static   void  main ( String  args [])      {          int  day  =   2 ;                   switch ( day )          {              case   1 :                  System . out . println ( "Monday" );                  break ;              case   2 :                  System . out . println ( "Tuesday" );          ...