CoreJava-6 - OOP - Inheritance
Core Java - OOP - Inheritance
Object-oriented programming (OOP) is a programming paradigm
that is based on the concept of objects, which can contain data and code to
manipulate that data. In OOP, the focus is on creating objects that interact
with each other to perform tasks, rather than on procedures and functions.
The key principles of OOP include:
- Encapsulation:
Encapsulation is the practice of hiding the internal details of an object
and providing a public interface for interacting with that object. This
helps to ensure that the object is used correctly and prevents external
code from accessing or modifying its internal state directly.
- Inheritance:
Inheritance is the ability of a class to inherit properties (fields and
methods) from a parent class. This allows for code reuse and the creation
of complex class hierarchies.
- Polymorphism:
Polymorphism is the ability of objects of different classes to be treated
as if they are of the same class, using inheritance and interfaces. This
allows for more flexible and dynamic code.
- Abstraction:
Abstraction is the practice of focusing on the essential features of an
object, while ignoring the details that are not relevant to its use. This
helps to simplify complex systems and make them easier to understand.
OOP is a powerful and flexible programming paradigm that is
widely used in modern software development, especially for building large,
complex systems. Java is a popular language for OOP due to its strong support
for the principles of OOP and its rich set of libraries and frameworks that
make it easy to build object-oriented applications.
Advantages of following design principles of OOP
Following the design principles of OOP has several
advantages, including:
- Modularity:
OOP allows for the creation of modular, reusable code using classes and
objects. This makes it easier to maintain and update code over time, as
changes can be made to individual modules without affecting the entire
system.
- Code
Reusability: OOP promotes code reusability, which helps reduce development
time and cost. Once a class is created, it can be reused in other parts of
the application, or even in other applications.
- Encapsulation:
Encapsulation allows for better control over the internal workings of an
object. By hiding the internal details of an object, it becomes easier to
maintain and modify the code without affecting other parts of the system.
- Flexibility:
OOP allows for greater flexibility in software design, as classes can be
easily extended or modified to meet changing requirements. This means that
OOP-based applications can be more easily adapted to changing business
needs or technological advancements.
- Security:
OOP promotes the use of access modifiers, which can be used to limit
access to sensitive data or methods. This helps improve the security of
the application by preventing unauthorized access to critical parts of the
system.
- Maintainability:
OOP makes code easier to maintain by promoting a clear separation of concerns
and reducing code complexity. This means that bugs can be more easily
identified and fixed, and new features can be added without causing
unintended side effects.
Overall, following the design principles of OOP can help
improve code quality, reduce development time, and cost, and make software
systems more flexible and adaptable to changing requirements.
What is Inheritance in Java ?
Inheritance is a fundamental feature of object-oriented
programming (OOP) in Core Java. It allows a class to inherit properties (fields
and methods) from another class, known as the superclass or parent class. The
class that inherits from the superclass is known as the subclass or child
class.
To create a subclass, the extends keyword is used
followed by the name of the superclass.
public class
ChildClass extends ParentClass { // child class definition } |
In addition to inheriting the fields and methods of the
parent class, the child class can also override methods of the parent class by
providing its own implementation. To do this, the child class defines a method
with the same name and signature as the method in the parent class.
public class
ChildClass extends ParentClass { @Override public void someMethod() { // override implementation } } |
In Java, there are four different types of inheritance:
- Single
Inheritance: Single inheritance is the most common type of inheritance
where a class inherits properties from a single parent class. In Java, a
class can extend only one parent class. For example:
public class ChildClass extends
ParentClass {
// child class definition } |
- Multilevel
Inheritance: Multilevel inheritance is a type of inheritance where a class
inherits properties from a parent class, which in turn inherits properties
from another parent class. In Java, this can be achieved by extending a
subclass from another subclass. For example:
public class GrandParentClass {
// grandparent class definition } public class ParentClass extends
GrandParentClass {
// parent class definition } public class ChildClass extends
ParentClass {
// child class definition } |
- Hierarchical
Inheritance: Hierarchical inheritance is a type of inheritance where a
class has multiple child classes extending from a single parent class. In
Java, this can be achieved by defining multiple classes that extend from a
single parent class. For example:
public class ParentClass {
// parent class definition } public class ChildClass1 extends
ParentClass {
// child class 1 definition } public class ChildClass2 extends
ParentClass { // child class 2 definition } |
- Multiple
Inheritance (through Interfaces): Multiple inheritance is a type of
inheritance where a class can inherit properties from multiple parent
classes. In Java, this can be achieved through interfaces. An interface
defines a set of methods that a class must implement. A class can
implement multiple interfaces, thereby inheriting properties from multiple
sources. For example:
public interface Interface1 {
void method1(); } public interface Interface2 {
void method2(); } public class ChildClass implements
Interface1, Interface2 {
// child class definition
public void method1() {
// implementation of method1 from Interface1
}
public void method2() {
// implementation of method2 from Interface2
} } |
It's important to note that Java does not support multiple
inheritance of classes. That is, a class cannot directly inherit from more than
one class. However, it can inherit from multiple interfaces, as shown in the
example above.
What are the advantages of Inheritance?
Inheritance is a key concept in object-oriented programming and
provides several advantages, including:
- Code
reuse: Inheritance allows classes to inherit fields and methods from their
parent classes, which can help reduce the amount of duplicate code in an
application. This can save time and effort during development and help
improve code maintainability.
- Extensibility:
Inheritance also allows for classes to be extended to add new
functionality or behaviour. By inheriting methods and fields from a parent
class, a subclass can add its own unique methods or fields without having
to rewrite code that is already in the parent class.
- Polymorphism:
Inheritance enables polymorphism, which is the ability for objects to be
treated as if they are of the same type, even if they are of different
classes. This can help make code more flexible and adaptable to changing
requirements.
- Hierarchy
and organization: Inheritance can help organize classes into a
hierarchical structure, making it easier to understand and manage complex
code bases.
- Simplification:
Inheritance can simplify code by allowing common functionality to be encapsulated
in a single parent class, rather than being duplicated in multiple
classes. This can make code easier to read and understand and can reduce
the risk of errors and bugs.
Overall, inheritance can help improve code quality, reduce
development time, and cost, and make software systems more flexible and
adaptable to changing requirements.
Write a program to have account number and balance in a class and access these from another class whenever there is a transaction of type deposit or withdrawal , the balance should be updated accordingly and display the current balance.
1. Read
or analyse the problem statement [Manual Test cases - Execute once ]
2. Algorithm
in simple English
3. convert
it into code.
4. Do
a dry run.
5. Add
comments wherever needed.
6. Get
the code reviewed for coverage [ SonarQube ] and optimization.
1. Give
the AccountNumber, Balance, transaction Amount and transaction type.
2. Check
the transaction type.
3. If
the transactionType = deposit, then Add
transaction Amount to the balance
4. If
the transactionType = Withdrawal, then Subtract
transaction Amount from the balance
5. display
the current balance.
package coreJava; public class
InheritanceExampleInput { //
Declaring the inputs for the InheritanceExample class public static int accountNumber = 123456 ; public static int balance ; public static int transactionAmount ; public static
String transactionType = "" ; } |
package coreJava; public class
InheritanceExample extends InheritanceExampleInput { /*This method will take the AccountNumber,Balance,transaction
Amount and transaction type and Check the
transaction type If the
transactionType = deposit, then Add
transaction Amount to the balance If the
transactionType = withdrawal, then
Subtract transaction Amount from the balance Return the
balance*/ int
checkBalance( int balance , int transactionAmount, String
transactionType ) { if(transactionType.equalsIgnoreCase("Deposit")) { balance = balance + transactionAmount; }else if(transactionType.equalsIgnoreCase("Withdrawal")) { balance = balance - transactionAmount; } return balance ; } public static void
main(String[] args) { InheritanceExample
myObj = new InheritanceExample(); // Displaying
the balance balance = 5000; transactionAmount =
50 ; transactionType = "Withdrawal"; System.out.println("The
balance is "+ myObj.checkBalance(balance,transactionAmount,transactionType) +" after
a "+ transactionType + "
for the account number : "+myObj.accountNumber); } } |
Comments
Post a Comment