MODULE-II MR-23 Java Inhertance and Packages
MODULE-II MR-23 Java Inhertance and Packages
1. Single-Level Inheritance :
When a class inherits another class, it is known as a single inheritance.
Dog class (Sub class) inherits to the Animal class (Super class), so it is called
a single inheritance.
2. Multi-Level Inheritance :
Import java.io.*;
1. class cse // Create a Super class
2. {
3. void csed()
4. {
5. System.out.println("Computer Science and Engineering Branch...");
6. }
7. }
8. class AIML extends cse // Create a sub-class1
9. {
10. void aimlmd()
11. {
System.out.println("AI&ML Branch...");
12. }
13. }
14. class DS extends AIML // Create s sub-class2
15. {
16. void dsd()
17. {
18. System.out.println("Data Science Branch...");
19. }
20. }
21. class CS extends DS // Create a sub class-3
22. {
23. void csd()
24. {
25. System.out.println (“ Cyber Security Branch….”);
26. }
27. }
28. Class IOT extends CS // Create sub class-4
29. {
30. void iotd()
31. {
32. System.out.println (“ Internet of Things Branch….”);
33. }
34. }
35. class mreca
36. {
37. public static void main(String args[])
38. {
39. IOT i=new IOT();
40. i.csed();
41. i.aimlmd();
42. i.dsd();
43. i.csd();
44. i.iotd();
45. }
46. }
3. Hierarchal Inheritance :
When two or more classes inherit a single class, it is known as hierarchical
inheritance.
import java.io.*;
1. public class cse // Create a super class
2. {
3. void csed()
4. {
5. System.out.println("Computer Science and Engineering...");
6. }
7. }
8. public class aiml extends cse // Create a sub class-1
9. {
10.void aimlmd()
11.{
12.System.out.println("Artificial Intelligence and Machine Leraning...");
13.}
14.}
15.public class ds extends cse // Create a sub class-2
16.{
17.void dsd()
18.{
19.System.out.println("Data Science Engineering...");
20.}
21.}
22.Public class iot extends cse // Create a sub class-3
23. {
24.void iotd()
25.{
26.System.out.println (“ Intertet of Things Department”);
27.}
28.public class mrec {
29.
30.public static void main(String args[])
31.{
32.iot i=new iot();
33.i.csed();
34.i..aimlmd();
35.i.dsd();
36.i.iotd();
37.}
38.}
5.Hybrid Inheritance :
OUTPUT:
He is son.
He is father.
He is grandfather.
She is daughter.
He is father.
He is grandfather.
1. class Bike{
2. final int speedlimit=90; // Use a final variable
3. void run() {
4. speedlimit=400;
5. }
6. public static void main(String args[])
7. {
8. Bike obj=new Bike();
9. obj.run();
10. }
11. }
final class Bike{} // Use a final class to declared only super class
The Object class in java is the parent class of all the classes in java by
default.
Example:
csem()
{
// statements and fields
}
class AIML extends coures { // Create a sub class
aiml()
{
//Statements and fields
}
class mreca
{
AIML a= new AIML();
a. csem();
a. aiml();
}
}
In Java, one type (super class) is a subtype of another class (sub class)
using extends keyword to implement into inheritance..
Sustainability in java :
Example: Dog class (Sub class) inherits to the Animal class (Super class),
so it is called a sustainability in java.
Specialization
Specification
Construction
Eextension
Limitation
Combination
Specialization
It is the most ideal form of inheritance. The subclass is a special case of
the parent class. It holds the principle of substitutability.
Specification
This is another commonly used form of inheritance. In this form of
inheritance, the parent class just specifies which methods should be
available to the child class but doesn't implement them. The java provides
concepts like abstract and interfaces to support this form of inheritance. It
holds the principle of substitutability.
Construction
This is another form of inheritance where the child class may change the
behavior defined by the parent class (overriding). It does not hold the
principle of substitutability.
Eextension
This is another form of inheritance where the child class may add its new
properties. It holds the principle of substitutability.
Limitation
This is another form of inheritance where the subclass restricts the
inherited behavior. It does not hold the principle of substitutability.
Combination
This is another form of inheritance where the subclass inherits properties
from multiple parent classes. Java does not support multiple inheritance
type.
Benefits of Inheritance :
Inheritance helps in code reuse. The child class may use the code
defined in the parent class without re-writing it.
Inheritance can save time and effort as the main code need not be
written again.
Inheritance provides a clear model structure which is easy to
understand.
An inheritance leads to less development and maintenance costs.
With inheritance, we will be able to override the methods of the base
class so that the meaningful implementation of the base class method
can be designed in the derived class.
In inheritance base class can decide to keep some data private so that
it cannot be altered by the derived class.
Costs of Inheritance :
Inheritance decreases the execution speed due to the increased time
and effort it takes, the program to jump through all the levels of
overloaded classes.
Inheritance makes the two classes (base and inherited class) get
tightly coupled. This means one cannot be used independently of each
other.
The changes made in the parent class will affect the behavior of child
class too.
The overuse of inheritance makes the program more complex.
class ParentClass{
int num = 10;
void showData() // Create Method Overrding
{
System.out.println("Inside ParentClass show method");
System.out.println("num = " + num);
}
}
class ChildClass extends ParentClass
{
void showData() // Create Method Overriding
{
System.out.println("Inside ChildClass show method");
System.out.println("num = " + num);
}
}
class cse {
public static void main(String[] args)
{
ParentClass obj = new ParentClass(); // Object create a super class
obj.showData(); // Call a method Super Class
obj = new ChildClass(); // Object create a same super class
obj.showData(); // Calla a method sub-class
}
}
1. abstract class A{
2. // Statements and fields
3. }
The following table depicts all built-in methods of Object class in java.
Method Description
wait() causes the current thread to wait, until another thread notifies.
wait(long,int) causes the current thread to wait for the specified milliseconds
and nanoseconds, until another thread notifies.
Java Packages
A java package is a group of similar types of classes, interfaces and
sub-packages.
Packages in java can be categorized in two types are
1. Built-in packages
2. User-defined packages.
1. package mypack;
2. public class Simple{
3. public static void main(String args[])
4. {
5. System.out.println("Welcome to package");
6. }
7. }
1. package pack;
2. public class A
3. {
4. public void msg()
5. {
6. System.out.println("Hello");
7. }
8. }
Import Packages
The import keyword is used to make the classes and interface of another
package accessible to the current package.
The build system uses the classpath to locate and load classes and
libraries required for the build process.
All the directories which contain .class files and JAR files (Java
Archive File) files or zip files when setting the CLASSPATH.