0% found this document useful (0 votes)
55 views

Inheritance Sample Programs

The document contains code examples demonstrating inheritance and polymorphism in Java: 1) It defines a Student class with id and name fields and a display() method, and creates Student objects in main(). 2) It defines an EncapTest class with private fields and public getter/setter methods to demonstrate encapsulation. A RunEncap class uses the methods. 3) It defines a Calculation superclass and My_Calculation subclass, overriding the multiplication() method to demonstrate polymorphism. 4) Several examples show inheritance and accessing superclass/subclass fields and methods. 5) The last example defines Vehicle superclass and bus/car subclasses, overriding display() and accessing superclass fields, to further demonstrate inheritance concepts
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Inheritance Sample Programs

The document contains code examples demonstrating inheritance and polymorphism in Java: 1) It defines a Student class with id and name fields and a display() method, and creates Student objects in main(). 2) It defines an EncapTest class with private fields and public getter/setter methods to demonstrate encapsulation. A RunEncap class uses the methods. 3) It defines a Calculation superclass and My_Calculation subclass, overriding the multiplication() method to demonstrate polymorphism. 4) Several examples show inheritance and accessing superclass/subclass fields and methods. 5) The last example defines Vehicle superclass and bus/car subclasses, overriding display() and accessing superclass fields, to further demonstrate inheritance concepts
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

class 

Student{  
    int id;  
    String name;  
      
    Student(int i,String n){  
    id = i;  
    name = n;  
    }  
    void display(){System.out.println(id+" "+nam
e);}  
   
    public static void main(String args[]){  
    Student s1 = new Student(111,"Karan");  
    Student s2 = new Student(222,"Aryan");  
    s1.display();  
    s2.display();  
   }  
}  
public class EncapTest {
private String name;
private String idNum;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setAge( int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
}}
public class RunEncap {

public static void main(String args[]) {


EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");

System.out.print("Name : " +
encap.getName() + " Age : " + encap.getAge());
}
}
class Calculation{
int z;
public void addition(int x, int y){
z = x+y;
System.out.println("The sum of the given
numbers:"+z); }
public void Substraction(int x,int y){
z = x-y;
System.out.println("The difference between the
given numbers:"+z); }
}
public class My_Calculation extends Calculation{
public void multiplication(int x, int y){
z = x*y;
System.out.println("The product of the given
numbers:"+z);
}
public static void main(String args[]){
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Substraction(a, b);
demo.multiplication(a, b); }}
class Super_class{
int num = 20;
public void display(){
System.out.println("This is the display
method of superclass");
} }

public class car extends Super_class {


int num = 10;
public void display(){
super.display();
System.out.println("This is the display method
of subclass");
System.out.println("value of the
variable named num in super class:"+ super.num);
}
public static void main(String args[]){
car obj = new car();
obj.display();
System.out.println("value of the variable named
num in sub class:"+ obj.num);
}
}
class Superclass{
int age;
Superclass(int age){
this.age = age;
}

public void getAge()


{
System.out.println("The value of the
variable named age in super class is: " +age);
}
}
public class Subclass extends Superclass {
Subclass(int age)
{
super(age);
}
public static void main(String argd[])
{
Subclass s = new Subclass(24);
s.getAge();
}}
class Vehicle{
int num = 20;
public void display()
{ System.out.println("This is the display
method of superclass");
} }
class bus extends Vehicle {
int num = 10;
public void display(){
super.display();
System.out.println("This is the display
method of subclass bus");
System.out.println("value of the
variable named num in super class:"+ super.num);
}}
public class car extends Vehicle{
public void display(){
super.display();
System.out.println("This is the display
method of subclass car");
System.out.println("value of the
variable named num in super class:"+ super.num);
}
public static void main(String args[]){
car obj = new car();
obj.display();
bus b = new bus();
b.display();
System.out.println("value of the
variable named num in sub class:"+ obj.num);
}
}

You might also like