Oops
Oops
Oops
-> We have to override abstract methods in child class, otherwise child class
becomes abstract automatically.
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.
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.
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.
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();
}
}
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.
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
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.
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.
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:-
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.