Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Java All

The document discusses abstraction in Java. It describes two ways to achieve abstraction: abstract classes and interfaces. Abstract classes can contain abstract and concrete methods, while interfaces only contain abstract methods. The document provides examples of how to define abstract classes and interfaces, and how to extend abstract classes and implement interfaces in subclasses. It also discusses why abstraction and interfaces are important concepts in object-oriented design.

Uploaded by

Vikas Pallewar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Java All

The document discusses abstraction in Java. It describes two ways to achieve abstraction: abstract classes and interfaces. Abstract classes can contain abstract and concrete methods, while interfaces only contain abstract methods. The document provides examples of how to define abstract classes and interfaces, and how to extend abstract classes and implement interfaces in subclasses. It also discusses why abstraction and interfaces are important concepts in object-oriented design.

Uploaded by

Vikas Pallewar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Abstraction-

It is the process of hiding the some details and showing the important
information to the end user called as “Abstraction”.

Example-

1. Car

2. ATM Machine

3. TV Remote

How to achieve the Abstraction in java?

There are two ways to achieve the abstraction in java.

1. Abstract class
2. Interface

Abstract class

 Abstract class have constructor

 It contains abstract methods or concrete methods or empty class or

combination of both methods.

 We cannot create the object of abstract class.

 To use abstract method of class, we should extends the abstract class and

use that methods.

 If we don't want to implement or override that method, make that class as

abstract.

 If any method is abstract in a class then that class must be declared as

abstract

Note- Multiple inheritances are not allowed in abstract class but allowed in

interfaces

Example-1

package com.abstraction;

public class Test { // this is abstract class


abstract void x1(); // this is abstract method

}
Here, method is the abstract then class should be abstract only as per below
example

package com.abstraction;

public abstract class Test { // this is abstract class

abstract void x1(); // this is abstract method

Example-2- we can write multiple abstract method into abstract class as per below

package com.abstraction;

public abstract class Test {

abstract void x1(); // abstract method

abstract void x2();


}

How to implement abstract methods?

We need to create the class which extends from abstract class as shown in
below.

package com.abstraction;

//this is the implementation class


public class Example extends Test {

@Override
void x1() {
System.out.println("x1 method..");
}

@Override
void x2() {
System.out.println("x2 method..");
}
}

package com.abstraction;
public class TestMain {

public static void main(String[] args) {

Example example = new Example();


example.x1();
example.x2();

}
}

Note- Suppose in the sub class, I don’t want to override the abstract methods then
make that subclass as abstract.

Interface-

1. It contains public abstract methods and public static final variables by


default.
2. We must follow I to C design principle in java. It means every class should be
implemented by some interfaces.
3. In company, Senior Software Engineer or Team Lead or Manager level people
can design the interface then give it to developer then developer will
implement it by writing the business logic into it.

4. We cannot create the object of interface.

5. In interface, we can just define the method only but implemented those
methods into implemented class.

6. Before 1.7, interface does not have any method body.

7. In 1.8 Declare the default & static method with body in interface.

8. In 1.9 we can define the private methods in interface also.

9. Java supports multiple inheritance in the terms of interfaces but not classes.

10. Interface does not have constructor.


Syntax

interface interface_name {

Example-1

public interface Demo {

public abstract void x1();


public static final int a=5;
}
Example-2

public interface A {
public abstract void x1(); // allowed

public void x2(); // allowed

abstract void x3();// allowed

void x4(); // allowed

Note- if we don’t write public or abstract in interface then JVM will insert it
automatically.

Example-3

package com.abstraction;

public interface A {
public abstract void x1(); // allowed
}

package com.abstraction;

//this is the implementation class


public class Test implements A {

@Override
public void x1() {
System.out.println("Test-x1 method");
}

package com.abstraction;

public class TestExample {

public static void main(String[] args) {

Test test= new Test();


test.x1();
}
}

Output

Test-x1 method

Example-4

package com.abstraction;

public interface A {

package com.abstraction;

public interface B {

package com.abstraction;

public interface C extends A,B {


}

This is allowed in java.

Example-5

package com.abstraction;

public interface A {
public abstract void x1(); // allowed
}

package com.abstraction;

public interface B {
public abstract void x1(); // allowed
}

package com.abstraction;

public class Test implements A,B {

@Override
public void x1() {
System.out.println("Test-x1 method");
}

package com.abstraction;

public class TestExample {

public static void main(String[] args) {

Test test= new Test();


test.x1();
}
}

package com.abstraction;

public class TestExample {


public static void main(String[] args) {

Test test= new Test();


test.x1();
}
}
Output
Test-x1 method

Below are the list of possible scenario regarding the interface and

Note- Try this from your end on laptop or desktop.

 interface can extend interface1 and interface2


 Interface can extends interface
 Interface can extends the multiple interface
 class extends class implements interface
 class implements interface
 class extends cl
ass implements interface1 and interface2

Why interface?

Suppose there is a requirement for Amazon to integrate SBI bank code into their
shopping cart. Their customers want to make payment for products they
purchased.

Let's say SBI develops code like below:

class Transaction {
void withdrawAmt(int amtToWithdraw) {
//logic of withdraw
// SBI DB connection and updating in their DB
}
}

Amazon needs this class so they request SBI bank for the same. The problem with
SBI is that if they give this complete code to amazon they risk exposing everything
of their own database to them as well as their logic, which cause a security
violation.

Now the solution is for SBI to develop an Interface of Transaction class as shown
below:
interface Transactioni {

void withdrawAmt(int amtToWithdraw) ;

class TransactionImpl implements Transactioni {

void withdrawAmt(int amtToWithdraw) {

//logic of withdraw

//SBI DB connection and updating in their DB

Now how amazon will do this as below as-

Transactioni ti = new TransactionImpl();

ti.withdrawAmt(500);

In this case, both application can achieve their aims.


Encapsulation-

The binding of data into single entity called as “Encapsulation.”

Example-

Class is the entity which contains variables and methods.

Class Employee {

int salary;

void m5() {

Real Time Example-

Suppose you have an account in the bank. If your balance variable is declared as a
public variable in the bank software, your account balance will be known as public,
In this case, anyone can know your account balance. So, is it correct approach?
answer is No.

So, they declare balance variable as private for making your account safe, so that
anyone cannot see your account balance.

The person who has to see his account balance, will have to access only private
members through public methods defined inside that class.

Thus, we can achieve security by utilizing the concept of data hiding. This is called
Encapsulation in Java.

Why ?

Employee e1= new Employee();

e1.salary=5000; //case 1

Employee e2= new Employee();

e2.salary=-3000; //case 2

In this example, case 1, we are passing the 5000 salary to the employee that is
correct but case 2, we are passing the salary -3000 that is the negative salary.
So salary can not be negative.

How we are going to achieve this by using encapsulation-

public class Employee {

private int salary;

public void setSalary(int sal) {


if (sal > 0) {
salary = sal;
} else {
salary = 0;
}
}

public int getSalary() {


return salary;
}
}

In this example, we are checking the whether the salary is greater than zero.
Because salary cannot be negative so in this way, we are going to achieve the
encapsulation.

Note- Always keeps global variables private and allows others to assign values
through public methods.

Program for Encapsulation using hard coded values

package com.encapsulation;

public class EncapsulationTest {

public static void main(String[] args) {

Employee employee= new Employee ();


employee.setSalary(-5000);
System.out.println("salary>>"+employee.getSalary());
}
}
Program for Encapsulation using Dynamic values.

package com.encapsulation;

public class Employee {

private int employeeId;


private String employeeName;
private String employeeCity;

public int getEmployeeId() {


return employeeId;
}

public void setEmployeeId(int employeeId) {


this.employeeId = employeeId;
}

public String getEmployeeName() {


return employeeName;
}

public void setEmployeeName(String employeeName) {


this.employeeName = employeeName;
}

public String getEmployeeCity() {


return employeeCity;
}

public void setEmployeeCity(String employeeCity) {


this.employeeCity = employeeCity;
}

package com.encapsulation;

import java.util.Scanner;

public class TestMain {

public static void getUserInput() {


System.out.println("Enter the ID>>");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
System.out.println("Enter the Name>>");
String name = scanner.next();
System.out.println("Enter the City");
String city = scanner.next();

Employee employee = new Employee();


employee.setEmployeeId(id);
employee.setEmployeeName(name);
employee.setEmployeeCity(city);

System.out.println("Employee Id>>" +
employee.getEmployeeId());
System.out.println("Employee Name>>" +
employee.getEmployeeName());
System.out.println("Employee City>>" +
employee.getEmployeeCity());

public static void main(String[] args) {


getUserInput();
}
}

Output-
Enter the ID>>
10
Enter the Name>>
ram
Enter the City
pune
Employee Id>>10
Employee Name>>ram
Employee City>>pune
Inheritance-

The process of creating the new class by using the existing class functionality called
as Inheritance.

Or

It is a mechanism in which one class acquires the property of another class

Inheritance means simply reusability.

It is called as - (IS Relationship)

Example- IS Relationship

Class Policy {

Class TermPolicy extends Policy {

In this example, Policy is super class and TermPolicy is sub class

Where TermPolicy IS A Policy.

Note-

All the parent members are derived into child class but they are depending upon
the below two condition as

-To check the access specifiers

-Members does not exist into sub class.

UML Diagram-

Parent -P
Child – C

Where p is parent class and c is the child class.

Note- Below are different names for super and sub class

Super Class->Parent Class->Base Class-> Old Class

Sub Class-> Child Class-> Derived Class -> New Class

Note-

1. Inherit the classes by using extends keywords.


2. Whenever we create the object of subclass then all the member will get
called super class as well as sub class. Because reason is that super class
members automatically inherited into sub class that’s why.
3. Why we use inheritance that is for code reusability, reusability means we can
reuse existing class features such as variables and method, etc.
4. We cannot extend the final class.

When to use?

If we want to extends or increase of features of class then go for inheritance.

Business requirement-

Why inheritance?

Suppose we have one class which contain the fields like, firstname, lastname,
address, city, mobile number and

In future we got the requirement to add the email then what option we have below-

1. Modify the attributes/fields/variable in existing class but this is not good


option it will increase the testing for that class.
2. Add the attributes/fields/variable in the new class, this is the good option we
can also reduce the testing efforts for this.
How the class will look like

Class Parent {

String firstname;

String lastname;

String address;

String city;

String mobilenumber;

Class Child extends Parent {

String email;

Note

We cannot assign parent class to child class- it means Child c=new Parent(); Here
we cant write new Parent();

All the members of super class will be directly inherited into sub class and they are
eligible and depends on access specifiers only.
Dynamic dispatch-

The process of assigning the child class reference to parent class called as
“Dynamic dispatch.”

Example-

Class X {

Class Y extends X {

Class Test {

Public static void main(string args[]){

X x= new Y(); // Here we are assigning the child reference new Y() to parent class
as X.

Inheritance Example-

Scenario 1

package com.inheritance;

class X {

int a = 10;
int b = 20;

void m1() {
System.out.println("Class X- m1() method");
}

void m2() {
System.out.println("Class X- m2() method");
}
}
package com.inheritance;

class Y extends X {

int b = 30;
int c = 40;

void m2() {
System.out.println("Class Y- m2() method");
}

void m3() {
System.out.println("Class Y- m3() method");
}
}

package com.inheritance;

public class TestMain {

public static void main(String[] args) {

//Scenario- 1
X x=new X();
System.out.println(x.a);
System.out.println(x.b);
System.out.println(x.c);
x.m1();
x.m2();
x.m3();

//Scenario-2
Y y = new Y();
System.out.println(y.a);
System.out.println(y.b);
System.out.println(y.c);
y.m1();
y.m2();
y.m3();

//Scenario-3
X x = new Y();
System.out.println(x.a);
System.out.println(x.b);
//System.out.println(x.c);
x.m1();
x.m2();
//x.m3();

//Scenario-4 (Note 3rd and 4th scenario are same)


X x = new X();
Y y = new Y();
x = y;
System.out.println(x.a);
System.out.println(x.b);
System.out.println(x.c);
x.m1();
x.m2();
x.m3();
//Scenario-5- Note- this is equivalent to 2nd
scenario
X x = new Y();
Y y = new Y();
y = (Y) x;
System.out.println(y.a);
System.out.println(y.b);
System.out.println(y.c);
y.m1();
y.m2();
y.m3();

//Scenario-6
Y y= new X();
}
}
There the five types of inheritance as below

1. Single inheritance
2. Multilevel inheritance
3. Hierarchical inheritance
4. Multiple inheritance
5. Hybrid inheritance

1. Simple or Single inheritance


In this only one super class and only one sub class called as single.

package com.inheritance;
public class Insurance {

void getInsuranceDetails() {
System.out.println("this is insurance details..");
}
}

package com.inheritance;

public class HealthInsurance extends Insurance {

void getHealthInsuranceDetails() {
System.out.println("this is health insurance details.");
}
}

package com.inheritance;

public class TestMain {

public static void main(String[] args) {

HealthInsurance healthInsurance = new HealthInsurance();


healthInsurance.getInsuranceDetails();
healthInsurance.getHealthInsuranceDetails();
}
}

Output
this is insurance details..
this is health insurance details.

2. Multilevel inheritance
It has only one base class and multiple derived class called as
multilevel. Or it refers to the concept of one class extending (Or inherits)
more than one base class.

package com.inheritance;

public class Account {

void getAccountDetails() {
System.out.println("this is account details..");
}
}

package com.inheritance;

public class CurrentAccount extends Account {

void getCurrentAccountDetails() {
System.out.println("this is current account details");
}
}

package com.inheritance;

public class SavingAccount extends CurrentAccount {

void getSavingAccountDetails() {
System.out.println("this is saving account details");
}
}

package com.inheritance;

public class TestMain {

public static void main(String[] args) {

SavingAccount savingAccount = new SavingAccount();


savingAccount.getAccountDetails();
savingAccount.getCurrentAccountDetails();
savingAccount.getSavingAccountDetails();
}
}

Output
this is account details..
this is current account details
this is saving account details

3. Hierarchical inheritance
One class is inherited by many sub classes called as.
package com.inheritance;

public class Loan {

void getLoanDetails() {
System.out.println("this is loan details");
}
}

package com.inheritance;

public class HomeLoan extends Loan {

void getHomeLoanDetails() {
System.out.println("this is home loan details..");
}
}

package com.inheritance;

public class PersonalLoan extends Loan {

void getPersonalLoanDetails() {
System.out.println("this is personal loan details");
}
}

package com.inheritance;

public class CarLoan extends Loan {


void getCarLoanDetails() {
System.out.println("this is car loan details.");
}
}

package com.inheritance;

public class TestMain {

public static void main(String[] args) {


HomeLoan homeLoan = new HomeLoan();
CarLoan carLoan = new CarLoan();
PersonalLoan personalLoan = new PersonalLoan();
homeLoan.getHomeLoanDetails();
carLoan.getCarLoanDetails();
personalLoan.getPersonalLoanDetails();
}
}

Output
this is home loan details..
this is car loan details.
this is personal loan details

4. Multiple inheritance
One class has many super classes called as multiple inheritance.

Why multiple inheritance not supported in java in case of classes?

Class base has test () method and class derived has also test () method.
Class test extends Base, Derived, which test method It will called, so it
create the ambiguity so that’s why multiple inheritance does not supports in
java.
package com.multiple.inheritance;

public class A {

void m1() {

}
}

package com.multiple.inheritance;

public class B {

void m1() {

}
}

package com.multiple.inheritance;

class C extends A,B {

public static void main(String[] args) {

C c= new C();
c.m1();
}
}

Note- it will get the compile time error.


5. Hybrid inheritance
It is the combination of single and multiple inheritance. So it is not
allowed in java.

Aggregation (Has Relationship)

If class has entity reference, it is known as Aggregation. It represents Has-A


relationship.

Example-

package com.test;

public class Employee {

private int id;


private String firstName;
private String lastName;
private String mobileNumber;

private Address address;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

public String getMobileNumber() {


return mobileNumber;
}

public void setMobileNumber(String mobileNumber) {


this.mobileNumber = mobileNumber;
}

public Address getAddress() {


return address;
}

public void setAddress(Address address) {


this.address = address;
}

// getter and setter


}
package com.test;

public class Address {

private String streetNo;


private String city;
private String state;
private String country;

public String getStreetNo() {


return streetNo;
}

public void setStreetNo(String streetNo) {


this.streetNo = streetNo;
}
public String getCity() {
return city;
}

public void setCity(String city) {


this.city = city;
}

public String getState() {


return state;
}

public void setState(String state) {


this.state = state;
}

public String getCountry() {


return country;
}

public void setCountry(String country) {


this.country = country;
}

}
package com.test;

import java.util.Scanner;

public class Test {

public void getUserDetails() {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter employee id>>");
int id = scanner.nextInt();
System.out.println("Enter employee first name>>");
String firstName = scanner.next();
System.out.println("Enter employee last name>>");
String lastName = scanner.next();
System.out.println("Enter employee mobile number>>");
String mobileNumber = scanner.next();

System.out.println("Enter street no>>");


String streetNo = scanner.next();
System.out.println("Enter city>>");
String city = scanner.next();
System.out.println("Enter state>>");
String state = scanner.next();
System.out.println("Enter country>>");
String country = scanner.next();
// set the value into employee object here
Employee employee = new Employee();
employee.setId(id);
employee.setFirstName(firstName);
employee.setLastName(lastName);
employee.setMobileNumber(mobileNumber);

// set value into address object here


Address address = new Address();
address.setStreetNo(streetNo);
address.setCity(city);
address.setState(state);
address.setCountry(country);

//set address object into employee object


employee.setAddress(address);

// get the value from employee object here


System.out.println("Employee ID>>" + employee.getId());
System.out.println("Employee First Name>>" +
employee.getFirstName());
System.out.println("Employee Last Name>>" +
employee.getLastName());
System.out.println("Employee Mobile Number>>" +
employee.getMobileNumber());

System.out.println("Employee Street No>>" +


employee.getAddress().getStreetNo());
System.out.println("Employee City>>" +
employee.getAddress().getCity());
System.out.println("Employee State>>" +
employee.getAddress().getState());
System.out.println("Employee Country>>" +
employee.getAddress().getCountry());

public static void main(String[] args) {

Test test = new Test();


test.getUserDetails();
}
}
Output
Enter employee id>>
10
Enter employee first name>>
Ajay
Enter employee last name>>
pawar
Enter employee mobile number>>
8595958575
Enter street no>>
3
Enter city>>
pune
Enter state>>
maharashtra
Enter country>>
india
Employee ID>>10
Employee First Name>>Ajay
Employee Last Name>>pawar
Employee Mobile Number>>8595958575
Employee Street No>>3
Employee City>>pune
Employee State>>maharashtra
Employee Country>>india
Polymorphism-

One entity that behaves differently in different cases called as polymorphism.

Example-

1-Light button, we are using that button to on or off the lights.

2. a person acts as an employee in the office, a customer in the shopping mall, a passenger
in bus/train, a student in school, and a son at home.

3. Smartphone is entity that behaves different such as text message, calling, send mail,
video call etc.

How to achieve polymorphism in java?

We can achieve polymorphism by using two ways.

1. Method overloading-
2. Method overriding-

1. Method overloading-
It is the same method name with different argument called as Method overloading.
There is no need of super and sub class relationship.
It is called as early binding, compile time polymorphism or static binding.

Rules-

Method name must be same.


Parameter or argument must be different.
Return type is anything
Access specifier is anything

Example-1

package com.tests;

public class TestMain {

void add(int a, int b) {


System.out.println(a + b);

void add(double a, double b) {


System.out.println(a + b);
}

void add(double a) {
System.out.println(a);
}

void add(int a, int b, int c) {


System.out.println(a + b + c);
}
}

package com.tests;

public class ExampleMain {

public static void main(String[] args) {

TestMain testmain = new TestMain();


testmain.add(10.5);
testmain.add(10.5, 11.5);
testmain.add(2, 4);
testmain.add(5, 10, 15);
}
}

Output is

10.5
22.0
6
30
Why?

Suppose we got the business requirement from the client in last year

Class Employee {

void addStudent (String firstname, String lastname, String city) {

End user is calling the class as below

//End User 1

Employee employee=new Employee ();

employee. addStudent (“ram”,” pawar”,” Pune”);

//End User 2

employee. addStudent (“ram”,” deshmukh”,” Mumbai”);

After that I got the new requirement from the client in current year, to update the
pan card details.

What options we have in this case?

1. Modified field/Attributes/Variable into the existing method.

2. Design new method and add new parameter into it.

First way modifying attributes/field/variable into existing method is not good


approach, it will increase testing of this class. If we are making the changes into
existing method, then how end user calls the method I mean they need to add one
more extra field/variable/attributes, in future again, you got requirement to add one
more field/variable/attributes, so every time user need to change at their side, this is
not the good thing.

Second way, design the same method in that class and add the new field into it. If
client second want pan card details so he can call that method otherwise calls the
first method if pan card is not required.

Example- 2

package com.poly;

public class A{

void test(Object object) {


System.out.println("test- Object");
}
void test(String string) {
System.out.println("test- String");
}

public static void main(String[] args) {


A a = new A();
a.test(new Object());
a.test("ram");
a.test(new A());
a.test(new String());
}
}

test- Object
test- String
test- Object
test- String

Why it is called as compile time polymorphism?

Because it is decided at compile time which one method should get called that’s why it is
called as compile time polymorphism.

2. Method overriding-
It is the same method name with same argument called as method overriding.

There is need of super and sub relationship.

It is called as late binding, run time polymorphism or dynamic binding. Dynamic

Method dispatch etc.

Rules-

Method name must be same.


Return type must be same or different.
Access specifier is anything.
Parameters must be same.

Note- we can extend the method scope in overriding but not reduce the visibility of
it.

Why?

Maintainability
Readability of code.

Example-

package com.override.demo;

public class A {

void m1() {
System.out.println("class - A- m1 () method");
}

package com.override.demo;

public class B extends A {

@Override
void m1() {
System.out.println("class - B- m1 () method");
}

void m7() {
System.out.println("class- B- m7() method");
}
}

package com.override.demo;

public class TestMain {

public static void main(String[] args) {

B b= new B();
b.m1();
b.m7();

}
}

Output-

class - B- m1 () method
class- B- m7() method
Program Explaination-

 In the above program, B is implementing the method m1 () with the same signature

as super class A i.e m1 () of class B is overriding m1() of class A.

 If you want to add new features(variable or method) to existing class, then you

should not disturb the existing class. You should always write the subclass of that

class that is the best practice.

Note-Why we need to write the sub class

1. To add the new features


2. To inherit the existing functionality.

Subclass method's access modifier must be the same or higher than the superclass method
access modifier

superclass In subclass, we can have access specifier


public Public
protected protected, public
default default, protected, public
private We cannot override the private

Method Overloading- Live Example-1

Class MobilePattern{

void getMobilePattern(Thumb thumb){

//logic here

void getMobilePattern(int number){


//logic here

void getMobilePattern(int x1, int y1, int x2, int y2){

//logic here

Live Example-2

Class Banking{

void getBanking(CreditCard creditCard){

//logic here

void getBanking(Netbanking netBanking){

//logic here

void getBanking(DebitCard debitCard){

//logic here
}

void getBanking(UPI upi){

//logic here

Method Overriding- Live Example-1

Class SBI {

void getSimpleIntereset(float simpleRate){

//logic here

Class Axis extends SBI{

void getSimpleIntereset(float simpleRate){

//logic here

Class HDFC extends Axis {


void getSimpleIntereset(float simpleRate){

//logic here

Live Example-2

Class FirstTier {

void getSeatAvailability(int seat){

//logic here

Class SecondTier extends FirstTier{

void getSeatAvailability(int seat){

//logic here

Class ThirdTier extends SecondTier {

void getSeatAvailability(int seat){

//logic here

}
}
Assignment

1) Explain OOPs concept ? And where have you used all 4 of them in your framework.
Answer: OOP : Object Oriented Programming : It is to design a program using classes and
objects.
Object : Object has state and behavior. State has some Identification. Behavior means some
action of that state.
Example : A dog is an object because it has states like color, name, breed, etc. as well as
behaviors like wagging the tail, barking, eating, etc.Object is actually derived something from
class.
Class : it is the blueprint on the basis of which the object is created. Object is inheriting the
properties of class. Object is created from class.
Example : JsonPath Jsp = new JsonPath();
Jsonpath() : Class
Jsp : Object
Here , all the method in JsonPath() class are come in Jsp Object
Where have you used all 4 of them in your framework.
 Inheritance The mechanism in Java by which one class acquires the properties
(instance variables) and functionalities of another class is known as Inheritance.
 Polymorphism Polymorphism allows us to perform a task in multiple ways.

 Encapsulation All the classes in a framework are an example of Encapsulation.


 Abstraction Abstraction is the methodology of hiding the implementation of internal
details and showing the functionality to the users.

2) Explain the difference between JVM, JDK and JRE


Answer:
JRE : JRE stands for “Java Runtime Environment”. It comprises of the JVM (Java Virtual
Machine), Java platform classes, and supporting libraries.
Using JRE, we can only execute already developed applications. We cannot develop new
applications or modify existing applications.
JRE only provides Runtime Environment.
JDK : JDK stands for Java Development Kit. It is a superset of JRE (Java Runtime
Environment).
Using JDK, we can develop, compile and execute (run) new applications and also we can
modify existing applications. JDK includes JRE and development tools (environment to
develop, debug and monitor Java programs).
JVM : JVM stands for Java Virtual Machine. JVM drives the java code. Using JVM, we can
run java byte code by converting them into current OS machine language. It makes Java to
become a portable language (write once, run anywhere)
What is JIT Code : The JIT compiler helps improve the performance of Java programs by
compiling bytecodes into native machine code at run time.
3) Explain multiple , single and multi-level inheritance.
Answer: Single Inheritance : One Parent class is directly inherited by child class
Multi-level Inheritance : One Parent class is inherited by child class another inherited another
child class.
Multiple Inheritance : Two Parent class and one child class.

4) Types of polymorphism?
Answer: Polymorphism allows us to perform a task in multiple ways. the word Polymorphism
‘Poly’ means ‘Many’ and ‘Morphos’ means ‘Shapes’.
Types of Polymorphism
 Method Overloading
 Method Overriding
5) Method overloading and how to implement it ?
Answer: In one class two method name are same name are known as “Method Overloading”.
Why need of Method overloading – when we have two different arguments.
When we want different output from same method (Input).
For eg. public class Soap
{
Public static void display (){}
Public static void display (int a){} // pass argument
}
Need for overloading: Function is same but depending on argument your output result is
change. This is used when different result from argument.
6) Method overriding and how to implement it ?
Answer: Method Overriding
 When we want to enhance or change the functionality of inherited object to achieve
desired results.
 To implement this method definition should be same.
 Using overriding we can only enhance the properties of class. We cannot reduce
properties.
7) What points should be taken care of while overriding a method .
Answer:
 To implement this method definition should be same.
 Method definition= method name, argument, return type.

8) Use of final keyword.


Answer: Using final keyword we prevent Inheritance and Overriding(type of Polymorphism).

9) Explain different access modifiers supported by JAVA.


Answer: Access modifiers are keywords used to control the visibility of fields, methods, and
constructors in a class.
Four Types of Access Modifiers
Private: We can access the private modifier only within the same class and not from outside
the class.
Default: We can access the default modifier only within the same package and not from
outside the package.
Protected: We can access the protected modifier within the same package and also from
outside the package with the help of the child class.
Public: We can access the public modifier from anywhere. We can access public modifiers
from within the class as well as from outside the class and also within the package and outside
the package.
10) What is JRE System library ?
Answer: JRE System Library implements the Java API in Eclipse IDE. So all the predefined
functions of Java, can be accessed by the Java Programs written in Eclipse IDE because of this
JRE System Library.
11) What is external Library , how to add them and name some external libraries you have used
in your framework.
Answer: External library are imported from outside so that we can perform specific job.
From maven repository we can download them. It is added as dependencies outside the built
in porm.xml file. That are testNg, RestAssured, ApachePIO.
Topic Name

Encapsulation
Inheritance
Polymorphism
Abstraction
Object class
Wrapper class
final Keywords
Questions

What is abstract class and interface

Clone method elaboration (deep and shallow)

can we extend interface into interface

Explain Oops concepts ?

What is Object class

Final finally and finalize

What is Abstraction? When to use abstract class n when to use Interface?

Class inheritance concept

How to prevent any other class to extend our class

Method overriding and overloading

OOPS Concepts, Abstract Class And Interface, Encapsulation

What is mean by compile time polymorphism and runtime

Different methods present in Object class

When to use abstract and interface

Why to make class as final

Difference between normal class and interface

What is wrapper classes

Opps concept like different between abtract class and interface, and what is sealed class
, What is constructed, what is oppoclass, types of collection, what polymorphism, What
is data abstraction,

What is difference between Primitive data type and wrapper classes?

Logic for accessing multiple inheritance

String == comparator and equlas method

I have 3 different Implementations, at run time I need to pick up the object dynamically
and should to appropriate implementation.

types of inheritance

Multiple inheritance

What is final keyword?


What is finalized?

what is finallise and finally

Scenario of method overriding?

how can we override finallise

You have 5 object and its hashcode is same then what problem in your program

Deep cloning and shallow cloning

difference between Setter and Getter method

What is abstraction n how to achieve?

Composition and inheritance difference

Aggregation

Why interfaces are you used

overloading vs overidding

diff abstract class and interface

what is exception how handle it??

What is finally block

Can we overide static method?if no why we are not able to override...

Tell me oops concept all Piller with example

Example of hierarchical inheritance

OOPs Piller

Single Inheritance

Hashcode method

Types of polymorphism

Wrapper class

Difference between abstract class and interface

Difference between wrapper class and data types

What is polymorphism?

What is interface n abstract class?

Interface and abstrat class diff

What is multiple inheritence

Can we override static methods?

contract of equals and hashcode

final class

explains pillars of OOPs with examples.


is multiple inheritance in supported in Java if no why

what is interface

Encapsulation

Method overloading & overridingf

example of overriding

diff between runtime polyphorphisam and overriding

Oops concepts

method overriding

can we override static method

difference between abstract class and interface.

Your class has private construtor and private variables

method is overriden with that method

how to access that elements without getter and setter

Can we override hashcode method? how and if yes, which

What is marker interface ,why we use it

what is marker interface? If marker interface is empty then why marker interface we
used in java

What is opps

Overloading

Wrapper class

What is final keyword

What is encapsulation

What is polymorphism

in method overriding,if chlid class method throws exception and super class method does
not throws exception ,so is it method overriding or not

what happen if we override super class static method

coverihant return type

contract betn hashcode and equals method

what happened if we override equals method and forget to override hashcode method in
hashmap and what happened in opposite case

"what is cloneable interface

what is drawback of shallow copy

Method overload vs override scenario q

Concrete method?
Output of method overloading with string parameter and object parameter and passing
value null(ambiguous)

Output of interface having same method and implementing both with same class

Diamond issue(Multiple inheritance)

can interface really solves problem of multiple interface

How to make a copy of objects?

What is method overloading and varios scenerios of it.

what is solid principle?

What is Runtime polymorphism? Live example

If we make class as private what wil happen? Scenario -

private class Example{ }

public final class Example{ }

public static class Example{ }

Difference between class and interface?

How we can achieve multiple inheritance using interface?

What is the purpose of introducing default and static method in java?

Difference between static and final

Can we declare an interface as final

Can we override overloaded method?

garbage collection...how we can do it explicitly...

Difference betwenn == and equals()?

how to achieve encapsulation and abstraction

"parent class have public method and we override into child class

with private access modifier is it possible??"

method overloading? and overriding which is compile time and whichi is runtime??

What is the purpose of marker interface if we don’t have method in it?

why method overloading is not possible by changing return type

what is alternative for multiple inheritance

By how many methods we can create object

Diffrence between pass by value and pass by refence

what is upcasting

dynamic dispatch advantage


what is memory management and how the memory is garbage collected in java

what is Object-oriented analysis and design (OOAD)

How to prevent any other class to extend our class

What is overriding and cross questions on it

What is encapsulation realtime Example

Difference between abstraction and encapsulation

What is immutable class.

Join both get employee name and address

Write a code for method overriding.

Scenario on dynamic dispatch.

Static method overriding scenario.

What is Encapsulation how to achieve it?

What is immutable. Design immutable class.

what is multiple inheritance and what will happen if we tried it with class?

difference between method overloading ans ovveriding?

can we ovveride the static method?

when we use final variable?

Dynamic method dispatch

Method overloading and method overriding, its scanario.

data hiding & use of getter & setter

Inheritance scenario.

What is Abstraction.

How Multiple inheritance is supported in case of interfaces?

inhertance scenario child parent classes

Why need static method in interface

What is diff bet interface and abstract class?

Is method overloading depends on return type?

How can we write main method as final?

inheritance scenario they give u

final and static keywords

custom immutable class

Method overloading overiding real time example


what is meant by hashcode

What are the different methods in object class?

what is method signature

tell me abstraction in details.... how you implements it in your project

Diff Interface vs Abstract

What is aggregation

diff between aggregation and composition.

explain oops concepts and scenario type question.

why method overloading is compile time polymophism and method

what is mean by static binding?

can we make object final

how multiple inheritance is possible in case of interface. Write program for that

java is defined by reference or by value

what exactly checks == & .equals method

What is abstraction

Methods in object cls

Use of equals method in object class

write a program to override.

difference between Abstraction and interface

write a program for implementing an inferace.

how to create immutable class

which is default immutable class in java

s1==s2 & s1. equals s2 difference

Overloading vs Overriding

Explain Inheritance

Why not Multiple Inheritance

Abstract vs Final method

Method overriding and method overloading

What is final in java

Inheritance

Implements

Extends vs Implements

advantages of overloading
why multiple inheritance not allowed? Reson

Oops concepts in detail

Why polymorphism

Why abstraction

Real life scenarios

Why run time and compile time

Extends keyword use

what is autoboxing and unboxing

You might also like