0% found this document useful (0 votes)
11 views28 pages

Sealed

Uploaded by

Dinesh R Balaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views28 pages

Sealed

Uploaded by

Dinesh R Balaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

• Given below is a hypothetical table showing rates of income tax for male

citizens below the age of 65 years:


• Write a program to input the age, gender (male or female) and Taxable Income
of a person.
• If the age is more than 65 years or the gender is female, display “wrong
category”. If the age is less than or equal to 65 years and the gender is male,
compute and display the income tax payable as per the table given above.

Taxable income (TI) in ₹ Income Tax in ₹

Does not exceed Rs. 1,60,000 Nil

Is greater than Rs. 1,60,000 and less than


(TI - 1,60,000) x 10%
or equal to Rs. 5,00,000.

Is greater than Rs. 5,00,000 and less than


[(TI - 5,00,000) x 20%] + 34,000
or equal to Rs. 8,00,000

Is greater than Rs. 8,00,000 [(TI - 8,00,000) x 30%] + 94,000


Java Program convert to C# Program

• public class KboatIncomeTax • if (age > 65 || gender.equalsIgnoreCase("female")) {


• System.out.print("Wrong Category");
• { • }
• public static void main(String args[]) { • else {
• Scanner in = new • if (ti <= 160000)
Scanner(System.in); • tax = 0;
• else if (ti <= 500000)
• System.out.print("Enter • tax = (ti - 160000) * 10 / 100;
Gender(male/female): "); • else if (ti <= 800000)
• String gender = in.nextLine(); • tax = 34000 + ((ti - 500000) * 20 / 100);
• else
• System.out.print("Enter Age: ");
• tax = 94000 + ((ti - 800000) * 30 / 100);
• int age = in.nextInt(); •
• System.out.print("Enter Taxable • System.out.println("Tax Payable: " + tax);
Income: "); • }
• }
• double ti = in.nextDouble(); • }
• double tax = 0.0;
Runtime Polymorphism
• Method Overriding : Run-time polymorphism is also known as
inheritance-based polymorphism or method overriding.
• Method Overriding is a technique that allows the invoking of
functions from another class (base class) in the derived class.
• Creating a method in the derived class with the same signature as a
method in the base class is called as method overriding.
• Overriding is a feature that allows a subclass or child class to provide a
specific implementation of a method that is already provided by one
of its super-classes or parent classes.
• When a method in a subclass has the same name, same parameters
or signature and same return type(or sub-type) as a method in its
super-class, then the method in the subclass is said to override the
method in the super-class.
3 types of keywords for Method
Overriding:
• virtual keyword: This modifier or keyword use within base class method. It is
used to modify a method in base class for overridden that particular method
in the derived class.
• override: This modifier or keyword use with derived class method. It is used
to modify a virtual or abstract method into derived class which presents in
base class.
• base Keyword: This is used to access members of the base class from
derived class. It basically used to access constructors and methods or
functions of the base class.
• The base keyword cannot use within a static method.
• Base keyword specifies which constructor of the base class should be invoked while
creating the instances of the derived class.
• using System;
• namespace Hello_Word • class Program
• {
• class baseClass
• {
• { • static void Main(string[]
• public virtual void Greetings()
args)
• {
• Console.WriteLine("baseClass Saying Hello!"); • {
• }
• }
• baseClass obj1 = new
subClass();
• class subClass : baseClass • obj1.Greetings();
• {
• public override void Greetings() • Console.ReadLine();
• {
• base.Greetings();
• }
• Console.WriteLine("subClass Saying Hello!"); • }
• }
• } • }
C# program to show the use of 'base‘ keyword in method overriding

• using System;
• public class web {
• class GFG {
• string name = “Programming"; • static void Main()
• public virtual void showdata()
• {
• {
• Console.WriteLine("Website Name: " + name); • stream E = new
• } stream();
• }
• class stream : web {
• E.showdata();
• string s = "Computer Science"; •
• public override void showdata()
• {
• }
• base.showdata(); • }
• Console.WriteLine("About: " + s);
• }
• }
• Note:
• Method overriding is possible only in derived classes. Because a
method is overridden in the derived class from the base class.
• A non-virtual or a static method can’t be overridden.
• Both the override method and the virtual method must have the
same access level modifier.
• The overridden base method cannot be a sealed method
Hiding Methods
• C# also provides a concept to hide the methods of the base class from
derived class, this concept is known as Method Hiding. It is also known
as Method Shadowing.
• you can hide the implementation of the methods of a base class from the
derived class using the new keyword.
• Method Hiding/Shadowing is also an approach of re-implementing the
parent class methods under the child class exactly with the same
signature (same name and same parameters).
• The child classes can re-implement any method of its parent class
methods even if they are not declared as virtual. That means here the
child class re-implements the parent class methods without taking any
permission from its parent.
• using System; • class Program
• namespace MethodHiding
• {
• {
• public class Parent • static void Main(string[]
• { args)
• public virtual void Show()
• {
• {
• Console.WriteLine("Parent Class Show Method"); • Child obj = new Child();
• }
• public void Display()
• obj.Show();
• { • obj.Display();
• Console.WriteLine("Parent Class Display Method"); Output:
• }
• Console.ReadKey();
• } • }
• public class Child : Parent
• {
• }
• } • }
• using System;
• namespace MethodHiding • //Method Hiding/Shadowing
• { • public new void Display()
• public class Parent • {
• { • Console.WriteLine("Child Class Display Method");
• public virtual void Show() • }
• {
• }
• Console.WriteLine("Parent Class Show Method");
• class Program
• }
• {
• public void Display()
• static void Main(string[] args)
• {
• {
• Console.WriteLine("Parent Class Display Method");
• }
• Child obj = new Child();
• } • obj.Show();
• public class Child : Parent • obj.Display();
• { • Console.ReadKey();
• //Method Overriding • }
• public override void Show() • }
• { • }
• Console.WriteLine("Child Class Show Method");
• } • Output:
• Hiding an inherited member using new does not remove the member,
it only makes the member inaccessible in the derived class.
• Class A • The new F() in B has private
• { access, its scope does not
• public static void F() extend to the class C.
• }
• Class B:A
• Thus the call F() in C, is valid,
• { invokes F() of A class.
• new private static void F()
• {
• }
• }
• Class C:B
• {
• Static void F1()
• {
• F();
• }

• }
Sealed class
• Sealed classes are used to restrict the users from inheriting the class. A class can be
sealed by using the sealed keyword.

• The keyword tells the compiler that the class is sealed. No class can be derived from
a sealed class.
• sealed class class_name
• {
• // data members
• // methods
• .
• .
• .

• }
• using System;
• namespace SealedClass {
• sealed class Animal {

• }

• class Dog : Animal {

• }

• class Program {
• static void Main (string [] args) {

• Dog d1 = new Dog();
• Console.ReadLine(); Since a sealed class cannot be inherited, the program
• } generates the following error:
• }
error CS0509: 'Dog': cannot derive from sealed type 'Animal'
• }
• using System;
• sealed class SealedClass {
• public int Add(int a, int b)
• {
• return a + b;
• }
• }

• class Calculator{
• static void Main(string[] args)
• {
• // Creating an object of Sealed Class
• SealedClass slc = new SealedClass();

• // Performing Addition operation
• int total = slc.Add(6, 4);
• Console.WriteLine("Total = " + total.ToString());
• }
• }
• using System;
• sealed class SealedClass { sealed class Calculation:SealedClass {
• public int Add(int a, int b) public int Sub(int a, int b)
• { {
return a - b;
• return a + b; }
• } }
• }

• class Calculator{
• static void Main(string[] args)
• {
• Calculation slc = new Calculation ();

• int total = slc.Add(6, 4);
• Console.WriteLine("Total = " + total.ToString());
• total = slc.Sub(6, 4);
• Console.WriteLine("Total = " + total.ToString());
• }
• }
Sealed Method
• During method overriding, if we don't want an overridden method
to be further overridden by another class, we can declare it as
a sealed method.
• However, a method can be sealed in the classes in which they have
been inherited.
example of a sealed method in a derived class

• using System;

• class Printer {

• // Display Function for Dimension printing


• public virtual void show()
• {
• Console.WriteLine("display dimension : 6*6");
• }

• public virtual void print()


• {
• Console.WriteLine("printer printing....\n");
• }
• }
• class LaserJet : Printer {

• // Sealed Display Function for Dimension printing


• sealed override public void show()
• {
• Console.WriteLine("display dimension : 12*12");
• }
• override public void print()
• {
• Console.WriteLine("Laserjet printer printing....\n");
• }
• }
• class Officejet : LaserJet {


• // compiler error : 'Officejet.show()' : cannot override inherited member 'LaserJet.show()' because it is sealed.
• override public void print()
• {
• Console.WriteLine("Officejet printer printing....");
• }
• }
• // Driver Class
• class Program {

• // Driver Code
• static void Main(string[] args)
• {
• Printer p = new Printer();
• p.show();
• p.print();

• Printer ls = new LaserJet();


• ls.show();
• ls.print();

• Printer of = new Officejet();


• of.show();
• of.print();
• }
• }
• Create a project to demonstrate the working of sealed class and sealed method.
For displaying by Defining a class Employee having the following description:

• Instance variables:
• int pan to store personal account number
• String name to store name
• double tax income to store annual taxable income
• double tax to store tax that is calculated

• Member functions:
• input ( ) Store the pan number, name, taxable income
• calc( ) Calculate tax for an employee
• display ( ) Output details of an employee.
• Project to implement run time polymorphism by creating a base class
with two methods. In the first method, call the second method.
Inherit a class and override the second method. create an object of
the derived class.

You might also like