0% found this document useful (0 votes)
12 views7 pages

Java Inheritance and OOP Examples

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Java Inheritance and OOP Examples

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

class 

Student{  
    int id;  
    String name;  
      
    Student(int i,String n){  
    id = i;  
    name = n;  
    }  
    void display(){[Link](id+" "+nam
e);}  
   
    public static void main(String args[]){  
    Student s1 = new Student(111,"Karan");  
    Student s2 = new Student(222,"Aryan");  
    [Link]();  
    [Link]();  
   }  
}  
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();
[Link]("James");
[Link](20);
[Link]("12343ms");

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

public class car extends Super_class {


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

public void getAge()


{
[Link]("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);
[Link]();
}}
class Vehicle{
int num = 20;
public void display()
{ [Link]("This is the display
method of superclass");
} }
class bus extends Vehicle {
int num = 10;
public void display(){
[Link]();
[Link]("This is the display
method of subclass bus");
[Link]("value of the
variable named num in super class:"+ [Link]);
}}
public class car extends Vehicle{
public void display(){
[Link]();
[Link]("This is the display
method of subclass car");
[Link]("value of the
variable named num in super class:"+ [Link]);
}
public static void main(String args[]){
car obj = new car();
[Link]();
bus b = new bus();
[Link]();
[Link]("value of the
variable named num in sub class:"+ [Link]);
}
}

Common questions

Powered by AI

Encapsulation in the `EncapTest` class provides the benefit of data protection by restricting access to private variables and exposing only specific public methods for interacting with the data, thereby enhancing security and data integrity. However, this approach can have drawbacks, such as increased overhead due to the necessity of writing additional methods for getting and setting data, which could complicate the codebase if not designed carefully. Overusing accessors can sometimes lead to breaking encapsulation by allowing excessive external manipulation, thus defeating its purpose .

In the `My_Calculation` class, method overriding is utilized by defining a `multiplication` method that is unique to this class, while it inherits the `addition` and `Substraction` methods from its superclass, `Calculation`. This demonstrates polymorphism, where a subclass can extend or modify functionalities of its superclass. The significance of method overriding in object-oriented programming includes increased flexibility and the ability to define a subclass-specific version of a method, thereby improving code reuse and clarity .

The `super` keyword is used in the `Subclass` constructor to invoke the constructor of its superclass, `Superclass`, which takes an integer `age` as an argument. This ensures that the `age` field is properly initialized in the superclass. The `super` keyword helps in calling the parent class methods and accessing its fields, ensuring that the subclass maintains the integrity of the inherited features .

Encapsulation enhances data protection by restricting direct access to some of an object's components, which can prevent unauthorized parts of a program from interfering with the object's internal state. In the provided code example, the class `EncapTest` makes the fields `name`, `idNum`, and `age` private and provides public methods `getName`, `getIdNum`, `getAge`, `setName`, `setIdNum`, and `setAge` to access and modify these fields. This encapsulation ensures that changes to the data can only be made through these controlled methods .

Polymorphism is achieved in the 'car' and 'bus' classes by method overriding. Both classes override the `display` method defined in their superclass, `Vehicle`, to provide specific implementations. When the `display` method is called on an object of type `car` or `bus`, the method unique to that subclass is executed, illustrating polymorphism. This allows the same method to behave differently based on the object's runtime type .

Setter methods in Java, such as `setAge`, `setName`, and `setIdNum`, provide controlled access to modify the value of private fields in a class. This is important for encapsulation because it allows modifications only through these methods, wherein validation checks or other logic can be implemented before changing the internal state. This helps maintain data integrity and protects the data from invalid inputs .

In the context of the `car` class' `display` method, the keyword `super` is used to call the `display` method of the superclass `Vehicle`. This is necessary to ensure that the functionality provided by the superclass's version of the `display` method is preserved and executed before adding the subclass-specific behavior. It maintains the inheritance chain and allows the subclass to build on top of the existing methods, promoting code reuse and extension .

The principle of inheritance is reflected in the design of the `Calculation` and `My_Calculation` classes through the `My_Calculation` class extending `Calculation`. This signifies that `My_Calculation` inherits methods and variables from `Calculation`, such as `addition` and `Substraction`, and is capable of adding new functionalities like `multiplication`. Inheritance allows `My_Calculation` to reuse and enhance existing code, demonstrating the 'is-a' relationship between subclasses and superclasses .

The 'car' and 'bus' classes demonstrate an inheritance relationship with the 'Vehicle' class, meaning they are subclasses or derived classes of 'Vehicle'. This implies that both 'car' and 'bus' inherit properties and methods from 'Vehicle', such as the field 'num' and the method 'display'. This is a fundamental concept of inheritance in object-oriented programming, where a subclass can reuse code from the superclass, potentially overriding existing methods or adding new ones .

The 'this' keyword is used in the constructor of the `Superclass` to differentiate between the class fields and the parameters of the constructor which have the same name. In this context, 'this.age = age;' assigns the value of the parameter `age` to the instance variable `age` of the `Superclass` object being created. This is essential when both the instance variable and the parameter name are the same to remove ambiguity .

You might also like