Detailed presentation on Inheritance and interfaces in JAVA. Presentation includes suitable example for better understanding the concepts such as Overriding in java and also keywords such as FINAL and SUPER.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are implemented by classes where they inherit the properties and must define the body of the abstract methods. Key points are:
- Interfaces can only contain abstract methods and static constants, not method bodies.
- Classes implement interfaces to inherit the properties and must define the abstract method bodies.
- An interface can extend other interfaces and a class can implement multiple interfaces.
Here I discuss about Java programming language and easiest way to solve programming problem. Java basic syntax and their uses are described briefly so that anyone can easily understand within very short time. If anyone follow the slide with proper way,I assure that he or she will find java programming interesting.
The document discusses access specifiers in C++ classes. There are three access specifiers: public, private, and protected. Private restricts access to class members to only within the class, public allows access from anywhere, and protected is used for inheritance but not discussed here. By default, members are private. Public members can be accessed from outside the class, while private members can only be accessed within class functions. Access specifiers control the scope and accessibility of class members.
Type casting involves assigning a value of one data type to a variable of another type. There are two types of casting: widening (implicit) and narrowing (explicit). Widening casting converts data to a broader type without needing explicit casting, like converting an int to a long. Narrowing casting converts to a narrower data type and requires explicit casting, such as converting a double to a long.
This document provides an overview of the Java Virtual Machine (JVM) and how it executes Java code. It describes that the JVM converts Java bytecode into machine language and executes it, allowing Java programs to run on different platforms. It also outlines the key components of the JVM, including the class loader, execution engine, stack, method area, and garbage collected heap.
1) A friend function allows access to private and protected members of a class. It is declared inside the class using the keyword "friend".
2) A friend function is not a member function - it is defined outside of the class and does not have access to non-static members using the class object. However, it can access private and protected members of the class.
3) In the example, the Temperature class declares the temp function as a friend. This allows temp to directly access and modify the private celsius member, something that regular non-member functions cannot do. The friend declaration gives temp special access privileges.
An operator is a symbol designed to operate on data.
They can be a single symbol, di-graphs, tri-graphs or keywords.
Operators can be classified in different ways.
This is similar to function overloading
This document discusses exception handling in Java. It defines exceptions as abnormal conditions that disrupt normal program flow. Exception handling allows programs to gracefully handle runtime errors. The key aspects covered include the exception hierarchy, try-catch-finally syntax, checked and unchecked exceptions, and creating user-defined exceptions.
This document discusses the diamond problem that can occur with multiple inheritance in C++. Specifically, it shows an example where a class "four" inherits from classes "two" and "three", which both inherit from class "one". This results in two copies of the base class "one" being present in objects of class "four", leading to ambiguity when trying to access attributes from the base class. The document presents two ways to resolve this issue: 1) manual selection using scope resolution to specify which attribute to access, and 2) making the inheritance of the base class "one" virtual in classes "two" and "three", which ensures only one copy of the base class exists in class "four" objects. The virtual
Static Data Members and Member FunctionsMOHIT AGARWAL
Static data members and static member functions in C++ classes are shared by all objects of that class. Static data members are initialized to zero when the first object is created and shared across all instances, while static member functions can only access other static members and are called using the class name and scope resolution operator. The example program demonstrates a class with a static data member "count" that is incremented and accessed by multiple objects to assign increasing code values, and a static member function "showcount" that prints the shared count value.
This presentation introduces Java packages, including system packages that are part of the Java API and user-defined packages. It discusses how packages organize related classes and interfaces, the structure of package names and directories, and how to create and access packages. Packages provide advantages like grouping related code, preventing name collisions, and improving reusability.
This document discusses inheritance and method overriding in Java. It defines inheritance as a mechanism where one class inherits features from another superclass. There are different types of inheritance in Java including single, multilevel, hierarchical and multiple inheritance through interfaces. Method overriding allows a subclass to provide its own implementation of a method defined in the superclass without modifying the superclass. The rules for overriding include matching argument lists and not overriding private, static or final methods. Overriding enables dynamic binding at runtime.
Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Destructor on the other hand is used to destroy the class object.
The document discusses exception handling in programming. It defines different types of errors like syntax errors, semantic errors, and logical errors. Runtime errors can occur due to issues like division by zero. Exception handling involves finding and throwing exceptions using try, catch, and throw keywords. The catch block handles the exception. Common Java exceptions include NullPointerException, FileNotFoundException, and ArrayIndexOutOfBoundsException.
The document discusses classes and objects in object-oriented programming. It defines a class as a blueprint for objects that bind data and functions together. A class defines data members and member functions. Objects are instances of a class that can access class data and functions. The document provides examples of defining a class called "test" with private and public members, and creating objects of the class to demonstrate accessing members.
Type casting is a topic of computer science engineering subject know as computer graphics multimedia and animation(cgma) it contains data types which is explained by the examples
The document discusses input/output streams in Java. There are two types of streams: byte streams and character streams. Byte streams handle input and output of bytes for binary files, while character streams handle input and output of characters for text files. Java also defines three standard streams for input, output, and errors that are represented by System.in, System.out, and System.err respectively. The document provides examples of different stream types and how they are used for input and output in Java programs.
The document provides an introduction to the Java programming language. It discusses that Java was originally called Oak and developed by Sun Microsystems in 1991. It can be used to create both applications and applets. The source code is compiled into bytecode, which is then interpreted by the Java Virtual Machine (JVM) to execute programs. Key features of Java include being simple, secure, portable, object-oriented, robust, and multi-threaded. The document also covers concepts such as packages, interfaces, inheritance, method overloading/overriding, and multithreading.
This document discusses exception handling in C++ and Java. It defines what exceptions are and explains that exception handling separates error handling code from normal code to make programs more readable and robust. It covers try/catch blocks, throwing and catching exceptions, and exception hierarchies. Finally, it provides an example of implementing exception handling in a C++ program to handle divide-by-zero errors.
Learn the various forms of polymorphism in Java with illustrative examples to explain method overloading(Compile-time polymorphism) and method overriding(Run-time polymorphism)
This document defines polymorphism and describes its two types - compile-time and run-time polymorphism. Compile-time polymorphism is demonstrated through method overloading examples, while run-time polymorphism is demonstrated through method overriding examples. The key advantages of polymorphism are listed as code cleanliness, ease of implementation, alignment with real world scenarios, overloaded constructors, and reusability/extensibility.
Operators in Java provide symbols that operate on arguments to produce results. The document discusses the different types of operators in Java including assignment, arithmetic, relational, logical, bitwise, and ternary operators. Examples are provided to demonstrate the usage of various operators like increment/decrement, arithmetic, relational, logical, bitwise, ternary, and instanceof operators in Java code.
This document introduces object-oriented programming concepts including classes, inheritance, encapsulation, and polymorphism. It discusses how OOP allows for more organized and flexible code through the use of classes, objects, and methods. Key aspects of classes like constructors, destructors, and access modifiers are explained. Other concepts covered include static vs non-static classes, method overloading, enumerations, structures, abstract classes, interfaces, and abstract methods. The document aims to provide an overview of fundamental OOP principles.
The document discusses threads and multithreading in Java. It defines a thread as a single sequential flow of control within a program. Multithreading allows a program to be divided into multiple subprograms that can run concurrently. Threads have various states like newborn, runnable, running, blocked, and dead. The key methods for managing threads include start(), sleep(), yield(), join(), wait(), notify(). Synchronization is needed when multiple threads access shared resources to prevent inconsistencies. Deadlocks can occur when threads wait indefinitely for each other.
This document discusses classes, objects, and methods in Java. It defines a class as a user-defined data type that contains fields and methods. Objects are instances of classes that allocate memory at runtime. Methods define behaviors for objects and are declared within classes. The document covers defining classes, creating objects, accessing members, constructors, method overloading and overriding, static members, passing objects as parameters, recursion, and visibility control.
This document discusses the diamond problem that can occur with multiple inheritance in C++. Specifically, it shows an example where a class "four" inherits from classes "two" and "three", which both inherit from class "one". This results in two copies of the base class "one" being present in objects of class "four", leading to ambiguity when trying to access attributes from the base class. The document presents two ways to resolve this issue: 1) manual selection using scope resolution to specify which attribute to access, and 2) making the inheritance of the base class "one" virtual in classes "two" and "three", which ensures only one copy of the base class exists in class "four" objects. The virtual
Static Data Members and Member FunctionsMOHIT AGARWAL
Static data members and static member functions in C++ classes are shared by all objects of that class. Static data members are initialized to zero when the first object is created and shared across all instances, while static member functions can only access other static members and are called using the class name and scope resolution operator. The example program demonstrates a class with a static data member "count" that is incremented and accessed by multiple objects to assign increasing code values, and a static member function "showcount" that prints the shared count value.
This presentation introduces Java packages, including system packages that are part of the Java API and user-defined packages. It discusses how packages organize related classes and interfaces, the structure of package names and directories, and how to create and access packages. Packages provide advantages like grouping related code, preventing name collisions, and improving reusability.
This document discusses inheritance and method overriding in Java. It defines inheritance as a mechanism where one class inherits features from another superclass. There are different types of inheritance in Java including single, multilevel, hierarchical and multiple inheritance through interfaces. Method overriding allows a subclass to provide its own implementation of a method defined in the superclass without modifying the superclass. The rules for overriding include matching argument lists and not overriding private, static or final methods. Overriding enables dynamic binding at runtime.
Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Destructor on the other hand is used to destroy the class object.
The document discusses exception handling in programming. It defines different types of errors like syntax errors, semantic errors, and logical errors. Runtime errors can occur due to issues like division by zero. Exception handling involves finding and throwing exceptions using try, catch, and throw keywords. The catch block handles the exception. Common Java exceptions include NullPointerException, FileNotFoundException, and ArrayIndexOutOfBoundsException.
The document discusses classes and objects in object-oriented programming. It defines a class as a blueprint for objects that bind data and functions together. A class defines data members and member functions. Objects are instances of a class that can access class data and functions. The document provides examples of defining a class called "test" with private and public members, and creating objects of the class to demonstrate accessing members.
Type casting is a topic of computer science engineering subject know as computer graphics multimedia and animation(cgma) it contains data types which is explained by the examples
The document discusses input/output streams in Java. There are two types of streams: byte streams and character streams. Byte streams handle input and output of bytes for binary files, while character streams handle input and output of characters for text files. Java also defines three standard streams for input, output, and errors that are represented by System.in, System.out, and System.err respectively. The document provides examples of different stream types and how they are used for input and output in Java programs.
The document provides an introduction to the Java programming language. It discusses that Java was originally called Oak and developed by Sun Microsystems in 1991. It can be used to create both applications and applets. The source code is compiled into bytecode, which is then interpreted by the Java Virtual Machine (JVM) to execute programs. Key features of Java include being simple, secure, portable, object-oriented, robust, and multi-threaded. The document also covers concepts such as packages, interfaces, inheritance, method overloading/overriding, and multithreading.
This document discusses exception handling in C++ and Java. It defines what exceptions are and explains that exception handling separates error handling code from normal code to make programs more readable and robust. It covers try/catch blocks, throwing and catching exceptions, and exception hierarchies. Finally, it provides an example of implementing exception handling in a C++ program to handle divide-by-zero errors.
Learn the various forms of polymorphism in Java with illustrative examples to explain method overloading(Compile-time polymorphism) and method overriding(Run-time polymorphism)
This document defines polymorphism and describes its two types - compile-time and run-time polymorphism. Compile-time polymorphism is demonstrated through method overloading examples, while run-time polymorphism is demonstrated through method overriding examples. The key advantages of polymorphism are listed as code cleanliness, ease of implementation, alignment with real world scenarios, overloaded constructors, and reusability/extensibility.
Operators in Java provide symbols that operate on arguments to produce results. The document discusses the different types of operators in Java including assignment, arithmetic, relational, logical, bitwise, and ternary operators. Examples are provided to demonstrate the usage of various operators like increment/decrement, arithmetic, relational, logical, bitwise, ternary, and instanceof operators in Java code.
This document introduces object-oriented programming concepts including classes, inheritance, encapsulation, and polymorphism. It discusses how OOP allows for more organized and flexible code through the use of classes, objects, and methods. Key aspects of classes like constructors, destructors, and access modifiers are explained. Other concepts covered include static vs non-static classes, method overloading, enumerations, structures, abstract classes, interfaces, and abstract methods. The document aims to provide an overview of fundamental OOP principles.
The document discusses threads and multithreading in Java. It defines a thread as a single sequential flow of control within a program. Multithreading allows a program to be divided into multiple subprograms that can run concurrently. Threads have various states like newborn, runnable, running, blocked, and dead. The key methods for managing threads include start(), sleep(), yield(), join(), wait(), notify(). Synchronization is needed when multiple threads access shared resources to prevent inconsistencies. Deadlocks can occur when threads wait indefinitely for each other.
This document discusses classes, objects, and methods in Java. It defines a class as a user-defined data type that contains fields and methods. Objects are instances of classes that allocate memory at runtime. Methods define behaviors for objects and are declared within classes. The document covers defining classes, creating objects, accessing members, constructors, method overloading and overriding, static members, passing objects as parameters, recursion, and visibility control.
The document discusses inheritance in Java programming. It defines inheritance as a mechanism that allows one class to inherit features like fields and methods from another class. There are three main types of inheritance discussed - single inheritance where one class inherits from another, multilevel inheritance where a class inherits from another inherited class, and hierarchical inheritance where multiple classes inherit from a single base class. The key concepts of superclass, subclass, method overriding and abstract classes are explained.
Inheritance in Java allows classes to acquire properties and behaviors of parent classes. The extends keyword indicates that a subclass inherits from a superclass. There are different types of inheritance in Java including single inheritance, multilevel inheritance, and hierarchical inheritance, but multiple inheritance is not supported. Interfaces can be used as a workaround for multiple inheritance by implementing multiple interfaces.
- Inheritance is a mechanism where a derived class inherits properties from a base class. The derived class can have additional properties as well.
- The base class is called the parent/super class and the derived class is called the child/sub class. The subclass inherits all non-private fields and methods from the parent class.
- Key benefits of inheritance include code reusability, extensibility, data hiding and method overriding. Inheritance promotes software reuse by allowing subclasses to reuse code from the parent class.
The document discusses inheritance and polymorphism in Java. It covers defining subclasses that inherit properties and methods from superclasses, invoking superclass constructors using the super keyword, overriding methods in subclasses, and polymorphism which allows calling the same method on objects of different types. It provides examples of defining subclasses for shapes like Circle and Rectangle that extend a GeometricObject superclass.
This document discusses inheritance and polymorphism in Java. It begins by introducing the motivations for inheritance, which is to avoid redundancy when designing classes that share common features, like circles, rectangles, and triangles. It then lists the chapter objectives, which include defining subclasses, invoking superclass constructors and methods, overriding methods, polymorphism, and other inheritance-related concepts. The document provides an example of superclass GeometricObject and subclasses Circle and Rectangle. It also discusses that superclass constructors are not inherited by subclasses but must be invoked explicitly or implicitly using the super keyword.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. An interface cannot have method bodies and can only contain abstract methods and variables that are public, static, and final by default. Classes implement interfaces to inherit their methods. Since Java 8, interfaces can also contain default and static methods.
1. The document discusses Java Database Connectivity (JDBC) and serialization in Java. JDBC is a specification that allows Java programs to store data in databases. Serialization is the process of saving an object's state to a file.
2. To develop a serializable subclass, a class must implement the Serializable interface and include getter and setter methods for its fields. An object of this subclass can then be written to a file using ObjectOutputStream.
3. Deserialization reads an object back from a file using ObjectInputStream and reconstructs it in memory from its stored state.
1. The document discusses Java concepts like serialization, reflection, JDBC, and distributed applications.
2. Serialization allows saving object state to a file by implementing the Serializable interface. Reflection obtains runtime information about classes.
3. JDBC is used to connect to databases and store data permanently. Distributed applications run across a network and allow for data sharing between clients.
Inheritance allows classes to inherit properties and behaviors from parent classes in Java and C#. Both languages support simple, multilevel, and hierarchical inheritance through the use of extends and implements keywords. Java does not support multiple inheritance directly but allows classes to inherit from one parent class and implement multiple interfaces. Constructors and methods can be called or overridden in subclasses using the super and this keywords respectively.
Object Oriented Concepts required to know about the Android Application Programming are discussed over here. Get the most of the details about each and every concept of OO paradigm so you can use them very well in Android Application Programming.
Object Oriented Programming_Chapter 3 (Two Lectures)
1- Let’s think on Inheritance
2- Let’s focus on Superclass’s Constructor
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
The document provides an overview of advanced programming concepts in Java including object-oriented programming (OOP) concepts, arrays and collections, exceptions handling, and multithreading. It discusses OOP concepts like classes, objects, abstraction, encapsulation, inheritance, and polymorphism. It also covers array and collections like Array, ArrayList, List, and Set. Finally, it briefly mentions exceptions handling techniques like try-catch and multithreading in Java.
International Journal of Distributed and Parallel systems (IJDPS)samueljackson3773
The growth of Internet and other web technologies requires the development of new
algorithms and architectures for parallel and distributed computing. International journal of
Distributed and parallel systems is a bimonthly open access peer-reviewed journal aims to
publish high quality scientific papers arising from original research and development from
the international community in the areas of parallel and distributed systems. IJDPS serves
as a platform for engineers and researchers to present new ideas and system technology,
with an interactive and friendly, but strongly professional atmosphere.
Concept of Problem Solving, Introduction to Algorithms, Characteristics of Algorithms, Introduction to Data Structure, Data Structure Classification (Linear and Non-linear, Static and Dynamic, Persistent and Ephemeral data structures), Time complexity and Space complexity, Asymptotic Notation - The Big-O, Omega and Theta notation, Algorithmic upper bounds, lower bounds, Best, Worst and Average case analysis of an Algorithm, Abstract Data Types (ADT)
We introduce the Gaussian process (GP) modeling module developed within the UQLab software framework. The novel design of the GP-module aims at providing seamless integration of GP modeling into any uncertainty quantification workflow, as well as a standalone surrogate modeling tool. We first briefly present the key mathematical tools on the basis of GP modeling (a.k.a. Kriging), as well as the associated theoretical and computational framework. We then provide an extensive overview of the available features of the software and demonstrate its flexibility and user-friendliness. Finally, we showcase the usage and the performance of the software on several applications borrowed from different fields of engineering. These include a basic surrogate of a well-known analytical benchmark function; a hierarchical Kriging example applied to wind turbine aero-servo-elastic simulations and a more complex geotechnical example that requires a non-stationary, user-defined correlation function. The GP-module, like the rest of the scientific code that is shipped with UQLab, is open source (BSD license).
ELectronics Boards & Product Testing_Shiju.pdfShiju Jacob
This presentation provides a high level insight about DFT analysis and test coverage calculation, finalizing test strategy, and types of tests at different levels of the product.
Passenger car unit (PCU) of a vehicle type depends on vehicular characteristics, stream characteristics, roadway characteristics, environmental factors, climate conditions and control conditions. Keeping in view various factors affecting PCU, a model was developed taking a volume to capacity ratio and percentage share of particular vehicle type as independent parameters. A microscopic traffic simulation model VISSIM has been used in present study for generating traffic flow data which some time very difficult to obtain from field survey. A comparison study was carried out with the purpose of verifying when the adaptive neuro-fuzzy inference system (ANFIS), artificial neural network (ANN) and multiple linear regression (MLR) models are appropriate for prediction of PCUs of different vehicle types. From the results observed that ANFIS model estimates were closer to the corresponding simulated PCU values compared to MLR and ANN models. It is concluded that the ANFIS model showed greater potential in predicting PCUs from v/c ratio and proportional share for all type of vehicles whereas MLR and ANN models did not perform well.
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYijscai
With the increased use of Artificial Intelligence (AI) in malware analysis there is also an increased need to
understand the decisions models make when identifying malicious artifacts. Explainable AI (XAI) becomes
the answer to interpreting the decision-making process that AI malware analysis models use to determine
malicious benign samples to gain trust that in a production environment, the system is able to catch
malware. With any cyber innovation brings a new set of challenges and literature soon came out about XAI
as a new attack vector. Adversarial XAI (AdvXAI) is a relatively new concept but with AI applications in
many sectors, it is crucial to quickly respond to the attack surface that it creates. This paper seeks to
conceptualize a theoretical framework focused on addressing AdvXAI in malware analysis in an effort to
balance explainability with security. Following this framework, designing a machine with an AI malware
detection and analysis model will ensure that it can effectively analyze malware, explain how it came to its
decision, and be built securely to avoid adversarial attacks and manipulations. The framework focuses on
choosing malware datasets to train the model, choosing the AI model, choosing an XAI technique,
implementing AdvXAI defensive measures, and continually evaluating the model. This framework will
significantly contribute to automated malware detection and XAI efforts allowing for secure systems that
are resilient to adversarial attacks.
Sorting Order and Stability in Sorting.
Concept of Internal and External Sorting.
Bubble Sort,
Insertion Sort,
Selection Sort,
Quick Sort and
Merge Sort,
Radix Sort, and
Shell Sort,
External Sorting, Time complexity analysis of Sorting Algorithms.
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxRishavKumar530754
LiDAR-Based System for Autonomous Cars
Autonomous Driving with LiDAR Tech
LiDAR Integration in Self-Driving Cars
Self-Driving Vehicles Using LiDAR
LiDAR Mapping for Driverless Cars
The Fluke 925 is a vane anemometer, a handheld device designed to measure wind speed, air flow (volume), and temperature. It features a separate sensor and display unit, allowing greater flexibility and ease of use in tight or hard-to-reach spaces. The Fluke 925 is particularly suitable for HVAC (heating, ventilation, and air conditioning) maintenance in both residential and commercial buildings, offering a durable and cost-effective solution for routine airflow diagnostics.
Value Stream Mapping Worskshops for Intelligent Continuous SecurityMarc Hornbeek
This presentation provides detailed guidance and tools for conducting Current State and Future State Value Stream Mapping workshops for Intelligent Continuous Security.
Value Stream Mapping Worskshops for Intelligent Continuous SecurityMarc Hornbeek
Ad
Inheritance and interface
1. 1
C.K.PITHAWALA COLLEGE OF
ENGINEERING & TECHNOLOGY, SURAT
Branch :- computer 3nd Year/5rd SEM (Div. D)
ALA Subject :- OOPJ
Topic Name :- Inheritance and interfaces
Group No :-B4
Enrolment No Name
Submitted To
160090107051
160090107049
160090107025
160090107044
160090107059
Shubham Sharma
Pakshal Shah
Hariom Maurya
Pravin Rathod
Naitik Vajani
Dr. Ami Choksi
3. Contents
Introduction to inheritance
Types of Inheritance
Method overriding using inheritance
Super keyword
Final keyword
Interfaces
4. Introduction to inheritance
As the name suggests, inheritance means to take something that is already made.
Inheritance is a unique feature of object oriented programming.
Inheritance is mainly used for code reusability.
The class which acquires the properties of the class is called as sub-class and the
class whose properties are acquired is called as superclass.
Consider the example:
As shown in example, the child inherit the properties of
father. Hence child will be subclass while father is a
superclass.
5. IS-A Relationship
Inheritance defines IS-A relationship between a super class and its subclass
For Example:
Car IS A vehicle
Bike IS A vehicle
EngineeringCollege IS A college
MedicalCollege IS A college
In java, inheritance is implemented with the help of keyword - “extends”.
6. Types of inheritance
• Single inheritance
• Multilevel inheritance
• Hierarchical inheritance
7. Single Inheritance
A structure having one and only one super class as well as subclass.
Child class is authorized to access the property of Parent class.
Super class
Subclass
Syntax :
class A
{
………….
………….
}
class B extends A
{
………….
………….
}
Sharing of
properties
8. Multilevel inheritance
B
C
A
When a class is derived from a class
which is also derived from a class, then
such type of inheritance is called as
multilevel inheritance.
Syntax :
class A
{
………….
………….
}
class B extends A
{
………….
………….
}
class C extends B
{
………….
………….
}
9. Hierarchical inheritance
In hierarchical inheritance, one class is
inherited by many subclasses.
subclasses must be connected with only
one super class.
B D
A
C
Syntax :
class A
{
………….
………….
}
class B extends A
{
………….
………….
}
class C extends A
{
………….
………….
}
class D extends A
{
………….
………….
}
10. Why multiple inheritance is not supported in java ?
To reduce the complexity and simply the language, java doesn’t support
multiple inheritance.
Suppose there are 3 classes A,B and C. The C class inherits class A and B.
If A and B have same method and you call it from child class object, there
will be ambiguity to call method of A or B class.
Since compile time errors are better than run time errors, java renders
compile time error if you inherit 2 classes.
11. Access Specifiers in java
There are four Access Specifiers in Java
1. Public: When a member of a class is declared as public specifier, it can be
accessed from any code.
2. Protected: Protected is only applicable in case of Inheritance. When a
member of a class is declared as protected, it can only be accessed by the
members of its class or subclass.
3. Private: A member of a class is declared as private specifier, can only be
accessed by the member of its class.
4. Default: When you don't specify a access specifier to a member, Java
automatically specifies a default. And the members modified by default can only
be accessed by any other code in the package, but can't be accessed outside
of a package.
13. A program demonstrating inheritance in Java
27.public static void main(String[] args) {
28.cylinder c1=new cylinder();
29.c1.height(5.12,6.5);
30.double area,volume;
31.volume=c1.getdata();
32.System.out.println("Volume of cylinder :"+volume);
33.circle ob=new circle();
34.ob.radius(4.44);
35.ob.getdata();
36.area=ob.getdata();
37.System.out.println("area of circle:"+area);
38.}
39.}
Output of program:
Volume of cylinder :679.2447999999
Area of circle:61.90070400000001
14. Method overriding in Java
Subclasses inherit all methods from their superclass
Sometimes, the implementation of the method in the superclass does not
provide the functionality required by the subclass.
In these cases, the method must be overridden.
To override a method, provide an implementation in the subclass.
The method in the subclass MUST have the exact same signature as the
method it is overriding.
15. Program demonstrating method overriding
1. class A{
2. void display()
3. {
4. System.out.println("This is parent class.");
5. }
6. }
7. class B extends A{
8. void display()
9. {
10.System.out.println("This is first child class");
11.}
12.public static void main(String[] args) {
13.B b1=new B();
14.b1.display();
15.}
16.}
Output:
This is first child class
16. Super keyword
As the name suggest super is used to access the members of the super class.
It is used for two purposes in java.
1. The first use of keyword super is to access the data variables of the super class
hidden by the sub class.
e.g. Suppose class A is the super class that has two instance variables as int a and
float b.
class B is the subclass that also contains its own data members named a and b.
Then we can access the super class (class A) variables a and b inside the subclass
class B just by calling the following command.
super.member;
17. Super keyword
2.Use of super to call super class constructor: The second use of the keyword
super in java is to call super class constructor in the subclass.
This functionality can be achieved just by using the following command.
super(param-list);
Here parameter list is the list of the parameter requires by the constructor in the
super class.
super must be the first statement executed inside a super class constructor.
If we want to call the default constructor then we pass the empty parameter
list.
If we dont use super then the compiler does this task implicitly to instantiate
superclass members.
18. Program to demonstrate Super Keyword
1) class Animal{
2) Animal(){System.out.println("animal is created");}
3) }
4) class Dog extends Animal{
5) Dog(){
6) super();
7) System.out.println("dog is created");
8) }
9) }
10)class TestSuper3{
11)public static void main(String args[]){
12)Dog d=new Dog();
13)}}
Output:
Animal is created
Dog is created
19. Final Keyword
The final keyword in java is used to restrict the user.
The final keyword can be used in mainly 3 context.
1. Variable: If you make any variable final, you cannot change the
value of final variable.
Example:
1. class bike{
2. final int speedlimit=50;
3. void run(){
4. speedlimit=60; //Compile-time error
5. }
6. public static void main(String[] args){
7. Bike ob=new bike();
8. ob.run();
9. }
10. }
Output:
Compile time error
20. Final keyword
2. Method: If you make a method final, you cannot override it.
Example:
1. class bike{
2. final void run(){
3. System.out.println(“Running”);
4. }
5. class honda extends run(){
6. void run(){ //compile time error
7. System.out.println(“Running at 100 kmph”);
8. }
9. }
10. public static void main(String[] args){
11. Bike ob=new bike();
12. ob.run();
13. }
14. }
Output:
Compile time error
21. Final keyword
3. Class : If you make any class final, you cannot extend it.
Example:
1. final class bike{
2. //code here
3. }
4. class honda extends run(){ //Compile time error
5. void run(){
6. System.out.println(“Running at 100 kmph”);
7. }
8. public static void main(String[] args){
9. honda ob=new honda();
10. ob.run();
11. }
12. }
Output:
Compile time error
22. Interface in Java
Java Supports a special feature called interface.
This feature helps to connect a class with more than
one classes (in order to achieve multiple inheritance).
For this type of connectivity java uses ‘implements’
keyword.
A class can implements multiple interfaces at a same
time
All the variables defined in the interface are final and
cannot be changed in subclass.
Syntax :
interface A
{
int a=10;
public int getdata();
public void display();
}
interface B
{
public void getmarks();
}
class D implements A,B
{
………..
………..
}
23. Comparison between interface and abstract class
Features Interface Abstract class
Multiple inheritance A class may implement
multiple interfaces
A class can extend only one
abstract class
Default implementation An interface cannot provide
any code at all
An abstract class can
provide complete code,
default code
Constants Constants are by default
Static and final
Both static and instance
constants are allowed
Object An object of interface is
never created
An object of abstract class
can be created