0% found this document useful (0 votes)
67 views

Java Lab

Switch case statements provide an easy way to dispatch execution to different parts of code based on the value of an expression. Nested for loops allow a loop to exist inside the body of another loop. Function overloading allows a class to have multiple methods with the same name but different argument lists. Inheritance is an important part of object-oriented programming that allows a subclass to acquire properties and behaviors of a parent class.

Uploaded by

Damini Shars
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Java Lab

Switch case statements provide an easy way to dispatch execution to different parts of code based on the value of an expression. Nested for loops allow a loop to exist inside the body of another loop. Function overloading allows a class to have multiple methods with the same name but different argument lists. Inheritance is an important part of object-oriented programming that allows a subclass to acquire properties and behaviors of a parent class.

Uploaded by

Damini Shars
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Switch Case

Nested for loops


Function Overloading
Inheritance
Topic –
Ananya Gupta , Damini Kumari, Meghna Singhal, Rajat
Mittal
(00396202718,1196202718,2396202718,2896202718)
Topic – Switch Case
WHAT IS SWITCH CASE

 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

public class code {


public static void area(int len,int bre)
{ int area = len*bre;
System.out.println("area of rectangle is "+area);
}
public static void area(int rad)
{ int area =22*rad*rad/7;
System.out.print("areaa of circle is "+area);
}
CODE

public static void main(String[] args)


{
Scanner s = new Scanner(System.in);
int c = s.nextInt();
switch(c)
{
case 1:
area(20,30);
break;
case 2:
area(5);
break;
}
}
}
OUTPUT
Topic – Nested for loops
FOR LOOP
 for loop: A Java statement that executes a group of statements repeatedly until a given test fails.
General syntax:
for (<initialization> ; <test> ; <update>) {
<statement>;
<statement>;
...
<statement>;
}
EXAMPLE:- for (int i = 1; i <= 5; i++) { // repeat 5 times
System.out.println("I am so smart");
}
 Behavior of the for loop:
 Start out by performing the <initialization> once.
 Repeatedly execute the <statement(s)> followed by the <update> as
long as the <test> is still a true statement.
NESTED FOR LOOP
 If a loop exists inside the body of another loop, it's called a nested loop. So using a For loop inside another For loop is
known as nested for loop.
// outer loop
for (int i = 1; i <= 5; ++i) {
// codes // inner loop
for(int j = 1; j <=2; ++j) {
// codes
}
.. }

 Behavior of the for loop:


CODE
AIM:-Write a Program to print the following pattern, if number is 6
654321
65432
6543
654
65
6

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.

Why use inheritance in java?


 For Method Overriding (so runtime polymorphism can be achieved).
 For Code Reusability.

 Terms used in Inheritance


 Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
 Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
 Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
 Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods

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

 Why multiple inheritance is not supported in java?


 Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you
call it from child class object, there will be ambiguity to call the method of A or B class.
 Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same
method or different, there will be compile time error.
Code

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

You might also like