Java Concepts: Inheritance, Interfaces, and Multithreading
🕒 2025-04-21 09:14:50.117231In this post, we will be focusing on the most important Java Concepts i.e. inheritance, interfaces, and multithreading.
What will you learn?
- Java Inheritance Simplified: Practical Examples and Explanation
- How multiple inheritance is possible in Java? Through class or interface
- Exploring Multithreading in Java: Benefits, Importance, and Thread Lifecycle"
Java Inheritance Simplified: Practical Examples and Explanation
Inheritance is a process of creating a new class (child class/derived class) built upon an existing class (parent class/base class). It is a mechanism that allows one object to inherit all of the properties and behaviors of its parent object. It is an essential part of OOPs (Object Oriented Programming). When we inherit from a parent class, we can use its methods and fields. Furthermore, we can extend the current class by adding additional methods and properties. The IS-A relationship, often known as a parent-child relationship, is represented by inheritance.
Why use inheritance in Java?
- For Method Overriding (to enable runtime polymorphism).
- For code reusability.
We have two methods to initialize the data: using the constructor and using the setter method.
Using constructor
package javaProject;
public class Person {
private String name;
private int age;
private String address;
// using constructor
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public static void main(String[] args){
Person p = new Person("Ishwar",26,"Butwal");
}
}
Using the setter method of getter/setter
package javaProject;
public class Person {
private String name;
private int age;
private String address;
// using setter
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
}
public static void main(String[] args){
Person p = new Person();
p.setName("Ishwar");
p.setAge(26);
p.setAddress("Butwal");
}
}
Now to inheritance from the base (parent class), we can use the extend keyword as below. Class Student inherits from the Parent class. I am using a constructor to initialize/set the value. You can replace it with a setter method if you like. Here is the complete code showing the inheritance mechanism.
package javaProject;
// parent class
class Person{
private String name;
private int age;
private String address;
// using constructor
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
}
// child class; use extends to inherit property
class Students extends Person{
private int stuid;
public Students(String name, int age, String address, int stuid) {
super(name, age, address);
this.stuid = stuid;
}
public int getStuid() {
return stuid;
}
}
public class Main{
public static void main(String[] args){
Students s = new Students("Ishwar",23,"Butwal",6);
System.out.println(s.getName());
System.out.println(s.getAge());
System.out.println(s.getAddress());
System.out.println(s.getStuid());
}
}
How multiple inheritance is possible in Java? Through class or interface
/*
Q. Discuss the use of interfaces to achieve multiple inheritance.
Q. What is interface? How can you implement multiple inheritance using the interface concept? Discuss with suitable example.
Q. How do you achieve multiple inheritance in java? Discuss.
Q. Why concept of interface is important? How multiple-inheritance is supported
by interface? Explain with suitable example.
Q. Write short note on: Interface
*/
package myProgram;
class Cricket {
public void func() {
System.out.println("I play cricket");
}
}
class Football {
public void func() {
System.out.println("I play football");
}
}
public class Student extends Cricket, Football { //suppose if it were
public static void main(String[] args) {
Student s = new Student();
s.func(); //which method has to be invoked?
}
}
//But we will get error with this
//so, multiple inheritance is not achieved through class
//solution: interface
package myProgram;
interface Cricket{
public void func1(); //interface method (does not have a body)
}
interface Football{
public void func2(); //interface method (does not have a body)
}
public class Student implements Cricket, Football {
public void func1() {
System.out.println("I play Cricket");
}
public void func2() {
System.out.println("I play Football");
}
public static void main(String[] args) {
Student s = new Student();
s.func1();
s.func2();
}
}
Exploring Multithreading in Java: Benefits, Importance, and Thread Lifecycle"
Multithreading involves executing multiple threads simultaneously. A multi-threaded program consists of two or more sections that can run concurrently. Each component can tackle a separate task at the same time, making the most use of available resources, which is especially important when our machine has multiple CPUs.
Importance/benefits of multithreading:
- Multithreading allows us to write efficient programs that make maximum use of the CPU.
- Because of multithreading, animation loops can sleep for a second between frames without forcing the entire system to pause.
- Multithreading requires less overhead than multitasking: In multitasking, processes run in their own different address space, and tasks involved in multithreading can share the same address space.
Life cycle of thread:
A thread's life cycle includes several stages. A thread, for example, gets born, starts, runs, and then dies. The graphic below illustrates a thread's whole life cycle.
package ishwar;
public class Multithreading {
public static void main(String[] args) {
JavaThread thread1 = new JavaThread("Thread1");
thread1.start();
JavaThread thread2 = new JavaThread("Thread2");
thread2.start();
}
}
class JavaThread implements Runnable {
Thread mythread;
private String myname;
JavaThread(String name) {
myname = name;
}
@Override
public void run() {
System.out.println("Thread running: " + myname);
for (int i = 1; i <= 5; i++) {
System.out.println(i);
System.out.println(myname);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread has been interrupted");
}
}
}
public void start() {
System.out.println("Thread started");
if (mythread == null) {
mythread = new Thread(this, myname);
mythread.start();
}
}
}
Conclusion:
In this post, we've looked at some important Java Concepts: inheritance, multiple inheritance, and multithreading. By grasping these concepts, you can improve your understanding of Java and utilize its strong capabilities to create more efficient and effective programs.
I hope this post is very helpful to you. If you have any questions, ask me in the comment section. I will reply as soon as possible. Thanks.
Comments
Loading comments...
Leave a Comment