In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:
- subclass (child) - the class that inherits from another class
- superclass (parent) - the class being inherited from
//Class name extance class name{}//
To inherit from a class, use the extends keyword.
In the example below, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass):
Today i PRACTICES QUESTIONS work with this questions
Write a program for this questions answers are gitlab account
(https://gitlab.com/arunkumarsiva81/aug25batch.git)
//Single INHERITANCE_
class Shape{
void area()
{
System.out.println("Area of Shape");
}
}
class Circle extends Shape{
void area(){
double radius = 5;
double value = 3.14;
double X = radius*value;
System.out.println("Area of circlr :"+X);
}
}
class Rectangle extends Shape{
void area(){
int length = 4, width = 8;
int result = length*width;
System.out.println("Area of Rectangle :"+result);
}
}
class Triangle extends Shape{
void area(){
int base =3, height =8;
int result = base*height;
System.out.println("Area of Triangle :"+result);
}
}
public class Main {
public static void main(String[] args) {
Circle obj1 = new Circle();
Rectangle obj2 = new Rectangle();
Triangle obj3 = new Triangle();
obj1.area();
obj2.area();
obj3.area();
}
}
//*MULTI LEVEL INHERITANCE *
Bank Example
Class Bank → method bankDetails().
Class Branch (extends Bank) → method branchDetails().
Class Customer (extends Branch) → method customerDetails().
Test multi-level access with a Customer object.
package Arun;
class Bank{
void bankName(){
String BankName ="State bank of india";
String address ="xxxxxxxx";
System.out.println("name of the bank :"+BankName);
System.out.println("main branch address :"+address);
}
}
class Branch extends Bank {
void BranchName(){
String branch = "chennaibranch";
String branchadd = "vellachary";
int contacts = 0001200000;
int ifsccode = 004500;
System.out.println("This is branch name"+branch);
System.out.println("This is address"+branchadd);
System.out.println("contacts number"+contacts);
System.out.println("branch ifsc code"+ifsccode);
}
}
class Customer extends Branch {
void customerName (){
String name = "Arun kumar";
int accNo = 0000054000;
int mobileNo = 000545000;
System.out.println("customer name"+name);
System.out.println("Account number"+accNo);
System.out.println("mobileNo :"+mobileNo);
}
}
public class Multilevel{
public static void main (String[] args){
Customer details = new Customer();
details.bankName();
details.BranchName();
details.customerName();
}
}
//HIERAICAL INHERITANCE
1️⃣ Shape Example 🔺⚪⬛
Create a base class Shape with a method area().
Create subclasses:
Circle (extends Shape) → override area() to calculate circle area.
Rectangle (extends Shape) → override area() to calculate rectangle area.
Triangle (extends Shape) → override area() to calculate triangle area.
In main(), create objects of all three classes and call their area() methods()
class Shape{
void area()
{
System.out.println("Area of Shape");
}
}
class Circle extends Shape{
void area(){
double radius = 5;
double value = 3.14;
double X = radius*value;
System.out.println("Area of circlr :"+X);
}
}
class Rectangle extends Shape{
void area(){
int length = 4, width = 8;
int result = length*width;
System.out.println("Area of Rectangle :"+result);
}
}
class Triangle extends Shape{
void area(){
int base =3, height =8;
int result = base*height;
System.out.println("Area of Triangle :"+result);
}
}
public class Hierarchical {
public static void main(String[] args) {
Circle obj1 = new Circle();
Rectangle obj2 = new Rectangle();
Triangle obj3 = new Triangle();
obj1.area();
obj2.area();
obj3.area();
}
}
Top comments (0)