Java Lab
Java Lab
Switch case statements are a substitute for long if statements that compare a variable to several integral values.
The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts
of code based on the value of the expression.
A switch works with the byte , short , char , and int primitive data types. ... A statement in the switch block can be
labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all
statements that follow the matching case label.
CODE
import java.util.*;
class pattern
{
public static void main(String args[])
{
System.out.println("Enter a no:");
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
for(int i=1;i<=x;i++)
{
for(int j=x;j>=i;j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT
Topic – Function Overloading
What is function overloading?
Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument
lists are different.
Argument lists refer to the parameters of the method.
If we have to perform only one operation, having same name of the methods increases the readability of the program.
Method overloading is an example of Static Polymorphism.
Three ways to overload a method-
In order to overload a method, the argument lists of the methods must differ in either of these:
Number of parameters
add(int,int)
add(int,int,int)
Data type of parameters
add(int,int)
add(int,float)
Sequence of data types of parameters
add(int,float)
add(float,int)
Code
class calArea {
public void area(double height,double base) {
double area = (height*base)/2;
System.out.println("Area of Triangle is "+area);
}
public void area(int len,int bre) {
int area = len*bre;
System.out.println("Area of Rectangle is "+area);
}
public void area(double radius) {
double area = 3.14*radius*radius;
System.out.println("Area of Circle is "+area);
}
}
class Main {
public static void main(String args[]) {
calArea A= new calArea();
A.area(2.0,2.0);
A.area(3,4);
A.area(2.2);
}
}
Output
Topic – Inheritance
What is Inheritance?
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an
existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current
class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
of the existing class when you create a new class. You can use the same fields and methods already defined in the
previous class.
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends"
is to increase the functionality.
In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or
subclass.
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only.
Single Inheritance :-
When a class inherits another class, it is known as a single inheritance.
Multilevel Inheritance :-
When there is a chain of inheritance, it is known as multilevel inheritance.
Hierarchical Inheritance :-
When two or more classes inherits a single class, it is known as hierarchical inheritance.
Multiple Inheritance :- When one class inherits multiple classes, it is known as multiple inheritance.
Hybrid Inheritance :- Hybrid inheritance is a combination of two or more types of inheritance
class Animal {
// method in the superclass
public void eat() {
System.out.println("I can eat");
}
}
class Dog extends Animal {
public void eat() {
// call method of superclass
super.eat();
System.out.println("I eat dog food");
}
public void bark() {
System.out.println("I can bark");
}
}
Code
class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog labrador = new Dog();
// call the eat() method
labrador.eat();
labrador.bark();
}
}
Output
THANKYOU