Java All
Java All
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
1. Abstract class
2. Interface
Abstract class
To use abstract method of class, we should extends the abstract class and
abstract.
abstract
Note- Multiple inheritances are not allowed in abstract class but allowed in
interfaces
Example-1
package com.abstraction;
}
Here, method is the abstract then class should be abstract only as per below
example
package com.abstraction;
Example-2- we can write multiple abstract method into abstract class as per below
package com.abstraction;
We need to create the class which extends from abstract class as shown in
below.
package com.abstraction;
@Override
void x1() {
System.out.println("x1 method..");
}
@Override
void x2() {
System.out.println("x2 method..");
}
}
package com.abstraction;
public class TestMain {
}
}
Note- Suppose in the sub class, I don’t want to override the abstract methods then
make that subclass as abstract.
Interface-
5. In interface, we can just define the method only but implemented those
methods into implemented class.
7. In 1.8 Declare the default & static method with body in interface.
9. Java supports multiple inheritance in the terms of interfaces but not classes.
interface interface_name {
Example-1
public interface A {
public abstract void x1(); // 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;
@Override
public void x1() {
System.out.println("Test-x1 method");
}
package com.abstraction;
Output
Test-x1 method
Example-4
package com.abstraction;
public interface A {
package com.abstraction;
public interface B {
package com.abstraction;
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;
@Override
public void x1() {
System.out.println("Test-x1 method");
}
package com.abstraction;
package com.abstraction;
Below are the list of possible scenario regarding the interface and
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.
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 {
//logic of withdraw
ti.withdrawAmt(500);
Example-
Class Employee {
int salary;
void m5() {
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 ?
e1.salary=5000; //case 1
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.
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.
package com.encapsulation;
package com.encapsulation;
package com.encapsulation;
import java.util.Scanner;
System.out.println("Employee Id>>" +
employee.getEmployeeId());
System.out.println("Employee Name>>" +
employee.getEmployeeName());
System.out.println("Employee City>>" +
employee.getEmployeeCity());
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
Example- IS Relationship
Class Policy {
Note-
All the parent members are derived into child class but they are depending upon
the below two condition as
UML Diagram-
Parent -P
Child – C
Note- Below are different names for super and sub class
Note-
When to use?
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-
Class Parent {
String firstname;
String lastname;
String address;
String city;
String mobilenumber;
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 {
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;
//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-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
package com.inheritance;
public class Insurance {
void getInsuranceDetails() {
System.out.println("this is insurance details..");
}
}
package com.inheritance;
void getHealthInsuranceDetails() {
System.out.println("this is health insurance details.");
}
}
package com.inheritance;
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;
void getAccountDetails() {
System.out.println("this is account details..");
}
}
package com.inheritance;
void getCurrentAccountDetails() {
System.out.println("this is current account details");
}
}
package com.inheritance;
void getSavingAccountDetails() {
System.out.println("this is saving account details");
}
}
package com.inheritance;
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;
void getLoanDetails() {
System.out.println("this is loan details");
}
}
package com.inheritance;
void getHomeLoanDetails() {
System.out.println("this is home loan details..");
}
}
package com.inheritance;
void getPersonalLoanDetails() {
System.out.println("this is personal loan details");
}
}
package com.inheritance;
package com.inheritance;
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.
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;
C c= new C();
c.m1();
}
}
Example-
package com.test;
}
package com.test;
import java.util.Scanner;
Example-
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.
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-
Example-1
package com.tests;
void add(double a) {
System.out.println(a);
}
package com.tests;
Output is
10.5
22.0
6
30
Why?
Suppose we got the business requirement from the client in last year
Class Employee {
//End User 1
//End User 2
After that I got the new requirement from the client in current year, to update the
pan card details.
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{
test- Object
test- String
test- Object
test- String
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.
Rules-
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;
@Override
void m1() {
System.out.println("class - B- m1 () method");
}
void m7() {
System.out.println("class- B- m7() method");
}
}
package com.override.demo;
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
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
Subclass method's access modifier must be the same or higher than the superclass method
access modifier
Class MobilePattern{
//logic here
//logic here
Live Example-2
Class Banking{
//logic here
//logic here
//logic here
}
//logic here
Class SBI {
//logic here
//logic here
//logic here
Live Example-2
Class FirstTier {
//logic here
//logic here
//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.
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.
Encapsulation
Inheritance
Polymorphism
Abstraction
Object class
Wrapper class
final Keywords
Questions
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,
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
You have 5 object and its hashcode is same then what problem in your program
Aggregation
overloading vs overidding
OOPs Piller
Single Inheritance
Hashcode method
Types of polymorphism
Wrapper class
What is polymorphism?
final class
what is interface
Encapsulation
example of overriding
Oops concepts
method overriding
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 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 happened if we override equals method and forget to override hashcode method in
hashmap and what happened in opposite case
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
"parent class have public method and we override into child class
method overloading? and overriding which is compile time and whichi is runtime??
what is upcasting
what is multiple inheritance and what will happen if we tried it with class?
Inheritance scenario.
What is Abstraction.
What is aggregation
how multiple inheritance is possible in case of interface. Write program for that
What is abstraction
Overloading vs Overriding
Explain Inheritance
Inheritance
Implements
Extends vs Implements
advantages of overloading
why multiple inheritance not allowed? Reson
Why polymorphism
Why abstraction