0% found this document useful (0 votes)
9 views36 pages

Unit-01 - Abstraction, Encapsulation, Scanner Class, Array

The document outlines the course KCS 602: Web Technology, taught by Ms. Pragya, focusing on Java concepts such as abstraction, interfaces, encapsulation, and arrays. It provides detailed explanations of abstract classes and methods, their implementation, and the use of interfaces for achieving abstraction in Java. Additionally, it includes examples and code snippets to illustrate these concepts, emphasizing their importance in developing robust software solutions.

Uploaded by

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

Unit-01 - Abstraction, Encapsulation, Scanner Class, Array

The document outlines the course KCS 602: Web Technology, taught by Ms. Pragya, focusing on Java concepts such as abstraction, interfaces, encapsulation, and arrays. It provides detailed explanations of abstract classes and methods, their implementation, and the use of interfaces for achieving abstraction in Java. Additionally, it includes examples and code snippets to illustrate these concepts, emphasizing their importance in developing robust software solutions.

Uploaded by

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

Department of Computer Science and Engineering

Course Code : KCS 602


Course Name :WEB TECHNOLOGY
Faculty Name : Ms. Pragya
Email : [email protected]
Department of Computer Science and Engineering
Vision
To build strong teaching environment that responds to the needs of industry and challenges of the society.

Mission
• M1 : Developing strong mathematical & computing skill set among the students.
• M2 : Extending the role of computer science and engineering in diverse areas like Internet of Things (IoT),
Artificial Intelligence & Machine Learning and Data Analytics.
• M3 : Imbibing the students with a deep understanding of professional ethics and high integrity to serve the
Nation.
• M4 : Providing an environment to the students for their growth both as individuals and as globally competent
Computer Science professional wit encouragement for innovation & start-up culture.

Subject:
Syllabus

Subject: Web Technology


Abstraction in Java

Abstraction in Java is the process in which we only show essential details/functionality to the user.
The non-essential implementation details are not displayed to the user.

In Java, abstraction is achieved by interfaces and abstract classes.


Achieve 100% abstraction using interfaces and achieve partial abstraction using abstract class.

Abstraction Real-Life Example:


Consider a real-life example of a man driving a car.
The man only knows that pressing the accelerators will increase the speed of a car or applying brakes will
stop the car, but he does not know how on pressing the accelerator the speed is actually increasing, he
does not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc
in the car. This is what abstraction is.

Subject : Web Technology


Abstraction in Java

Java Abstract classes and Java Abstract methods

An abstract class is a class that is declared with an abstract keyword.


Syntax: abstract class <Class_Name>
{
// code
}
Example: abstract class Abc
{
// code
}

Subject : Web Technology


Abstraction in Java

Java Abstract classes and Java Abstract methods

An abstract method is a method that is declared without implementation.


Example: abstract class Abc
{
abstract public void abcd();
}
An abstract class may or may not have all abstract methods. Some of them can be concrete methods
Example: abstract class Abc
{
abstract public void abcd();
public void run()
{
// code
}
}

Subject : Web Technology


Abstraction in Java

Java Abstract classes and Java Abstract methods


A method-defined abstract must always be redefined in the subclass, thus making overriding compulsory
or making the subclass itself abstract.

Any class that contains one or more abstract methods must also be declared with an abstract keyword.

There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with
the new operator.

An abstract class can have parameterized constructors and the default constructor is always present in an
abstract class.

Subject : Web Technology


Abstraction in Java

Algorithm to implement abstraction in Java


Determine the classes or interfaces that will be part of the abstraction.

Create an abstract class or interface that defines the common behaviors and properties of these classes
.
Define abstract methods within the abstract class or interface that do not have any implementation
details.

Implement concrete classes that extend the abstract class or implement the interface.

Override the abstract methods in the concrete classes to provide their specific implementations.
Use the concrete classes to implement the program logic.

Subject : Web Technology


Abstraction in Java

When to use abstract classes and abstract methods?

There are situations in which we will want to define a superclass that declares the structure of a given
abstraction without providing a complete implementation of every method.

Sometimes we will want to create a superclass that only defines a generalization form that will be shared
by all of its subclasses, leaving it to each subclass to fill in the details.

Subject : Web Technology


Abstraction in Java

When to use abstract classes and abstract methods?

abstract Class CSE


{
extends abstract public void webTechnology();
public void faculty()
{ extends
//
}

Concrete class Concrete class


class Cse_A{ class Cse_B
{
//Override method //Override method
{} {}
public void lectureDelivery() public void lectureDelivery()
{} {}
} }

Subject : Web Technology


Abstraction in Java

package GLbajaj_WT;

public abstract class CseDepartment {


private int faculty=75;
int thirdyearfaculty=10;

abstract public void computerNetwork();


abstract public void webTechnology();

public void courseThirdYear()


{
System.out.println("No of faculties assigned for third year
subject: "+thirdyearfaculty);
System.out.println("---------------------------------------------------------
------------");
}

Subject : Web Technology


Abstraction in Java

package GLbajaj_WT;

public class Cse_A extends CseDepartment{


public void computerNetwork()
{
System.out.println("Notes provided");
}

public void webTechnology()


{
System.out.println("Quiz provided");
}
public void attendance()
{
System.out.println("75% mandatory");
}
}

Subject : Web Technology


Abstraction in Java

package GLbajaj_WT;

public class Cse_B extends CseDepartment{


public void computerNetwork()
{
System.out.println("Unit -01 Notes provided");
}

public void webTechnology()


{
System.out.println("Core Java....");
}
public void attendance()
{
System.out.println("Absentees not allowed");
}

Subject : Web Technology


Abstraction in Java

package GLbajaj_WT;

public class TestCSEDept {

public static void main(String[] args) {


Cse_A a=new Cse_A();
a.computerNetwork();
a.webTechnology();
a.attendance();
Cse_B b=new Cse_B();
b.computerNetwork();
b.webTechnology();
b.attendance();
a.courseThirdYear();
}

Subject : Web Technology


Abstraction in Java

Output:

Notes provided
Quiz provided
75% mandatory
Unit -01 Notes provided
Core Java....
Absentees not allowed
No of faculties assigned for third year subject: 10
---------------------------------------------------------------------

Subject : Web Technology


Interface in Java

Interface
By using interfaces, we can achieve 100% abstraction in Java classes.
In Java or any other language, interfaces include both methods and variables but lack a method body. Apart from
abstraction, interfaces can also be used to implement interfaces in Java.

Implementation: To implement an interface we use the keyword “implements” with class.

extends implements extends

Subject : Web Technology


Interface in Java

•Interface in java is declared using keyword interface.


•It is also just like normal class.
•For interface also the .class file is going to be generated.
•All methods inside interface are public by default.
•All variables declared inside interface is implicitly public final variable or constants.

interface Office
{
int employee; //Error because only final variables so they need to be initialized has denoted in below
example. No other types are allowed inside interface
}

interface Office
{
int employee=100;
}

Subject : Web Technology


Interface in Java

•All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract
keyword.
•Cant create the Object of interface.
•In Java its legal for an interface to extend multiple interface.
•for example following code :
package com.WT;
interface Animal
{
public void eat();
public void walk();
}
interface Human
{
public void talk();
public void work();
}
interface Livingbeing extends Animal,Human // extend multiple interface
{
public void enjoy ()
}

Subject : Web Technology


Interface in Java

•Whenever we implement interface we need to provide implementation to all the abstract methods of it, or else we
need to make the inherited class also has abstract.
interface Office
{
void work();
public void enjoy();
}
abstract class Manager implements Office
{
//implementation is not give for work() and enjoy().so class should be abstract
}

Subject : Web Technology


Interface in Java

Example:-
interface Animal
{
public void eat();
public void walk();
}
interface Human
{
public void talk();
public void work();
}
interface Livingbeing extends Animal,Human
{
public void enjoy ();
}

Subject : Web Technology


Interface in Java

public class_Test implements Livingbeing


{
public void eat()
{
// TODO Auto-generated method stub
}
public void walk()
{
// TODO Auto-generated method stub
}
public void talk()
{
// TODO Auto-generated method stub
}
public void work()
{ // TODO Auto-generated method stub
}
public void enjoy()
{ // TODO Auto-generated method stub } }

Subject : Web Technology


Interface in Java

The Interface reference can be give to its child class object. It behave same has the inheritance. Using the interface
reference we cant accesses the child class members. But we can accesses the overrided methods.
Example:
interface Animal
{
public void eat();
public void walk();
}
class Cow implements Animal
{
public void eat()
{
System.out.println("Cow eats");
}
public void walk()
{
System.out.println("Cow walk in four legs");
}

Subject : Web Technology


Interface in Java

public void giveMilk()


{
System.out.println("Cow give white milk");
}
}
class Test1
{
public static void main(String[] args)
{
Animal a = new Cow();
a.eat();
a.walk();
a.giveMilk();//Error because parent reference cant accesses child members
}
}

Subject : Web Technology


Abstract Vs Interface in Java

Subject : Web Technology


Scanner Class in Java

Scanner is a class in java.


Which is present in java.util package.
Scanner is used to scan the input for the program.
Scanner will help to scan the input from the keyboard and also we can read the content of the file has a input for
the program.

Scanner scanFromKey = new Scanner(System.in);


Scanner scanFromFile = new Scanner(new FileReader("vikas.txt"));

Subject : Web Technology


Encapsulation in Java

Encapsulation
•Encapsulation is the packing of data and functions into a single component.
•Encapsulation is the ability to package data, related behavior in an object bundle and control/restrict access to them
(both data and function) from other objects.
•It is all about packaging related stuff together and hide them from external elements.
•Encapsulation can be achieved by declaring fields in a class as private, while providing access to these fields via
public, typically, getter and setter methods. Or else we can tell that the encapsulation can be achieved by java beans
•Every java class has the concept of encapsulation.

ENCAPSULATION=DATA HIDING+ABSTRACTION

import java.util.Scanner;
class Register
{
private String name,email,password;
private int age; Data Hiding
private long phone;
private double hight;
private char gender;

Subject : Web Technology


Encapsulation in Java

public String getName() NOTE:


{ Here we use getter and setter methods.
return name;
} <return data type> getXXX()
public void setName(String name) {
{ return value;
this.name = name; }
}
public String getEmail() public void setXXX()
{ {
return email;
} }
public void setEmail(String email)
{
this.email = email;
}

Subject : Web Technology


Encapsulation in Java

public String getPassword()


{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}

Subject : Web Technology


Encapsulation in Java

public long getPhone()


{
return phone;
}
public void setPhone(long phone)
{
this.phone = phone;
}
public double getHight()
{
return hight;
}
public void setHight(double hight)
{
this.hight = hight;
}

Subject : Web Technology


Encapsulation in Java

public char getGender()


{
return gender;
}
public void setGender(char gender)
{
this.gender = gender;
}}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
class RegisterDetails
{
void insertDetails(Register r)
{
System.out.println("Name is "+r.getName()); System.out.println("Email is "+r.getEmail());
System.out.println("Password is "+r.getPassword()); System.out.println("Age is "+r.getAge());
System.out.println("Gender is "+r.getGender()); System.out.println("Phone number "+r.getPhone());
System.out.println("Height is "+r.getHight());
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------

Subject : Web Technology


Encapsulation in Java

public class Demo


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name");
String name = scan.next();
System.out.println("Enter the Email");
String email = scan.next();
System.out.println("Enter the password");
String password = scan.next();
System.out.println("Enter the age");
int age=scan.nextInt();
System.out.println("Enter phone number");
long l = scan.nextLong();
System.out.println("Enter height");
double height = scan.nextDouble();

Subject : Web Technology


Encapsulation in Java

System.out.println("Enter gender");
char gender =(char) scan.next().charAt(0);

Register r = new Register();

r.setName(name);
r.setEmail(email);
r.setPassword(password);
r.setHight(hight);
r.setAge(age);
r.setGender(gender);

RegisterDetails rd = new RegisterDetails();

rd.insertDetails(r);// hear the object is sent to the insertDetails method in RegisterDetails


}
}

Subject : Web Technology


Encapsulation in Java

Output Output after taking input


Enter the name Name is ABCD
ABCD Email is [email protected]
Enter the Email Password is 12345
[email protected] Age is 27
Enter the password Gender is M
12345 Phone number 0000000002
Enter the age Height is 5.9
27
Enter phone number
0000000002
Enter height 5.9
Enter gender M

Subject : Web Technology


Array in Java

•Array is a collection of similar type of elements that, all elements are stored in same memory location.
•We can store the fixed number of elements in the array( i mean the mentioned size of elements).
•There are two types of array:
One dimensional array
Multi dimensional array
•To check the size of array.

Syntax :
arrayName.length

Declaring Array One Dimensional Array :


dataType variable[ ] = new dataType[size];
Example :
int[ ] a = new int[5];
int [ ]a = new int[5];
int a[ ] = new int[5];
int a[ ] = {1,2,3,4,5,6};

Subject : Web Technology


Array in Java

Two dimensional array :


dataType variable[ ][ ] = new dataType[row-size][column-size];

Example :
int[ ][ ] a = new int[3][3];
int [ ]a[ ] = new int[3][3];
int a[ ][ ] = new int[3][3];
int [ ][ ]a = new int[3][3];
int a[ ][ ] = {{1,2,3},{4,5,6}};

Note : - To check the size of array or to get the size of array

arrayName.length

To check the size of individual row of array

arrayName[row-number].length

Subject : Web Technology


Array in Java

public class Program {


for(int j=0;j<a[i].lenghth;j++)
public static void main(String[] args) throws IOException
{
{
System.out.println(a[i][j]+” “);
Scanner s = new Scanner(System.in);
}
int a[][] = new int[3][];
System.out.println();
a[0]=new int[4];
}
a[1]=new int[1];
}
a[2]=new int[2];
}

Subject : Web Technology

You might also like