Unit-01 - Abstraction, Encapsulation, Scanner Class, Array
Unit-01 - Abstraction, Encapsulation, Scanner Class, Array
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
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.
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.
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.
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.
package GLbajaj_WT;
package GLbajaj_WT;
package GLbajaj_WT;
package GLbajaj_WT;
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
---------------------------------------------------------------------
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.
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;
}
•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 ()
}
•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
}
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 ();
}
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");
}
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;
System.out.println("Enter gender");
char gender =(char) scan.next().charAt(0);
r.setName(name);
r.setEmail(email);
r.setPassword(password);
r.setHight(hight);
r.setAge(age);
r.setGender(gender);
•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
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}};
arrayName.length
arrayName[row-number].length