Oops

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Abstractaction

-> In java abstraction is a concept, where we can hide our


implementation.
->it can be achieved in two ways, abstract class , interface.
->we can create reference of abstract class.
-> In java abstraction can be achieved by using abstract class .
-> In java abstract class means incomplete class.
-> Abstract class considered as 0 to 100 implemented class.
-> Here abstract means incomplete or unimplemented.
-> Incomplete or unimplemented method should be declared with abstract
keyword.
-> If class contains any abstract method, class becomes abstract
automatically, and in java we can not create objects of abstract class.

-> To prevent object creation, we can declare a class as an abstract.


To use abstract class we can extends in other class.

-> Abstract class can be use as IS-A relationship.

-> We have to override abstract methods in child class, otherwise child class
becomes abstract automatically.

Q1. What is an abstract class in Java, and how is it different


from a regular class?

Ans:-
Abstraction is a fundamental OOP concept in Java that allows you to define a class
with a set of methods and properties without specifying their implementation
details. It hides the internal complexity of a class, exposing only what is necessary
for external use.

Q2. What is an abstract method, and how is it declared in an abstract class?

Answer:-
An abstract method is a method declared in an abstract class that has no
implementation in the abstract class itself. It is declared using the “abstract”
keyword and ends with a semicolon, without any method body. Subclasses of the
abstract class must provide implementations for all the abstract methods.
Q3.Can an abstract class have constructors? If so, how do they work?

Yes, abstract classes in Java can have constructors, and they work similarly to
constructors in regular classes.

Q4.Why do we use abstract classes and methods in Java?

Answer:-
We use abstract classes and methods to create a common interface or blueprint
for a group of related classes. Abstract classes allow us to define methods that
must be implemented by subclasses, ensuring a consistent interface while allowing
for variations in behavior. They also prevent instances of the abstract class from
being created, which is useful when we want to enforce certain behavior in
subclasses.

abstract class animal {


animal() {
System.out.println("animal constructor every day eat");
}
public abstract void sound();
}
class dog extends animal {

public void sound() {


System.out.println("dogs are barking");
}

class lion extends animal {


public void sound() {
System.out.println("lines are Roar");
}

public class oops1abstract {


public static void main(String[] args) {

// dog d1 = new dog();


lion l1 = new lion();
l1.sound();
INTERFACE
Interface is just like a class , which contains abstract
methods. And we can not create objects of interface.
->To create interface we can use interface keyword
->Interface contains all methods are abstract and public by default.
->Interface considered as 0 implemented class
->We cannot create object of interface.
->To use interface we can use implements keyword and we have to override
all abstract methods into child class or child class become
abstract implicitly.

->All the variable in interface are public and final by default .


->Interface itself is public by default.
->Interface can extends another interface.
->Class can implements multiple interface.
->Class can not extends interface.
->Interface can not implements another interface.
->Interface cannot extends a class.

Interface Demo1{
int a = 10 , b = 20;
void m1();
}
Class Demo2 implements Demo1
{
Public void m1()
{
System.out.println(“m1 method of Demo1”);
}
}
Class MainClass
{
public static void main(String args[])
{
Demo2 d2 = new Demo2();
d2.m1(); // m1 method of Demo1
}
}

Usage of Interface
1. To achieve multiple inheritance
2. To provide runtime polymorphism
3. To achieve loosely couple code

Interface Car
{
int maxSpeed = 110;
void colour();
void type();
void cc();
}
Class BmwCar implements Car
{
int minSpeed = 60;
public void colour()
{
SOP(“BMW car is only available in black, white and grey colour”);
}
Public void type()
{
SOP(“BMW car is only for family and business type car”);
}
public void cc()
{
SOP(“BMW car available in 2000 cc to 5000 cc”);
SOP(“minSpeed = ” + minSpeed + “maxSpeed” + maxSpeed);
}
}
Class MainClass
{
public static void main(String args[])
{
Car c = new BmwCar();
c.colour();
c.type();
c.cc();
}
}

Different between abstract and interface in pg 90.

INHERITANCE
->In java inheritance is a concept, where we can reuse our code.
->By using inheritance, we can achieve reusability concept.
->By using inheritance, we can remove code complexity.
->In inheritance a class, which provides properties to other class,consider as
parent class.
->In inheritance a class ,which acquires properties from another
consider as child class.
->To perform inheritance we can use extends keyword.
->Inheritance also known as IS-A relationship.
-> To prevent inheritance we can declare a class as final.

​ What is inheritance in Java?


● Answer: Inheritance is a fundamental concept in Java's
object-oriented programming (OOP), where a new class (subclass)
can inherit properties and behaviours (fields and methods) from an
existing class (superclass).
​ Explain the "is-a" relationship in the context of inheritance.
● Answer: The "is-a" relationship is a fundamental principle of
inheritance. It states that a subclass is a specialized version of its
superclass, implying that objects of the subclass can be treated as
objects of the superclass. For example, a Car is-a Vehicle.
​ What keyword is used to establish inheritance in Java?
● Answer: The extends keyword is used to establish inheritance in Java.
It is used in the subclass declaration to specify which superclass it
inherits from.
​ Can a subclass inherit private members (fields and methods) from its
superclass?
● Answer: No, a subclass cannot inherit private members from its
superclass. Private members are not accessible in subclasses.

What is constructor chaining in inheritance?

● Answer: Constructor chaining is the process of calling a constructor of


the superclass from the constructor of the subclass using super(). It
allows the subclass to initialize inherited fields or perform common
setup.
​ Explain the super keyword in Java and how it's used in inheritance.
● Answer: The super keyword is used to refer to the superclass from
within a subclass. It can be used to access superclass members and
call superclass constructors. For example, super() is used to call a
superclass constructor.
​ What is the difference between super and this in Java?
● Answer: super is used to refer to the superclass, while this is used to
refer to the current instance of the class. super is often used to
access superclass members and constructors, whereas this is used
to refer to instance members and methods of the current class.
​ Can a class inherit from multiple classes in Java?
● Answer: No, Java does not support multiple inheritance for classes,
meaning a class can extend only one superclass. However, a class can
implement multiple interfaces, achieving a form of multiple
inheritance through interface implementation.

Types of inheritance:-

1.Single Inheritance
2.Multilevel Inheritance
3.Hierarchical Inheritance
4.Hybrid Inheritance:

METHOD OVERLOADING:-
Method overloading is a feature in Java that allows multiple methods with
the same name to be defined in a class, as long as they have different
signature. This allows for more flexible and expressive code, as different
versions of the same method can be used depending on the types of
arguments passed to it.

CONSTRUCTOR OVERLOADING

Constructor overloading in Java is the process of defining multiple


constructors in a class, each with a different set of parameters. This allows
objects of the class to be initialised in different ways depending on
the needs of the program.
To overload a constructor, we simply define a new constructor with a
different set of parameters. The constructors can have different numbers
and types of parameters.

Method Overriding
Method overriding is a feature in object-oriented programming that allows a
subclass to provide its own implementation of a method that is already defined
in its superclass. This is done by declaring a method in the subclass with the
same name, return type, and parameter list as the method in the superclass.

->Here according to object method can be execute, hence method


overriding can be considered as runtime polymorphism.

->Method overriding is useful for developers (because they don’t


need to make new methods on the basis of users, they just have to
make slight change in the pre-define method.)
->To prevent method overriding we can declare a method as a final.

ENCAPSULATION
It is a mechanism through which we can wrapping the data members and
member methods of class in a single unit called encapsulation .
-> In java Encapsulation is a concept where we hide our data.
-> To hide a data, we can declare variable as a private.
-> To set private data, we can create Setter method .
-> To get private data, we can create Getter method.
-> To represent encapsulated class, we can override tostring() method of
object class.
-> Encapsulation also known as Wrapping of data.
-> Encapsulation also known as 100% concrete class.
-> Encapsulation can be use to send at data one end to others.
-> Encapsulation also known model class.
-> Encapsulation is a concept where we convent different data into single
Entity by overriding tostring() method of object class.

​ What is encapsulation in Java?


● Answer: Encapsulation is one of the four fundamental OOP concepts,
and it refers to the practice of bundling the data (fields) and methods
(functions) that operate on that data into a single unit called a class. It
restricts direct access to an object's data and ensures data integrity.
​ Why is encapsulation important in Java?
● Answer: Encapsulation provides several benefits, including data hiding,
abstraction, and controlled access. It helps maintain the integrity of an
object's state by preventing unauthorized modification of its data, and it
allows for easier maintenance and code evolution.
​ How is encapsulation achieved in Java?
● Answer: Encapsulation in Java is achieved by declaring the instance
variables of a class as private and providing public getter and setter
methods to access and modify those variables.
​ What are getter and setter methods in Java, and why are they used in
encapsulation?
● Answer: Getter methods (also known as accessor methods) are used
to retrieve the values of private instance variables, while setter
methods (mutator methods) are used to modify the values of those
variables. They allow controlled access to an object's data and help
enforce data validation rules.

What is the role of access modifiers (e.g., private, public, protected) in


encapsulation?
● Answer: Access modifiers determine the level of access to class
members (fields and methods). In encapsulation, instance variables
are typically declared as private to restrict direct access, while getter
and setter methods are declared as public to provide controlled
access.
​ What are the advantages of encapsulation?
● Answer: Encapsulation provides advantages such as data hiding
(preventing unauthorized access), abstraction (hiding implementation
details), code flexibility (allowing changes to the internal
implementation without affecting external code), and improved code
maintainability.
​ Can encapsulation be achieved without using getter and setter methods?
● Answer: While getter and setter methods are a common way to
implement encapsulation, encapsulation can also be achieved by
making instance variables private and providing other methods that
control access to the data or perform data validation.

public class Student {


private String name;
private int age;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getAge() {


return age;
}
public void setAge(int age) {
if (age >= 0) {
this.age = age;
} else {
System.out.println("Age cannot be negative.");
}
}
}

POLYMORPHISM:-
Polymorphism in Java is the ability of an object to take on many forms.
This is achieved through two mechanisms: method overloading and
method overriding.
There are two main types of polymorphism in java:-

1. Compile-time polymorphism: This type of polymorphism is


achieved through method overloading, which allows multiple methods with
the same name but different parameters to be defined in a class. The correct
method to be called is determined at compile time based on the number and
types of arguments passed to it.

2. Runtime polymorphism: This type of polymorphism is achieved through


method overriding, which allows a subclass to provide a specific
implementation of a method that is already defined in its superclass. The
correct implementation of the method to be called is determined at runtime
based on the actual type of the object being used.

Here's an example of method overloading in Java:


public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}

Calculator calculator = new Calculator();


int result1 = calculator.add(2, 3); // calls the first add
method and returns 5
double result2 = calculator.add(2.5, 3.5); // calls the
second add method and returns 6.0
int result3 = calculator.add(2, 3, 4); // calls the third
add method and returns 9

CONSTRUCTOR OVERLOADING
Constructor overloading in Java is the process of defining multiple
constructors in a class, each with a different set of parameters. This allows
objects of the class to be initialized in different ways depending on the needs
of the program.

public class Person {


private String name;
private int age;
public Person() {
this.name = "John Doe";
this.age = 30;
}|
public Person(String name) {
this.name = name;
this.age = 30;
}
public Person(String name, int age){
this.name = name;
}
}
Person person1 = new Person(); // calls the first constructor
Person person2 = new Person("Jane"); // calls the second constructor
Person person3 = new Person("Bob", 25); // calls the third constructor
​ What is polymorphism in Java?
● Answer: Polymorphism is the ability of different classes to be treated
as instances of the same class through a common interface, allowing
objects of different classes to respond to the same method or
message call in a way specific to their individual implementations.
​ What are the two main types of polymorphism in Java, and how do they
differ?
● Answer: The two main types of polymorphism in Java are
compile-time polymorphism (method overloading) and run-time
polymorphism (method overriding). Compile-time polymorphism is
resolved at compile time based on the method signature, while
run-time polymorphism is determined at runtime based on the object's
actual type.

What is method overriding, and when is it used?

● Answer: Method overriding is run-time polymorphism that occurs


when a subclass provides a specific implementation for a method that
is already defined in its superclass. It is used to customize or extend
the behavior of inherited methods in a subclass.
​ Explain the concept of dynamic method dispatch in Java.
● Answer: Dynamic method dispatch is a mechanism in Java that
determines which method to call at runtime based on the actual object
type rather than the reference type. It enables polymorphic behavior
and is achieved through method overriding.

How does polymorphism contribute to code reusability and flexibility in Java?


● Answer: Polymorphism allows you to write generic code that can work with
objects of different classes as long as they adhere to a common interface or
inheritance hierarchy. This promotes code reusability, flexibility, and
extensibility.

You might also like